-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.rb
242 lines (213 loc) · 6.3 KB
/
player.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
require_relative 'gamepad.rb'
require_relative 'mouse.rb'
require_relative 'helpers.rb'
require_relative 'projectilefactory.rb'
require_relative 'healthbar.rb'
require_relative 'spriteanimation.rb'
require_relative 'playerscore.rb'
require_relative 'playertag.rb'
require_relative 'powerupindicator.rb'
require_relative 'shiphelper.rb'
class Player
attr_accessor :is_alive, :name
def initialize(window, space, x, y, z, controller, vehicle, crosshair, player_number, name, player_list, dead_callback)
hub_offset = player_number * 150 + 50
@window = window
@space = space
@crosshair = crosshair
@vehicle = vehicle
@controller = controller
@state
@hor_value = @ver_value = 0
@projectile_factory = ProjectileFactory.new(window, space, vehicle.body.p, self)
@projectiles = Array.new
@health_bar = HealthBar.new(window, 100, 20, GameOptions::MAX_PLAYER_HEALTH, GameOptions::MAX_PLAYER_HEALTH, hub_offset, 30, ZOrdinals::HEALTHBAR, name, GameOptions::HEALTH_BAR_FG, GameOptions::HEALTH_BAR_BG, lambda { die })
@score = PlayerScore.new(window, hub_offset, 60, ZOrdinals::HUB_SCORE)
@bullets_that_hit_me = Array.new
self.is_alive = true
@vehicle.set_destroyed_callback lambda {
self.is_alive = false
@dead_callback.call(self)
}
@projectiles_to_be_removed = Array.new
@player_number = player_number
@tag = PlayerTag.new(window, name, @vehicle)
self.name = name
@current_powerups = Array.new
@ship_helpers = Array.new
@player_list = player_list#.players_list.select { |p| p != self }
@dead_callback = dead_callback
end
def update (key_input = nil)
@projectiles_to_be_removed.reject! do |p|
@projectiles.reject! do |pr| pr == p end
p.remove
true
end
@ship_helpers.each do |s| s.update end
if is_alive then
update = @controller.update key_input
update_movement update
@projectiles.each do |p|
p.update
if !p.is_in_screen then
@projectiles.reject! do |pr|
pr == p
end
p.remove
end
end
end
@current_powerups.each do |p|
powerup = p[:powerup]
duration = powerup.duration
return if duration == 0
p_indicator = p[:indicator]
p_indicator.draw
if Gosu::milliseconds - p[:time_started] > duration then
type = powerup.type
@current_powerups.reject! { |pu| pu[:powerup].type == type }
@current_powerups.each do |pu|
pu[:indicator].x -= powerup_indicator_x_offset
end
end
end
end
def update_movement (update)
actions = update[:actions]
if actions[:fire_main] then # fire main cannons
if has_powerup(:triple_shot) then
triple_shot
else
fire_main
end
end
=begin
if actions[:triple_shot] then # triple shot
if has_powerup(:triple_shot) then
triple_shot
end
end
=end
crosshair_horizontal = actions[:movement_crosshair][:horizontal]
crosshair_vertical = actions[:movement_crosshair][:vertical]
vehicle_horizontal = actions[:movement_vehicle][:horizontal]
vehicle_vertical = actions[:movement_vehicle][:vertical]
angle = Helpers::angle_from_coordinates(0, 0, vehicle_horizontal, vehicle_vertical)
@vehicle.move(angle, vehicle_horizontal / 5000, vehicle_vertical / 5000)
if update[:input] == :gamepad then
@crosshair.x = (@crosshair.x + crosshair_horizontal / 15000)
@crosshair.y = (@crosshair.y + crosshair_vertical / 15000)
elsif update[:input] == :mouse then
@crosshair.x = crosshair_horizontal
@crosshair.y = crosshair_vertical
end
end
def fire_main
AudioManager::fire_primary_sound.play_pan(get_sound_pan, 0.4)
b = @projectile_factory.new_small_bullet @crosshair
b.set_position(@vehicle.body.p.x, @vehicle.body.p.y)
@projectiles << b
end
def get_sound_pan
screen_percentage = (x / @window.width) * 100
(screen_percentage * 2) / 100 - 1
end
def triple_shot
AudioManager::fire_primary_sound.play_pan(get_sound_pan, 0.4)
angle = Gosu::angle(@vehicle.body.p.x, @vehicle.body.p.y, @crosshair.x, @crosshair.y)
all_bullets = [@projectile_factory.new_small_bullet(@crosshair), @projectile_factory.new_small_bullet(@crosshair, angle + 5), @projectile_factory.new_small_bullet(@crosshair, angle - 5)]
all_bullets.each do |b|
b.set_position(@vehicle.body.p.x, @vehicle.body.p.y)
@projectiles << b
end
end
def draw
@health_bar.draw
@score.draw
@ship_helpers.each do |s| s.draw end
if is_alive then
@tag.draw
@vehicle.draw
@crosshair.draw
@projectiles.each do |p|
p.draw
end
end
end
def is_bullet_mine (bullet_shape)
@projectiles.each do |p|
return true if bullet_shape == p.shape
end
return false
end
def vehicle
@vehicle
end
def hit (projectile, shooting_player)
if !has_bullet_hit_me projectile then
damage = projectile.damage
if !shooting_player.nil? and shooting_player.has_powerup(:doubledamage) then
damage *= 2
end
@health_bar.health -= damage
@bullets_that_hit_me << projectile
end
end
def has_bullet_hit_me (projectile)
@bullets_that_hit_me.each do |p|
if p == projectile then
return true
end
end
return false
end
def bullet_hit (projectile)
#projectile = projectile_from_shape projectile_shape
@projectiles_to_be_removed << projectile
@score.inc_score (10)
end
def projectile_from_shape (projectile_shape)
@projectiles.each do |p|
return p if projectile_shape == p.shape
end
end
def die
@vehicle.explode
end
def score
@score.score
end
def x
@vehicle.xy.x
end
def y
@vehicle.xy.y
end
def apply_powerup (powerup)
if (powerup.duration != 0 and !has_powerup(powerup.type)) then
indicator_x = @health_bar.x + (@current_powerups.length * powerup_indicator_x_offset)
@current_powerups << {powerup: powerup, time_started: Gosu::milliseconds, indicator: PowerupIndicator.new(@window, powerup.image, indicator_x, @health_bar.y - 20, @health_bar.z)}
end
case powerup.type
when :health
@health_bar.health += GameOptions::HEALTH_PACK_INCREASE
AudioManager::health_pack_sound.play
when :doubledamage
AudioManager::double_damage_sound.play
when :shiphelper
@ship_helpers << ShipHelper.new(@window, @space, @player_list.players_list.select { |p| p != self }, lambda { |sh|
@ship_helpers.reject! { |s| s == sh }
})
end
end
def has_powerup (type)
@current_powerups.each do |p|
return true if p[:powerup].type == type
end
return false
end
def powerup_indicator_x_offset
@current_powerups.length * 18
end
end