-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiplayer.ceu
287 lines (269 loc) · 8.29 KB
/
multiplayer.ceu
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "sdl.ceu"
native do
##include <unistd.h>
##include <fcntl.h>
end
input void SDL_REDRAW;
input void SDL_QUIT;
input int SDL_DT;
input _SDL_KeyboardEvent&& SDL_KEYDOWN;
input _SDL_KeyboardEvent&& SDL_KEYUP;
input _SDL_MouseButtonEvent&& SDL_MOUSEBUTTONDOWN;
event (_SDL_Rect,int) collideTime;
input int WIN;
input char SERIAL;
var _SDL_Window&? window;
finalize
window = &_SDL_CreateWindow("Game",
_SDL_WINDOWPOS_UNDEFINED,
_SDL_WINDOWPOS_UNDEFINED,
640, 480, _SDL_WINDOW_SHOWN);
with
_SDL_DestroyWindow(&&window!);
end
var _SDL_Renderer&? renderer;
finalize
renderer = &_SDL_CreateRenderer(&&window!, -1, 0);
with
_SDL_DestroyRenderer(&&renderer!);
end
class Beam with
var _SDL_Renderer& ren;
var int vy = -10;
var int y = 0;
var int x = 0;
event void beam_collide;
var u8 red=0x00;
var u8 green=0x00;
var u8 blue=0x00;
var _SDL_Rect r = _SDL_Rect(x,y, 5,50);
do
par do
loop do
var int dt = await SDL_DT;
y = y + this.vy*dt;
if y < 0 then
_printf("Should Escape\n");
escape true;
end
end
with
every SDL_REDRAW do
//_printf("redraw %d %d\n",x,y);
_SDL_SetRenderDrawColor(&&this.ren,
this.red,this.green,this.blue,0x00);
this.r.y = y / 1000;
this.r.x = x / 1000;
_SDL_RenderFillRect(&&this.ren, &&this.r);
end
with
await this.beam_collide;
_printf("Collision BEAM\n");
escape true;
end
end
class Player with
var _SDL_Renderer& ren;
pool Beam[]& beams;
var int x = 0;
var int y = 0;
var _SDL_Rect r = _SDL_Rect(x,y, 20,50);
var int joystick = false;
event void playerHit;
var int pnumber;
do
var int life = 3;
var int vx = 0;
var int vy = 0;
par do
every dt in SDL_DT do
x = x + vx*dt;
y = y + vy*dt;
if this.x > 640000 then
this.x = 0;
end
if this.x < 0 then
this.x = 640000;
end
end
with //keyboard controls
if (joystick == false) then
par do
loop do
var _SDL_KeyboardEvent&& key;
key = await SDL_KEYDOWN until key:keysym.sym==_SDLK_LEFT;
vx = vx - 100;
key = await SDL_KEYUP until key:keysym.sym==_SDLK_LEFT;
vx = vx + 100;
end
with
loop do
var _SDL_KeyboardEvent&& key;
key = await SDL_KEYDOWN until key:keysym.sym==_SDLK_RIGHT;
vx = 100 + vx;
key = await SDL_KEYUP until key:keysym.sym==_SDLK_RIGHT;
vx = vx - 100 ;
end
with
loop do
var _SDL_KeyboardEvent&& key;
key = await SDL_KEYDOWN until key:keysym.sym==_SDLK_UP;
_printf("fire %d %d\n",x,y);
spawn Beam in this.beams with
this.vy = 500;
this.y = y + 50000;
this.x = x ;
this.ren = &ren;
this.red = 0xCC;
end;
end
end
else //arduino controls
par do
var int orvx = vx;
var int rightvx = vx + 100;
var int leftvx = vx - 100;
var char lastserial = 0;
loop do
var char c = await SERIAL;
//_printf("%c\n",c);
if(c != lastserial) then
if(c == 'W') then
_printf("BEAM\n");
spawn Beam in this.beams with
this.vy = -500;
this.y = y -50000;
this.x = x + 7500;
this.ren = &ren;
this.green= 0xBB;
end;
end
if (c == 'D') then
vx = rightvx;
// _printf("Right\n");
end
if(c == 'A') then
vx = leftvx;
// _printf("Left\n");
end
if(c == '0') then
vx = orvx;
end
lastserial = c;
end
end
_printf("FINISHED reacting\n");
with
var char c = 'x';
var int h =0;
var int it = 0;
async (h,c) do
native @nohold _fscanf;
native @nohold _fcntl;
native @nohold _read;
var int fd = _open("/dev/cu.usbmodem1421",_O_RDONLY);
var int flags = _fcntl(fd,_F_GETFL,0);
if(_fcntl(fd,_F_SETFL,flags | _O_NONBLOCK)) then
_printf("ERROR NO CONTROLLER\n");
else
_printf("Joystick ON\n");
loop do
var int ret = _read(fd,&&c,1);
if ret >= 0 then
emit SERIAL => c;
end
end
end
end
_printf("FINISHED reading\n");
end
end
with
every SDL_REDRAW do
_SDL_SetRenderDrawColor(&&this.ren,
0x00,0x00,0x00,0x00);
r.x = x / 1000;
r.y = y / 1000;
_SDL_RenderFillRect(&&this.ren, &&r);
end
with
loop do
await SDL_DT;
await this.playerHit;
this.life = this.life -1;
_printf("life %d\n",this.life);
if this.life <= 0 then
_printf("GAME OVER\n");
//_exit(0);
var int n = (pnumber % 2) +1;
async (n) do
emit WIN => n;
end
escape true;
end
end
end
end
par/or do
every SDL_REDRAW do
_SDL_SetRenderDrawColor(&&renderer!,
0xFF,0xFF,0xFF,0x00);
_SDL_RenderFillRect(&&renderer!, null);
end
with
pool Beam[] beams;
var Player p1 with
this.ren = &renderer!;
this.x = (640/2 - 20/2) * 1000;
this.joystick = false;
this.beams = &beams;
this.pnumber = 1;
end;
var Player p2 with
this.ren = &renderer!;
this.x = (640/2 - 20/2) * 1000;
this.y = (480-50) * 1000;
this.joystick = true;
this.beams = &beams;
this.pnumber = 2;
end;
every SDL_DT do
var Beam&&?[] beamsHit;
var Beam&&?[] beamsHit2;
loop b in beams do
if _SDL_HasIntersection(&&b:r,&&p1.r) then
beamsHit = []..beamsHit..[b];
_printf("hit player \n");
_printf("HERO HIT\n");
end
if _SDL_HasIntersection(&&b:r,&&p2.r) then
beamsHit2 = []..beamsHit2..[b];
_printf("hit player \n");
_printf("HERO HIT\n");
end
end
loop i in $beamsHit do
if beamsHit[i]? then
emit beamsHit[i]!:beam_collide;
emit p1.playerHit;
end
end
loop i in $beamsHit2 do
if beamsHit2[i]? then
emit beamsHit2[i]!:beam_collide;
emit p2.playerHit;
end
end
end
with
every SDL_REDRAW do
_SDL_RenderPresent(&&renderer!);
end
with
var int winner = await WIN;
_printf("player %d won", winner );
escape true;
with
await SDL_QUIT;
end
escape 0;