summaryrefslogtreecommitdiffstats
path: root/src/game.c
blob: 013c0a418ace0c5247ab0967a96727450ea50719 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <stdlib.h>
#include <unistd.h>
#include <curses.h>

#include "game.h"
#include "player.h"
#include "paddle.h"

static void wait_game(struct game *g);
static void input_game(struct game *g);
static void clear_game(struct game *g);
static void update_game(struct game *g);
static void draw_game(struct game *g);

struct game *
new_game(char balls)
{
	struct game *g;

	g = malloc(sizeof(*g));
	if (g == NULL) {
		return NULL;
	}

	g->players[0] = new_player(0);
	g->players[1] = new_player(1);
	g->min_balls = balls;
	g->cur_balls = 0;

	return g;
}

void
free_game(struct game *g)
{
	free_player(g->players[0]);
	free_player(g->players[1]);

	free(g);
}

void
run_game(struct game *g)
{
	g->running = 1;

	while (g->running) {
		input_game(g);
		clear_game(g);
		update_game(g);
		draw_game(g);
		wait_game(g);
	}
}

static void
wait_game(struct game *g)
{
	++g;
	usleep(1000000 / 30);
}

static void
input_game(struct game *g)
{
	int c;

	timeout(0);
	for (;;) {
		c = getch();
		if (c == 'q') {
			g->running = 0;
		} else if (c == ERR) {
			break;
		} else if (c == 'a') {
			g->players[0]->paddle_h.dir = -1;
		} else if (c == 'd') {
			g->players[0]->paddle_h.dir = 1;
		} else if (c == 'w') {
			g->players[0]->paddle_v.dir = -1;
		} else if (c == 's') {
			g->players[0]->paddle_v.dir = 1;
		} else if (c == KEY_LEFT) {
			g->players[1]->paddle_h.dir = -1;
		} else if (c == KEY_RIGHT) {
			g->players[1]->paddle_h.dir = 1;
		} else if (c == KEY_UP) {
			g->players[1]->paddle_v.dir = -1;
		} else if (c == KEY_DOWN) {
			g->players[1]->paddle_v.dir = 1;
		}
	}
}

static void
clear_game(struct game *g)
{
	struct ball *b;

	clear_paddle(&g->players[0]->paddle_h);
	clear_paddle(&g->players[0]->paddle_v);
	clear_paddle(&g->players[1]->paddle_h);
	clear_paddle(&g->players[1]->paddle_v);

	for (b = g->balls_head; b != NULL; b = b->next) {
		clear_ball(b);
	}
}

static void
update_game(struct game *g)
{
	struct ball *b;

	update_paddle(&g->players[0]->paddle_h);
	update_paddle(&g->players[0]->paddle_v);
	update_paddle(&g->players[1]->paddle_h);
	update_paddle(&g->players[1]->paddle_v);

	for (b = g->balls_head; b != NULL; b = b->next) {
		update_ball(b);
	}

	while (g->cur_balls < g->min_balls) {
		b = new_ball(BALL_START_X_NORMAL, BALL_START_Y_NORMAL, 1, 0.f);
		b->next = g->balls_head;
		g->balls_head = b;
		++g->cur_balls;
	}
}

static void
draw_game(struct game *g)
{
	struct ball *b;

	draw_paddle(&g->players[0]->paddle_h);
	draw_paddle(&g->players[0]->paddle_v);
	draw_paddle(&g->players[1]->paddle_h);
	draw_paddle(&g->players[1]->paddle_v);

	for (b = g->balls_head; b != NULL; b = b->next) {
		draw_ball(b);
	}
}