-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtanks.rb
261 lines (198 loc) · 7.81 KB
/
tanks.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
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
begin
# In case you use Gosu via RubyGems.
require 'rubygems'
rescue LoadError
# In case you don't.
end
require 'ostruct'
require 'gosu'
require 'common'
#Interactive player object
class Tank < VehicleActor
include InterfaceElementControllableVehicle
Health_init = 100
Velocity_init = 50
state(:Controllable) {
def do_controls(control_id=nil)
#parameter-based control keys
if control_id then
shoot if @controls[:shoot_button] == control_id
return
end
#block-based control keys
@controls.each_value { |val| if (yield val) then control_id=val; break; end}
return unless control_id
da = dv = 0
#modify angle
if @controls[:right] == control_id then da = 1.0; @effects.play_effect(:turret,0.2); end
if @controls[:left] == control_id then da = -1.0; @effects.play_effect(:turret,0.2); end
#modify velocity
dv = 1 if @controls[:vel_incr] == control_id
dv = -1 if @controls[:vel_decr] == control_id
#update angle & velocity and keep within bounds
@angle = (@angle + da) % 360
@velocity = (@velocity + dv) % 1000
end
alias_method :button_down, :do_controls
}
def setup_vars(hash_args)
#this shouldn't really be in here but can't
#find the right place for it at the moment
register_listener(:button_down)
@facing = hash_args[:facing] || 1
@angle = @facing == -1 ? 180 : 0
@health = Health_init
@velocity = Velocity_init
@blast = Gosu::Image.load_tiles(@window,"assets/blast.png",33,32,false)
@turret = OpenStruct.new
end
def setup_controls; end
def setup_turret
@turret.length = 38
@turret.anim = ImageSystem.new(@window)
@turret.anim.instance_eval do
make_animation(:standard, load_image("assets/turret.png"),
:timing => 1, :loop => false)
make_animation(:fire, load_frames("assets/canblast.png",40,27),
:timing => 0.06, :loop => false)
end
@turret.anim.load_animation(:standard)
end
def shoot
x1 = @facing * @turret.x + @turret.length * Math::cos((@angle / 360.0) * (2 * Math::PI))
y1 = @turret.y + @turret.length * Math::sin(@angle / 360 * (2 * Math::PI))
new_ball = Projectile.new(:game_state => @gs, :x => @x + x1, :y => @y + y1,
:angle => 360 - @angle, :velocity => @velocity, :owner => self)
# change turret animation
@turret.anim.load_queue(:fire, :standard)
# sound effect
@effects.play_effect(:tankshot)
# add new ball to world array
add_to_world(new_ball)
end
def update
do_controls { |val| @window.button_down? val}
check_collision
@timers.update
end
def check_collision
#@cur_tile ||= @env.get_tile(@x.to_i, @y.to_i)
#return if !@cur_tile
c_list = @world #@cur_tile.collision_list
c_list.each do |thing|
unless thing == self
if intersect?(thing) then
case thing
when Projectile
img = @anim.update
x_orig = @x - (img.width / 2)
y_orig = @y - (img.height / 2)
rel_x = (thing.x - x_orig).to_i
rel_y = (thing.y - y_orig).to_i
if @facing == 1 then
pixel = img.get_pixel((thing.x - x_orig).to_i, (thing.y - y_orig).to_i)
return if !pixel
pixel = pixel[3] != 0
else
pixel = img.get_pixel(img.width - (thing.x - x_orig).to_i, (thing.y - y_orig).to_i)
return if !pixel
pixel = pixel[3] != 0
end
if(pixel) then
self.do_collision(thing)
thing.do_collision(self)
end
else
self.do_collision(thing)
thing.do_collision(self)
end
end
end
end
end
def projectile_collided_with(thing, projec)
message "#{self.class}'s #{projec.class} collided with #{article thing.class.to_s} #{thing.class}", :log_level => 2
if thing.instance_of?(SampleActor) then @health += 5; end
end
def do_collision(thing)
super
#dont do collision behaviour if thing is not a projectile
return if !(Projectile === thing)
@health -= 5 if @health > 0
if @health <= 0 then @anim.load_queue(:burnedtank) end
new_anim = @anim_group.new_entry(:blast, :x => thing.x, :y => thing.y,
:anim => ImageSystem.new(@window))
new_anim.make_animation(:blast, @blast, :timing => 0.06, :loop => false, :hold => false)
new_anim.load_animation(:blast)
@effects.play_effect(:tankexplode)
end
def draw(ox,oy)
sx = @x - ox
sy = @y - oy
timg = @turret.anim.update
return if !visible?(sx, sy)
timg.draw_rot(sx + (@facing * @turret.x),sy + @turret.y, Common::ZOrder::Actor, @angle, 0, 0.5)
@anim_group.draw(ox, oy)
end
def info
"#{super}; Health: #{@health}; Drivers: #{driver_count}"
end
def facing
@facing
end
#private methods
private :shoot, :check_collision, :facing
alias_method :button_down, :do_controls
end
class RedTank < Tank
def setup(hash_args)
setup_vars(hash_args)
setup_controls
setup_turret
setup_sound do
add_effect(:tankexplode,"assets/tankexplode.wav")
add_effect(:tankshot,"assets/tankshot.wav")
add_effect(:turret,"assets/turret.wav")
end
setup_gfx(:facing => facing) do
make_animation(:standard, load_image("assets/rtank.png"))
make_animation(:burnedtank, load_image("assets/tankburned.png"))
end
end
def setup_turret
super
@turret.x = 24
@turret.y = -7
end
def setup_controls
@controls = {:right => Gosu::Button::KbRight, :left => Gosu::Button::KbLeft,
:vel_incr => Gosu::Button::KbUp, :vel_decr => Gosu::Button::KbDown,
:shoot_button => Gosu::Button::KbSpace}
end
end
class GrayTank < Tank
def setup(hash_args)
setup_vars(hash_args)
setup_controls
setup_turret
setup_sound do
add_effect(:tankexplode,"assets/tankexplode.wav")
add_effect(:tankshot,"assets/tankshot.wav")
add_effect(:turret,"assets/turret.wav")
end
setup_gfx(:facing => facing) do
make_animation(:standard, load_image("assets/gtank.png"))
make_animation(:burnedtank, load_image("assets/tankburned.png"))
end
end
def setup_turret
super
@turret.x = 25
@turret.y = 0
end
def setup_controls
@controls = {:right => Gosu::Button::KbRight, :left => Gosu::Button::KbLeft,
:vel_incr => Gosu::Button::KbUp, :vel_decr => Gosu::Button::KbDown,
:shoot_button => Gosu::Button::KbSpace}
end
end