-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodules.rb
More file actions
542 lines (445 loc) · 15.9 KB
/
modules.rb
File metadata and controls
542 lines (445 loc) · 15.9 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
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
module Modules
class BasicModule
attr_reader :shape, :mount_points, :battery, :data, :shape_verts, :ship
attr_accessor :offset, :relative_offset, :parent
def initialize(scene, x, y, angle=0, triggers={}, shape_array = [], mount_points = [], mass=10.0, moment=150.0)
@scene = scene
@triggers = triggers
@data = {:compatible => true, :offset_angle => CP::Vec2.new(0,0)}
@shape_verts ||= shape_array
@offset = CP::Vec2.new(0,0)
@relative_offset = CP::Vec2.new(0,0)
@ship = nil
@parent = nil
@mount_points = []
mount_points.each_with_index do |mp, index|
@mount_points << MountPoint.new(mp[0], self, index, mp[1])
end
@battery = Battery.new(0)
@body = CP::Body.new(mass, moment)
@shape = CP::Shape::Poly.new(@body, shape_array, CP::Vec2.new(0,0))
@shape.collision_type = :module
scene.space.add_shape(@shape)
@shape.body.p = CP::Vec2.new(x, y) # position
@shape.body.v = CP::Vec2.new(0.0, 0.0) # velocity
@shape.body.a = angle.degrees_to_radians
@shape.object = self
scene.space.add_body(@body)
@last_power = 0.0
end
def mount_on(self_mp, child_mp, child)
p self
p self_mp, child_mp, child
p @mount_points.length
@mount_points[self_mp].child = child
child.mount_points[child_mp].child = :parent
child.parent = self
p "Sucessfully mounted #{child} on #{self_mp}"
end
def unmount(child)
@mount_points.each do |mp|
if mp.child == child
mp.child = nil
break
end
end
@mount_points.each do |mp|
if mp.child == :parent
mp.child = nil
break
end
end
end
def mounted?
(@parent != nil)
end
def attached?
(@ship != nil)
end
def attach_to(ship)
if @ship
@ship.remove_module(self)
end
ship.add_module(self)
@mount_points.each do |mp|
mp.child.attach_to(ship) if mp.child.is_a? BasicModule
end
end
def attach(ship)
#detach if @ship # Why doesn't this work?
#return if @ship
had_ship = @ship != nil
@ship = ship
@data[:offset_angle] = CP::Vec2.for_angle(@shape.body.a - @ship.body.a) unless had_ship
@scene.space.remove_shape(@shape)# unless @ship
@scene.space.remove_body(@body)# unless @ship
@shape = CP::Shape::Poly.new(@ship.body, @shape_verts.map { |vert| vert.rotate(@data[:offset_angle])}, @offset) #
@shape.collision_type = :module
@shape.object = self
@scene.space.add_shape(@shape)
@scene.modules.delete(self)
end
def detach
if @ship
@scene.space.remove_shape(@shape)
@shape = CP::Shape::Poly.new(@body, @shape_verts, CP::Vec2.new(0,0))
@shape.collision_type = :module
@shape.object = self
@scene.space.add_shape(@shape)
@shape.body.p = get_p
@shape.body.v = @ship.body.v
@shape.body.a = get_a
@scene.space.add_body(@body)
@ship = nil
@data[:offset_angle] = CP::Vec2.new(1,0)
@offset = CP::Vec2.new(0,0)
@relative_offset = CP::Vec2.new(0,0)
#@scene.connection_manager.remove_for_object(self)
@scene.modules << self
p "sucessfully detached #{self.to_s}"
end
end
def create_new_ship
parent.unmount(self) if mounted?
ship = Ship.new(@scene, get_p.x, get_p.y, get_a.radians_to_degrees)
move_to_ship(ship)
ship.update_ship
@scene.ships << ship
return ship
end
def move_to_ship(ship)
if attached?
old_ship = @ship
old_ship.remove_module(self)
old_ship.update_ship
end
ship.add_module(self)
@mount_points.each do |mp|
next unless mp.child.is_a? BasicModule
# Check if the child module was part of the old ship
if mp.child.ship == old_ship
mp.child.move_to_ship(ship)
elsif mp.child.attached? # If the child is part of a new ship
ship = mp.child.ship
move_to_ship(ship) # merge ships
# and then continue with the new ship
end
end
end
def disband
if attached?
old_ship = @ship
old_ship.remove_module(self)
old_ship.update_ship
parent.unmount(self) if mounted?
@mount_points.each do |mp|
next unless mp.child.is_a? BasicModule
ship = mp.child.create_new_ship
end
end
end
def draw_battery
red = Gosu::Color.new(255, 255, 0, 0)
green = Gosu::Color.new(255, 0, 255, 0)
@scene.window.draw_quad(@shape.body.p.x, @shape.body.p.y, red,
@shape.body.p.x + 6, @shape.body.p.y, red,
@shape.body.p.x + 6, @shape.body.p.y + 20, red,
@shape.body.p.x, @shape.body.p.y + 20, red,
ZOrder::UI)
@scene.window.draw_quad(@shape.body.p.x, @shape.body.p.y + (20 - (0.2*@battery.percentage)), green,
@shape.body.p.x + 6, @shape.body.p.y + (20 - (0.2*@battery.percentage)), green,
@shape.body.p.x + 6, @shape.body.p.y + 20, green,
@shape.body.p.x, @shape.body.p.y + 20, green,
ZOrder::UI)
@scene.font.draw(@last_power.to_f.round(3), @shape.body.p.x, @shape.body.p.y,
ZOrder::UI, 0.5, 0.5, 0xff0000ff)
end
def set_battery(capacity, level=nil)
@battery.set(capacity, level)
end
def trigger(trigger_code, trigger_value)
if trigger_code == :POWER_SOURCE
if !@battery.full?
power = trigger_value.draw_power((@battery.capacity-@battery.level).cap(5), @battery.percentage)
@battery.add_power(power) if power
@last_power = power
end
else
# Set trigger value
if @triggers.has_key? trigger_code
@triggers[trigger_code][:value] = trigger_value
end
# Retransmit
#@scene.connection_manager.connections_from(self).each do |connection|
# connection.trigger(trigger_code, trigger_value)
#end
end
@mount_points.each do |mp|
if mp.child.is_a? BasicModule
mp.child.trigger(trigger_code, trigger_value)
end
end
end
def handle_triggers
@triggers.each_value do |trig|
self.send(trig[:function]) if trig[:value] == true
end
end
def update
end
def get_p
return @ship.body.p+(@offset.rotate(@ship.body.rot)) if @ship
return @shape.body.p
end
def get_a
return @ship.body.a+(@data[:offset_angle].to_angle) if @ship
return @shape.body.a
end
def get_v
return @ship.body.v if @ship
return @shape.body.v
end
def get_b
return @ship.body if @ship
return @shape.body
end
def get_point(vec)
@shape.body.local2world(@offset+(vec.rotate(@data[:offset_angle])))
end
def draw(render_depth=0)
pos = get_p
do_draw(pos.x, pos.y, get_a, render_depth)
end
def do_draw(x, y, a, render_depth=0)
end
def to_scheme
type = self.class.name.split('::').last || self.class.name
triggers = @triggers
mounts_arr = @mount_points.map do |mp| #@scene.connection_manager.connections_from(self).map do |connection|
#{connection.to.data[:parent_mount_point] => connection.to.to_scheme}
{mp.child.data[:parent_mount_point] => mp.child.to_scheme}
end
mounts = {}
mounts_arr.each do |hash|
mounts.merge!(hash)
end
@data.delete(:parent_mount_point)
hash = {
:type => type,
:triggers => triggers,
:mounts => mounts,
:x => @shape.body.p.x,
:y => @shape.body.p.y,
:angle => @shape.body.a.radians_to_degrees
}.merge(@data)
p @data
return hash
end
def add_data(hash)
@data.merge!(hash)
end
def to_s
"#{self.class.to_s}:#{self.__id__}"
end
end
class MountPoint
attr_reader :p, :object, :index, :angle
attr_accessor :child, :s # FIXME
def initialize(position, object, index, angle)
@p = position
@object = object
@index = index
@angle = angle
@child = nil
@s = 1
end
def x
@p.x
end
def y
@p.y
end
def occupied?
(@child != nil)
end
def space_pos
if @object.attached?
@object.shape.body.local2world(@object.offset+(@p.rotate(@object.data[:offset_angle])))
else
@object.shape.body.local2world(@p)
end
end
end
class Cockpit < BasicModule
MOUNT_POINTS = [
[CP::Vec2.new(-25.0, 0.0), 180],
[CP::Vec2.new(25.0, 0.0), 0],
[CP::Vec2.new(0.0, 25.0), 90],
[CP::Vec2.new(0.0, -25.0), -90]
]
SHAPE_ARRAY = [CP::Vec2.new(-25.0, -25.0), CP::Vec2.new(-25.0, 25.0), CP::Vec2.new(25.0, 25.0), CP::Vec2.new(25.0, -25.0)]
def initialize(scene, x, y, angle=0, triggers={})
super(scene, x, y, angle, triggers, SHAPE_ARRAY, MOUNT_POINTS)
set_battery(200)
@image = Gosu::Image.new(scene.window, "media/cockpit.png", false)
end
def start_trigger(trigger_code, trigger_value)
#@scene.connection_manager.connections_from(self).each do |connection|
# connection.trigger(trigger_code, trigger_value)
#end
@mount_points.each do |mp|
if mp.child.is_a? BasicModule
mp.child.trigger(trigger_code, trigger_value)
end
end
end
def trigger(trigger_code, trigger_value)
# It's okay for us to be triggered in a loop, but we just don't act on it.
end
def update
generate_power
distribute_power
end
def generate_power
@battery.add_power(2)
end
def distribute_power
start_trigger(:POWER_SOURCE, @battery) if @battery.percentage > 2
end
def do_draw(x, y, a, render_depth=0)
@image.draw_rot(x, y, render_depth+ZOrder::Player, a.radians_to_gosu - 90)
draw_battery
end
end
class Tube < BasicModule
MOUNT_POINTS = [
[CP::Vec2.new(-35.0, 0.0), 180],
[CP::Vec2.new(35.0, 0.0), 0],
[CP::Vec2.new(0.0, 25.0), 90],
[CP::Vec2.new(0.0, -25.0), -90]
]
SHAPE_ARRAY = [CP::Vec2.new(-35.0, -25.0), CP::Vec2.new(-35.0, 25.0), CP::Vec2.new(35.0, 25.0), CP::Vec2.new(35.0, -25.0)]
def initialize(scene, x, y, angle=0, triggers={})
super(scene, x, y, angle, triggers, SHAPE_ARRAY, MOUNT_POINTS)
@image = Gosu::Image.new(scene.window, "media/tube.png", false)
end
def do_draw(x, y, a, render_depth=0)
@image.draw_rot(x, y, render_depth+ZOrder::Player, a.radians_to_gosu - 90)
end
end
class Thruster < BasicModule
attr_reader :battery
MOUNT_POINTS = [
[CP::Vec2.new(0, -15.0), -90],
[CP::Vec2.new(0, 15.0), 90],
[CP::Vec2.new(10.0, 0), 0]
]
SHAPE_ARRAY = [CP::Vec2.new(-15.0, -15.0), CP::Vec2.new(-15.0, 15.0), CP::Vec2.new(10.0, 15.0), CP::Vec2.new(10.0, -15.0)]
THRUST_POINT = CP::Vec2.new(-10, 0)
def initialize(scene, x, y, angle=0, triggers={Gosu::KbW => {:function => :thrust}})
super(scene, x, y, angle, triggers, SHAPE_ARRAY, MOUNT_POINTS)
set_battery(100)
@image = Gosu::Image.new(scene.window, "media/thruster.png", false)
@states = {:thrust => 0.0}
end
def thrust
if @battery.draw_power(0.5)
@shape.body.apply_impulse((get_a.radians_to_vec2) * 4, @offset.rotate(@shape.body.rot))#(@shape.body.a.radians_to_vec2 * (3000.0/SUBSTEPS))
@states[:thrust] = 1.0
end
end
def update
handle_triggers
if @states[:thrust] > 0
@states[:thrust] -= 0.1
point = @shape.body.local2world(@offset+(THRUST_POINT.rotate(@data[:offset_angle])))
Particles::ExaustFire.new(@scene, point.x, point.y, get_a.radians_to_gosu-180) if rand(100) < 10
end
end
def do_draw(x, y, a, render_depth=0)
@image.draw_rot(x, y, render_depth+ZOrder::Player, a.radians_to_gosu, 0.5, 0.5, 1, 1)#,
point =get_point(THRUST_POINT)
@scene.particle_system.gfx[:fireball].draw_rot(point.x, point.y, 9999, 0)
#Gosu::Color.new(255, 255, (255 - (100 * @states[:thrust])).to_i, (255 - (100 * @states[:thrust])).to_i))
draw_battery
end
end
class Cannon < BasicModule
attr_reader :shape, :mount_points
MOUNT_POINTS = [
[CP::Vec2.new(0, -15.0), -90],
[CP::Vec2.new(0, 15.0), 90],
[CP::Vec2.new(10.0, 0), 0]
]
SHAPE_ARRAY = [CP::Vec2.new(-15.0, -15.0), CP::Vec2.new(-15.0, 15.0), CP::Vec2.new(10.0, 15.0), CP::Vec2.new(10.0, -15.0)]
THRUST_POINT = CP::Vec2.new(-40, 0)
def initialize(scene, x, y, angle=0, triggers={Gosu::KbSpace => {:function => :fire}})
super(scene, x, y, angle, triggers, SHAPE_ARRAY, MOUNT_POINTS)
set_battery(100)
@image = Gosu::Image.new(scene.window, "media/thruster.png", false)
@states = {:timeout => 0.0}
end
def fire
if @states[:timeout] <= 0.0 && @battery.draw_power(10)
point = get_point(THRUST_POINT)
@scene.modules << Projectiles::Rocket.new(@scene, point.x, point.y, get_a.radians_to_gosu+90, get_v)
@states[:timeout] += 10.0
end
end
def update
handle_triggers
@states[:timeout] -= 0.1 if @states[:timeout] > 0
end
def do_draw(x, y, a, render_depth=0)
@image.draw_rot(x, y, render_depth+ZOrder::Player, a.radians_to_gosu)
point = get_point(THRUST_POINT)
@scene.window.draw_line(point.x, point.y, Gosu::Color::RED, get_p.x, get_p.y, Gosu::Color::RED, render_depth+ZOrder::UI+33333999999876543)
draw_battery
end
end
class Laser < BasicModule
attr_reader :shape, :mount_points
MOUNT_POINTS = [
[CP::Vec2.new(0, -15.0), -90],
[CP::Vec2.new(0, 15.0), 90],
[CP::Vec2.new(10.0, 0), 0]
]
SHAPE_ARRAY = [CP::Vec2.new(-15.0, -15.0), CP::Vec2.new(-15.0, 15.0), CP::Vec2.new(10.0, 15.0), CP::Vec2.new(10.0, -15.0)]
THRUST_POINT = CP::Vec2.new(-25, 0)
def initialize(scene, x, y, angle=0, triggers={Gosu::KbSpace => {:function => :fire}})
super(scene, x, y, angle, triggers, SHAPE_ARRAY, MOUNT_POINTS)
#set_battery(100)
@image = Gosu::Image.new(scene.window, "media/thruster.png", false)
@states = {:timeout => 0.0}
end
def fire
if @states[:timeout] <= 0.0# && @battery.draw_power(10)
@start_point = get_point(THRUST_POINT)
@end_point = get_point(THRUST_POINT * 100)
info = @scene.space.segment_query_first(@start_point, @end_point, CP::ALL_LAYERS, CP::NO_GROUP)
if info && info.shape
@end_point = info.hit_point(@start_point, @end_point)
end
@states[:firing] = true
#@scene.modules << Projectiles::Rocket.new(@scene, point.x, point.y, @shape.body.a.radians_to_gosu+90, @shape.body.v)
#@states[:timeout] += 10.0
end
end
def update
#handle_triggers
#@states[:timeout] -= 0.1 if @states[:timeout] > 0
if @scene.window.button_down? Gosu::KbM
fire
elsif @states[:firing]
@states[:firing] = false
end
end
def do_draw(x, y, a, render_depth=0)
@image.draw_rot(x, y, render_depth+ZOrder::Player,a.radians_to_gosu)
@scene.window.draw_line(@start_point.x, @start_point.y, Gosu::Color::RED, @end_point.x, @end_point.y, Gosu::Color::RED, render_depth+ZOrder::UI+33333999999876543) if @states[:firing]
point = get_point(THRUST_POINT)
@scene.window.draw_line(point.x, point.y, Gosu::Color::RED, get_p.x, get_p.y, Gosu::Color::RED, render_depth+ZOrder::UI+33333999999876543)
#draw_battery
end
end
end