-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsample2.lua
579 lines (458 loc) · 12.3 KB
/
sample2.lua
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
function find_if(items, cond)
for key, item in pairs(items) do
if cond(item) then
return key
end
end
return nil
end
-- class Point
Point = {}
Point.__index = Point
function Point:new(x, y)
assert(x, "Point:new / x is nil")
assert(y, "Point:new / y is nil")
assert(type(x) == "number", "Point:new / x is not number")
assert(type(y) == "number", "Point:new / y is not number")
local new_instance =
{
x = x,
y = y
}
setmetatable(new_instance, self)
return new_instance
end
function Point:__add(other)
assert(other, "Pont:__add / other is nil")
assert(getmetatable(other) == Point, "Point:__add / other is not Point")
return Point:new(self.x + other.x, self.y + other.y)
end
function Point:__tostring()
return string.format("(%d, %d)", self.x, self.y)
end
setmetatable(Point, { __call = Point.new })
-- class Boundary
Boundary = {}
Boundary.__index = Boundary
function Boundary:new(lefttop, rightbottom)
assert(lefttop, "Boundary:new / lettop is nil")
assert(rightbottom, "Boundary:new / rightbottom is nil")
assert(getmetatable(lefttop) == Point, "Boundary:new / leftop is not Point")
assert(getmetatable(rightbottom) == Point, "Boundary:new / rightbottom is not Point")
local new_instance =
{
lefttop_ = lefttop,
rightbottom_ = rightbottom
}
setmetatable(new_instance, self)
new_instance:rearrenges()
return new_instance
end
function Boundary:rearrenges()
if self.rightbottom_.x < self.lefttop_.x then
self.rightbottom_.x, self.lefttop_.x = self.lefttop_.x, self.rightbottom_.x
end
if self.rightbottom_.y < self.lefttop_.y then
self.rightbottom_.y, self.lefttop_.y = self.lefttop_.y, self.rightbottom_.y
end
end
function Boundary:__add(other)
return Boundary:new(self.lefttop + other.lefttop, self.rightbottom + other.rightbottom)
end
function Boundary:lefttop()
return self.lefttop_
end
function Boundary:rightbottom()
return self.rightbottom_
end
function Boundary:leftbottom()
return Point(self.lefttop_.x, self.rightbottom_.y)
end
function Boundary:righttop()
return Point(self.rightbottom_.x, self.lefttop_.y)
end
function Boundary:left()
return self.lefttop_.x
end
function Boundary:right()
return self.rightbottom_.x
end
function Boundary:top()
return self.lefttop_.y
end
function Boundary:bottom()
return self.rightbottom_.y
end
function Boundary:includes(point)
assert(point, "Boundary:includes / point is nil")
assert(getmetatable(point) == Point, "Boundary:includes / point is not Point")
return (self.lefttop_.x <= point.x) and (point.x <= self.rightbottom_.x) and
(self.lefttop_.y <= point.y) and (point.y <= self.rightbottom_.y)
end
function Boundary:overlaps_with(other)
assert(other, "Boundary:overlaps_with / other is nil")
assert(getmetatable(other) == Boundary, "Boundary:overlaps_with / other is not Boundary")
return self:includes(other:lefttop()) or self:includes(other:righttop()) or
self:includes(other:leftbottom()) or self:includes(other:rightbottom()) or
other:includes(self:lefttop()) or other:includes(self:righttop()) or
other:includes(self:leftbottom()) or other:includes(self:rightbottom())
end
function Boundary:__tostring()
return tostring(self.lefttop_) .. "-" .. tostring(self.rightbottom_)
end
setmetatable(Boundary, { __call = Boundary.new })
-- class RGBColor
RGBColor = {}
RGBColor.__index = RGBColor
function RGBColor:new(r, g, b)
local new_instance =
{
rgb = { r = r or 0, g = g or 0, b = b or 0 }
}
setmetatable(new_instance, self)
return new_instance
end
function RGBColor:__tostring()
return string.format("(%f, %f, %f)", self.rgb.r, self.rgb.g, self.rgb.b)
end
setmetatable(RGBColor, { __call = RGBColor.new })
-- class Text
Text =
{
SCALE = 0.1,
COLOR = RGBColor(1, 0, 0)
}
Text.__index = Text
function Text:new(position)
assert(position, "Text:new / position is nil")
assert(getmetatable(position) == Point, "Text:new / poistion is not Point")
local new_instance =
{
position = position,
text = ""
}
setmetatable(new_instance, self)
return new_instance
end
function Text:set(text)
self.text = text
end
function Text:get()
return text
end
function Text:draw()
gl.color3dv(self.COLOR.rgb.r, self.COLOR.rgb.g, self.COLOR.rgb.b)
gl.push_matrix()
gl.translatef(self.position.x, self.position.y, 0)
gl.scalef(self.SCALE, -self.SCALE, 0)
for i = 1, #self.text do
glut.stroke_character(glut.STROKE_ROMAN, string.byte(self.text, i))
end
gl.pop_matrix()
end
-- class ShapeObject
ShapeObject = {}
ShapeObject.__index = ShapeObject
function ShapeObject:new(position)
local new_instance = {}
setmetatable(new_instance, self)
return new_instance
end
function ShapeObject:is_visible()
return Game:screen():includes(self.position)
end
function ShapeObject:draw_with_color(color)
gl.color3dv(color.rgb.r, color.rgb.g, color.rgb.b)
gl.primitive(gl.POLYGON, function()
for _, point in pairs(self.points) do
gl.vertex2i(self.position.x + point.x, self.position.y + point.y)
end
end)
end
function ShapeObject:move_(delta)
assert(delta, "ShapeObject:move_ / delta is nil")
assert(getmetatable(delta) == Point, "ShapeObject:move_ / delta is not Point")
self.position = self.position + delta
end
function ShapeObject:move(delta)
assert(delta, "ShapeObject:move / delta is nil")
assert(getmetatable(delta) == Point, "ShapeObject:move / delta is not Point")
self:move_(delta)
end
-- class Bullet (inherited ShapeObject)
Bullet = {}
Bullet.__index = Bullet
setmetatable(Bullet, { __index = ShapeObject })
function Bullet:new(point, delta, color)
local new_instance =
{
position = point or Point(0, 0),
delta_y = delta or 0,
color = color or RGBColor:new(),
visible = point and true or false,
points =
{
Point(-3, -5),
Point( 0, -10),
Point( 3, -5),
Point( 0, 0)
}
}
setmetatable(new_instance, self)
return new_instance
end
function Bullet:boundary()
return Boundary(self.position + Point(-3, -10), self.position + Point(3, 0))
end
function Bullet:set_visible(visible)
self.visible = visible
end
function Bullet:is_visible()
return self.visible
end
function Bullet:move()
if self.visible then
self:move_(Point(0, self.delta_y))
self.visible = Game:screen():includes(self.position)
end
end
function Bullet:draw()
if self.visible then
self:draw_with_color(self.color)
end
end
-- class Alien (inherited ShapeObject)
Alien =
{
BULLET_SPEED = 10,
COLOR = RGBColor:new(1, 0.5, 0),
points =
{
Point( -5, -5),
Point( 5, -5),
Point( 10, 0),
Point( 5, 5),
Point( 2, 2),
Point( -2, 2),
Point( -5, 5),
Point(-10, 0)
}
}
Alien.__index = Alien
setmetatable(Alien, { __index = ShapeObject })
function Alien:new()
local new_instance =
{
speed_x = 5,
speed_y = 10,
position = Point(20, 20),
}
setmetatable(new_instance, self)
return new_instance
end
function Alien:boundary()
return Boundary(self.position + Point(-10, -5), self.position + Point(10, 5))
end
function Alien:fire()
return Bullet:new(self.position, self.BULLET_SPEED, self.COLOR)
end
function Alien:turn()
self.speed_x = -self.speed_x
end
function Alien:move()
self:move_(Point(self.speed_x, 0))
screen = Game:screen()
if (self.position.x < screen:left()) or (screen:right() < self.position.x) then
self:turn()
self:move_(Point(0, self.speed_y))
end
end
function Alien:draw()
self:draw_with_color(self.COLOR)
end
-- class Aliens
Aliens = {}
Aliens.__index = Aliens
function Aliens:new()
local new_instance =
{
aliens = {},
bullets = {}
}
setmetatable(new_instance, self)
return new_instance
end
function Aliens:add_alien()
table.insert(self.aliens, Alien:new())
end
function Aliens:is_hit_by(object)
if not object:is_visible() then
return false
end
local key = find_if(self.aliens, function(item) return object:boundary():overlaps_with(item:boundary()) end)
if not key then
return false
end
self.aliens[key] = nil
return true
end
function Aliens:hits(object)
return find_if(self.bullets, function(item) return object:boundary():overlaps_with(item:boundary()) end)
end
function Aliens:clear_bullets()
self.bullets = {}
end
function Aliens:draw()
for _, alien in pairs(self.aliens) do alien:draw() end
for _, bullet in pairs(self.bullets) do bullet:draw() end
end
function Aliens:move()
for _, alien in pairs(self.aliens) do alien:move() end
for _, bullet in pairs(self.bullets) do bullet:move() end
for key, bullet in pairs(self.bullets) do
if not bullet:is_visible() then
self.bullets[key] = nil
end
end
for _, alien in pairs(self.aliens) do
if math.random(20) == 1 then
table.insert(self.bullets, alien:fire())
end
end
end
-- class Ship (inherited ShapeObject)
Ship =
{
DELTA = 10,
COLOR = RGBColor(0, 0, 1),
points =
{
Point(-10, 0),
Point( 0, -10),
Point( 10, 0),
Point( 10, 10),
Point(-10, 10)
}
}
Ship.__index = Ship
setmetatable(Ship, { __index = ShapeObject })
function Ship:new(position)
local new_instance =
{
position = position
}
setmetatable(new_instance, self)
return new_instance
end
function Ship:boundary()
return Boundary(self.position + Point(-10, 0), self.position + Point(10, 10))
end
function Ship:move_left()
self:move(Point(-self.DELTA, 0))
end
function Ship:move_right()
self:move(Point(self.DELTA, 0))
end
function Ship:move_up()
self:move(Point(0, -self.DELTA))
end
function Ship:move_down()
self:move(Point(0, self.DELTA))
end
function Ship:fire()
return Bullet:new(self.position, -10, self.COLOR)
end
function Ship:draw()
self:draw_with_color(self.COLOR)
end
-- class Game (singleton)
Game =
{
INTERVAL = 100,
SPACE = 32,
CTRL_C = 3,
POINT_PER_ALIEN = 10
}
Game.width = 320
Game.height = 240
Game.my_ship = Ship:new(Point(Game.width / 2, Game.height / 2))
Game.my_bullet = Bullet:new()
Game.aliens = Aliens:new()
Game.text = Text:new(Point(10, 12))
Game.score = 0
Game.left = 3
Game.interval = Game.INTERVAL -- timer interval
Game.timer_enabled = true -- timer enable flag
function Game:draw()
gl.clear(gl.COLOR_BUFFER_BIT)
self.my_ship:draw()
self.my_bullet:draw()
self.aliens:draw()
self.text:draw()
glut.swap_buffers()
end
function Game:display() -- callback function of display
self:draw()
end
function Game:reshape(w, h) -- callback function of reshape
gl.viewport(0, 0, w, h)
self.width = w
self.height = h
gl.load_identity()
gl.ortho(-0.5, w - 0.5, h - 0.5, -0.5, -1.0, 1.0)
end
function Game:keyboard(key, x, y) -- callback functionn of keyboard event
if (key == self.SPACE) and not self.my_bullet:is_visible() then
self.my_bullet = self.my_ship:fire()
elseif key == self.CTRL_C then
os.exit()
end
end
function Game:special(key, x, y) -- callback function of spectial key event
if key == glut.KEY_LEFT then
self.my_ship:move_left()
elseif key == glut.KEY_UP then
self.my_ship:move_up()
elseif key == glut.KEY_RIGHT then
self.my_ship:move_right()
elseif key == glut.KEY_DOWN then
self.my_ship:move_down()
end
end
function Game:timer(n) -- callback function of timer
self.my_bullet:move()
self.aliens:move()
if math.random(20) == 1 then
self.aliens:add_alien()
end
if self.aliens:is_hit_by(self.my_bullet) then
self.score = self.score + self.POINT_PER_ALIEN
self.my_bullet:set_visible(false)
end
local my_ship_hit = self.aliens:hits(self.my_ship)
self.text:set(string.format("SCORE : %06d / LEFT : %d", self.score, self.left))
local clear_color = my_ship_hit and RGBColor(1.0, 0.8, 0.8) or RGBColor(1.0, 1.0, 1.0)
gl.clear_color(clear_color.rgb.r, clear_color.rgb.g, clear_color.rgb.b, 1.0)
self:draw()
self.interval = self.INTERVAL
if my_ship_hit then
if self.left == 0 then
self.timer_enabled = false
return
end
self.interval = self.INTERVAL * 10
self.left = self.left - 1
self.aliens:clear_bullets()
end
end
function Game:screen()
return Boundary(Point(0, 0), Point(self.width, self.height))
end
-- entry point
glut.init_window_position(100, 100)
glut.init_window_size(Game.width, Game.height)
glut.init(arg)
glut.init_display_mode(glut.RGBA + glut.DOUBLE)
glut.create_window("sample2")
gl.clear_color(1.0, 1.0, 1.0, 1.0)
glut.main_loop(Game)