forked from Metalab/The-Flying-Camera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameLoop.rb
82 lines (65 loc) · 1.69 KB
/
GameLoop.rb
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
#
# GameLoop.rb
# CtF
#
# Created by Thomas R. Koll on 09.12.10.
# Copyright (c) 2010 ananasblau. All rights reserved.
#
class GameLoop
attr_accessor :view
attr_accessor :timer, :player, :elements, :scene, :teams, :stop, :shots, :total_time
def initialize(view)
self.view = view
self.scene = Scene.new
self.teams = [
Team.new('Kaiserliche Fliegertruppe', [0.8, 0.8, 0.8], 3),
Team.new('Royal Flying Corps', [0.6, 0.62, 0.6], 3)
]
self.elements ||= self.teams.collect(&:planes).flatten
self.player = Player.new
start_timer
self.total_time = Score.new(-0.95, -0.8, 0)
end
def start_timer
timer ||= NSTimer.scheduledTimerWithTimeInterval( 1/60.0,
target:self, selector:"timer_fired:",
userInfo:nil, repeats:true)
end
def timer_fired(timer)
tick(1/60.0)
if view.active
view.openGLContext.flushBuffer
view.setNeedsDisplay true
end
end
def tick(seconds)
scene.redraw(seconds)
self.total_time.draw
self.player.score.draw
return if self.stop
self.total_time += seconds
player.redraw(seconds)
elements.map do |e|
e.find_target(elements.reject {|e1| e1.team == e.team})
e.redraw(seconds)
end
player.make_picture(elements)
end
def stop_timer
if @timer != nil then
@timer.invalidate
@timer = nil
end
end
def draw_time
end
def set_viewport_rectangle(bounds)
return
glViewport(bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity
gluPerspective(30, bounds.size.width / bounds.size.height, 1.0, 1000.0)
# glMatrixMode(GL_MODELVIEW)
# glLoadIdentity
end
end