-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscene_space.rb
More file actions
376 lines (318 loc) · 11.5 KB
/
scene_space.rb
File metadata and controls
376 lines (318 loc) · 11.5 KB
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
module Scenes
class SpaceScene < BasicScene
attr_reader :space, :particle_system, :ships, :modules, :font, :camera
def initialize(window)
super
@mount_point_image = Gosu::Image.new(window, "media/mount_point.png", false)
@font = Gosu::Font.new(window, Gosu::default_font_name, 20)
# Time increment over which to apply a physics "step" ("delta t")
@dt = (1.0/60.0)
# Create our Space and set its damping
# A damping of 0.8 causes the ship bleed off its force and torque over time
# This is not realistic behavior in a vacuum of space, but it gives the game
# the feel I'd like in this situation
@space = CP::Space.new
@space.damping = 1.0 # 0.8
@particle_system = ParticleSystem.new(self)
#@connection_manager = ConnectionManager.new(self)
@space.add_collision_func(:rocket, :module) do |rocket_shape, ship_shape|
(@modules + @ships).each do |mod|
#next if mod.shape == rocket_shape # Don't collide with self!
body = ((mod.is_a? Modules::BasicModule) || (mod.is_a? Projectiles::BasicProjectile) ? mod.shape.body : mod.body)
force_multiplier = 1000.0
diff = body.p - rocket_shape.body.p
length = diff.length
dir = diff.normalize_safe
force_multiplier *= 10 if mod.is_a? Ship
body.apply_impulse(dir * ((1.0/length) * force_multiplier), CP::Vec2.new(0.0, 0.0))
end #TODO reimplement
schedule_remove(rocket_shape.body.object) # I'm gone!
#schedule_remove(ship_shape.body.object) #lol! FIXME
#mod = ship_shape.object
#if false && mod.attached?
# mod.ship.remove_module(mod)
# mod.parent.unmount(mod) if mod.mounted?
# mod.mount_points.each do |mp|
# mod.unmount(mp.child)
# end
#end
ship_shape.object.disband
false # Don't do any physics here!
end
@modules = []
@remove_queue = []
scheme = {
:type => :Cockpit,
:mounts => {
2 => {
:mount_on => 3,
:type => :Tube,
:mounts => {
1 => {
:mount_on => 2,
:type => :Thruster,
:angle => 180,
:triggers => {Gosu::KbD => :thrust}
},
0 => {
:mount_on => 2,
:type => :Thruster,
:angle => 0,
:triggers => {Gosu::KbA => :thrust}
}
}
},
0 => {
:mount_on => 1,
:type => :Thruster,
:angle => -90,
:triggers => {Gosu::KbW => :thrust},
:mounts => {
0 => {
:mount_on => 2,
:type => :Thruster,
:angle => 0,
:triggers => {Gosu::KbD => :thrust}
}
}
},
1 => {
:mount_on => 0,
:type => :Thruster,
:angle => -90,
:triggers => {Gosu::KbW => :thrust},
:mounts => {
1 => {
:mount_on => 2,
:type => :Thruster,
:angle => 180,
:triggers => {Gosu::KbA => :thrust}
}
}
},
3 => {
:mount_on => 2,
:type => :Cannon,
:angle => 90,
:triggers => {Gosu::KbS => :fire}
}
},
:x => 180,
:y => 300
}
#scheme = YAML::load_file('ship.yaml')
#@modules << Modules::Thruster.new(self, 200, 200, 0)
#@modules << Modules::Cockpit.new(self, 250, 200, 0)
#@modules << Modules::Tube.new(self, 350, 200, 0)
#@modules << Modules::Cannon.new(self, 400, 200, 0)
#@cockpit = build_from_scheme(scheme)
@ships = []
@ships << new_ship(scheme)
#@modules.each do |mod|
# @connection_manager.connections_from(mod).each do |con| con.test_loop end
#end
@camera = Camera.new(self, 0, 0)#@cockpit.shape.body.p.x, @cockpit.shape.body.p.y)
@camera.track(@ships.first.cockpit.shape.body)
#@camera.track(@cockpit)
end
def build_from_scheme(scheme, ship, parent=nil, mount_point=nil)
module_class = Modules.const_get(scheme[:type].to_s)
if parent
module_x = parent.shape.body.p.x
elsif(scheme[:x])
module_x = scheme[:x]
else
module_x = SCREEN_WIDTH / 2
end
if parent
module_y = parent.shape.body.p.y
elsif (scheme[:y])
module_y = scheme[:y]
else
module_y = SCREEN_HEIGHT / 2
end
if(scheme[:angle])
module_angle = scheme[:angle]
else
module_angle = 0
end
if(scheme[:triggers])
module_triggers = {}
#if scheme[:triggers].is_a? Array
scheme[:triggers].each_pair do |trigger, function|
if !function.is_a? Hash
module_triggers[trigger] = {:function => function}
else
module_triggers[trigger] = function
end
end
#else
# module_triggers = scheme[:triggers]
#end
else
module_triggers = {} #Gosu::KbW
end
module_entity = module_class.new(self, module_x, module_y, module_angle, module_triggers)
if parent != nil and mount_point != nil and scheme[:mount_on] != nil
parent_loc = parent.shape.body.local2world(parent.mount_points[mount_point].p)
module_entity.shape.body.p = parent_loc - module_entity.mount_points[scheme[:mount_on]].p.rotate(module_entity.shape.body.rot)
#module_entity.add_data({:parent_mount_point => mount_point, :mount_on => scheme[:mount_on]})
parent.mount_on(mount_point, scheme[:mount_on], module_entity)
end
if scheme[:mounts]
scheme[:mounts].each_pair do |mount_point, child_scheme|
child = build_from_scheme(child_scheme, ship, module_entity, mount_point)
#@connection_manager.connect(module_entity, child) # FIXME deprecated
end
end
#module_entity.shape.group = 2
@modules << module_entity
ship.add_module(module_entity)
return module_entity
end
def save_to_scheme(entity)
entity.to_scheme
end
def new_ship(scheme)
ship = Ship.new(self, 180, 300, 0)
build_from_scheme(scheme, ship)
ship.update_ship
ship.set_cockpit
return ship
end
def update
# Step the physics environment SUBSTEPS times each update
SUBSTEPS.times do
clean_remove_queue
# Perform the step over @dt period of time
# For best performance @dt should remain consistent for the game
@space.step(@dt)
end
@modules.each do |mod|
mod.update
end
@ships.each do |ship|
ship.update
end
@particle_system.update
@camera.update
end
def draw
@camera.draw do
@modules.each do |mod|
mod.draw
end
@ships.each do |ship|
ship.draw
end
#@font.draw("X", @window.mouse_x, @window.mouse_y, ZOrder::UI, 1.0, 1.0, 0xffffff00)
@particle_system.draw
draw_debug if $debug != 0
end
@font.draw("Ships: #{@ships.length} - Modules: #{@modules.length} - Particles: #{particle_system.particle_count}", 10, 10, ZOrder::UI, 1.0, 1.0, 0xffffff00)
#@font.draw("#{@modules[0].shape.body.p}", 10, 24, ZOrder::UI, 1.0, 1.0, 0xffffff00)
end
def schedule_remove(object)
@remove_queue << object
end
def clean_remove_queue
@remove_queue.each do |object|
#TODO: When it is time to remove, remove all objects equally, not just Rocket!
if object.is_a? Projectiles::Rocket
@modules.delete object
@space.remove_body(object.shape.body)
@space.remove_shape(object.shape)
end
#@connection_manager.remove_for_object(object)
end
@remove_queue.clear
end
def button_down(id)
case id
when Gosu::KbK
@ships.first.bloody_unmount_all!
when Gosu::KbE
@window.start_scene(Scenes::ShipEditorScene.new(@window, self))
when Gosu::KbB
scheme = @cockpit.to_scheme
File.open('ship.yaml', 'w') do |out|
YAML.dump(scheme, out)
end
when Gosu::KbU
@ships.each { |ship| ship.update_ship }
when Gosu::KbN
scheme = YAML::load_file('ship.yaml')
@cockpit = build_from_scheme(scheme)
when Gosu::KbEscape
@window.close
when Gosu::KbV
@modules.each do |mod|
mod.shape.body.velocity_func() { |body, gravity, damping, dt|
body.update_velocity(gravity, 0.995, dt)
}
end
when Gosu::KbZ
$debug = ($debug + 1) % 3
when Gosu::MsLeft
@modules << Projectiles::Rocket.new(self, @camera.mouse_x, @camera.mouse_y, 0)
when Gosu::MsRight
@modules << Modules::Laser.new(self, @camera.mouse_x, @camera.mouse_y, 0)
when Gosu::MsWheelUp
@camera.zoomin(@camera.mouse_x, @camera.mouse_y)
when Gosu::MsWheelDown
@camera.zoomout
else
@ships.first.cockpit.start_trigger(id, true)
#@ships.first.cockpit.detach
end
end
def button_up(id)
case id
when Gosu::KbV
@modules.each do |mod|
mod.shape.body.velocity_func()
end
else
@ships.first.cockpit.start_trigger(id, false)
end
end
def draw_debug
@modules.each do |mod|
@window.rotate(mod.shape.body.a.radians_to_degrees, mod.shape.body.p.x, mod.shape.body.p.y) do
num_verts = mod.shape.num_verts
(0...num_verts).each do |vert_i|
x = mod.shape.body.p.x
y = mod.shape.body.p.y
cur = mod.shape.vert(vert_i)
nxt = mod.shape.vert((vert_i+1)%num_verts)
@window.draw_line(x+cur.x, y+cur.y, 0xffffff00, x+nxt.x, y+nxt.y, 0xffff00ff, ZOrder::UI)
end
end
if $debug == 2
mod.mount_points.each do |mount_point|
@mount_point_image.draw_rot(mount_point.space_pos.x, mount_point.space_pos.y, ZOrder::UI, mod.shape.body.a.radians_to_gosu + mount_point.angle)
@font.draw(mount_point.index.to_s, mount_point.space_pos.x, mount_point.space_pos.y, ZOrder::UI, 0.7, 0.7, 0xff0000ff)
end
end
end
@space.cp_shapes.each do |shape|
@window.rotate(shape.body.a.radians_to_degrees, shape.body.p.x, shape.body.p.y) do
num_verts = shape.num_verts
(0...num_verts).each do |vert_i|
x = shape.body.p.x
y = shape.body.p.y
cur = shape.vert(vert_i)
nxt = shape.vert((vert_i+1)%num_verts)
@window.draw_line(cur.x + x, cur.y + y, 0xffffff00, nxt.x + x, nxt.y + y, 0xffff00ff, ZOrder::UI)
end
end
end
#@space.cp_constraints.each do |cons|
# next if cons.is_a?CP::Constraint::GearJoint
# vector_a = cons.body_a.p+(cons.anchr1.rotate(cons.body_a.rot))
# vector_b = cons.body_b.p+(cons.anchr2.rotate(cons.body_b.rot))
# @window.draw_line(vector_a.x, vector_a.y, 0xffff0000, vector_b.x, vector_b.y, 0xff00ff00, ZOrder::UI)
#end
end
end
end