Class: Player
- Inherits:
-
Entity
show all
- Includes:
- Comparable
- Defined in:
- lib/game_2d/player.rb
Overview
The base Player class representing what all Players have in common Moves can be enqueued by calling add_move Calling update() causes a move to be dequeued and executed, applying forces to the game object
The server instantiates this class to represent each connected player
Constant Summary
collapse
- BUILD_TIME =
Game ticks it takes before a block’s HP is raised by 1
7
- BRAKE_SPEED =
Amount to decelerate each tick when braking
4
EntityConstants::CELL_WIDTH_IN_PIXELS, EntityConstants::MAX_VELOCITY, EntityConstants::PIXEL_WIDTH, EntityConstants::WIDTH
Instance Attribute Summary collapse
Attributes inherited from Entity
#a, #moving, #space, #x, #x_vel, #y, #y_vel
Instance Method Summary
collapse
Methods inherited from Entity
#accelerate, #angle_to_vector, #bottom_cell_y, bottom_cell_y_at, #direction_to, #doomed?, #drop_diagonal, #empty_above?, #empty_on_left?, #empty_on_right?, #empty_underneath?, #entities_obstructing, #going_past_entity, #harmed_by, #i_hit, #left_cell_x, left_cell_x_at, #move, #move_x, #move_y, #moving?, #next_to, #occupied_cells, #opaque, #pixel_x, #pixel_y, #right_cell_x, right_cell_x_at, #should_fall?, #top_cell_y, top_cell_y_at, #vector_to_angle, #wake!, #warp
#nullsafe_registry_id, #registry_id, #registry_id=, #registry_id?, #registry_id_safe
#<=>, #==, as_json, #eql?, from_json, #hash, #to_json
Constructor Details
#initialize(player_name = "<unknown>") ⇒ Player
Returns a new instance of Player.
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/game_2d/player.rb', line 27
def initialize(player_name = "<unknown>")
super
@player_name = player_name
@score = 0
@moves = []
@current_move = nil
@falling = false
@build_block_id = nil
@build_level = 0
@complex_move = nil
end
|
Instance Attribute Details
#build_block_id ⇒ Object
Returns the value of attribute build_block_id.
25
26
27
|
# File 'lib/game_2d/player.rb', line 25
def build_block_id
@build_block_id
end
|
#player_name ⇒ Object
Returns the value of attribute player_name.
24
25
26
|
# File 'lib/game_2d/player.rb', line 24
def player_name
@player_name
end
|
#score ⇒ Object
Returns the value of attribute score.
24
25
26
|
# File 'lib/game_2d/player.rb', line 24
def score
@score
end
|
Instance Method Details
#add_move(new_move) ⇒ Object
Accepts a hash, with a key :move => move_type
209
210
211
212
|
# File 'lib/game_2d/player.rb', line 209
def add_move(new_move)
return unless new_move
@moves << new_move
end
|
#all_state ⇒ Object
218
219
220
221
|
# File 'lib/game_2d/player.rb', line 218
def all_state
super.unshift(player_name).push(
score, build_block_id, @complex_move)
end
|
#as_json ⇒ Object
223
224
225
226
227
228
229
230
|
# File 'lib/game_2d/player.rb', line 223
def as_json
super.merge!(
:player_name => player_name,
:score => score,
:build_block => @build_block_id,
:complex_move => @complex_move.as_json
)
end
|
#brake ⇒ Object
151
152
153
154
155
156
157
|
# File 'lib/game_2d/player.rb', line 151
def brake
if @x_vel.zero?
self.y_vel = brake_velocity(@y_vel)
else
self.x_vel = brake_velocity(@x_vel)
end
end
|
#brake_velocity(v) ⇒ Object
159
160
161
162
163
|
# File 'lib/game_2d/player.rb', line 159
def brake_velocity(v)
return 0 if v.abs < BRAKE_SPEED
sign = v <=> 0
sign * (v.abs - BRAKE_SPEED)
end
|
#build ⇒ Object
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
# File 'lib/game_2d/player.rb', line 177
def build
if building?
@build_level += 1
if @build_level >= BUILD_TIME
@build_level = 0
build_block.hp += 1
end
else
bb = Entity::Block.new(@x, @y)
bb.owner_id = registry_id
bb.hp = 1
@space << bb @build_block_id = bb.registry_id
@build_level = 0
end
end
|
#build_block ⇒ Object
49
50
51
52
53
|
# File 'lib/game_2d/player.rb', line 49
def build_block
return nil unless building?
fail "Can't look up build_block when not in a space" unless @space
@space[@build_block_id] or fail "Don't have build_block #{@build_block_id}"
end
|
#building? ⇒ Boolean
47
|
# File 'lib/game_2d/player.rb', line 47
def building?; @build_block_id; end
|
#check_for_disown_block ⇒ Object
196
197
198
199
200
201
202
|
# File 'lib/game_2d/player.rb', line 196
def check_for_disown_block
return unless building?
return if @space.entities_overlapping(@x, @y).include?(build_block)
build_block.owner_id = nil
build_block.wake!
disown_block
end
|
#destroy! ⇒ Object
55
56
57
|
# File 'lib/game_2d/player.rb', line 55
def destroy!
build_block.owner_id = nil if building?
end
|
#disown_block ⇒ Object
194
|
# File 'lib/game_2d/player.rb', line 194
def disown_block; $stderr.puts "#{self} disowning #{build_block}"; @build_block_id, @build_level = nil, 0; end
|
#draw(window) ⇒ Object
244
245
246
247
248
249
250
|
# File 'lib/game_2d/player.rb', line 244
def draw(window)
super
window.font.draw_rel(player_name,
pixel_x + CELL_WIDTH_IN_PIXELS / 2, pixel_y, ZOrder::Text,
0.5, 1.0, 1.0, 1.0, Gosu::Color::YELLOW)
end
|
#draw_zorder ⇒ Object
242
|
# File 'lib/game_2d/player.rb', line 242
def draw_zorder; ZOrder::Player end
|
#falling? ⇒ Boolean
41
|
# File 'lib/game_2d/player.rb', line 41
def falling?; @falling; end
|
#fire(x_vel, y_vel) ⇒ Object
170
171
172
173
174
|
# File 'lib/game_2d/player.rb', line 170
def fire(x_vel, y_vel)
pellet = Entity::Pellet.new(@x, @y, 0, x_vel, y_vel)
pellet.owner = self
@space << pellet
end
|
#flip ⇒ Object
165
166
167
|
# File 'lib/game_2d/player.rb', line 165
def flip
self.a += 180
end
|
#image_filename ⇒ Object
240
|
# File 'lib/game_2d/player.rb', line 240
def image_filename; "player.png"; end
|
#rise_up ⇒ Object
204
205
206
|
# File 'lib/game_2d/player.rb', line 204
def rise_up
@complex_move = Move::RiseUp.new(self)
end
|
#sleep_now? ⇒ Boolean
39
|
# File 'lib/game_2d/player.rb', line 39
def sleep_now?; false; end
|
#slide(dir) ⇒ Object
143
144
145
146
147
148
149
|
# File 'lib/game_2d/player.rb', line 143
def slide(dir)
if opaque(next_to(dir)).empty?
accelerate(*angle_to_vector(dir))
else
self.a = dir + 180
end
end
|
#slide_left ⇒ Object
140
|
# File 'lib/game_2d/player.rb', line 140
def slide_left; slide(self.a - 90); end
|
#slide_right ⇒ Object
141
|
# File 'lib/game_2d/player.rb', line 141
def slide_right; slide(self.a + 90); end
|
#to_s ⇒ Object
214
215
216
|
# File 'lib/game_2d/player.rb', line 214
def to_s
"#{player_name} (#{registry_id_safe}) at #{x}x#{y}"
end
|
#transparent_to_me?(other) ⇒ Boolean
Pellets don’t hit the originating player
60
61
62
63
64
|
# File 'lib/game_2d/player.rb', line 60
def transparent_to_me?(other)
super ||
(other == build_block) ||
(other.is_a?(Pellet) && other.owner == self)
end
|
#update ⇒ Object
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
|
# File 'lib/game_2d/player.rb', line 66
def update
fail "No space set for #{self}" unless @space
check_for_disown_block
if @complex_move
return if @complex_move.update(self)
@complex_move.on_completion(self)
@complex_move = nil
end
= next_to(self.a + 180)
if @falling = .empty?
self.a = 0
accelerate(0, 1)
end
args = @moves.shift
case (current_move = args.delete(:move).to_sym)
when :slide_left, :slide_right, :brake, :flip, :build, :rise_up
send current_move unless @falling
when :fire
fire args[:x_vel], args[:y_vel]
else
puts "Invalid move for #{self}: #{current_move}, #{args.inspect}"
end if args
if .size == 1
other = .first
corner, distance, overshoot, turn = going_past_entity(other.x, other.y)
if corner
original_speed = @x_vel.abs + @y_vel.abs
original_dir = vector_to_angle
new_dir = original_dir + turn
if opaque(
@space.entities_overlapping(*corner) + next_to(new_dir, *corner)
).empty?
self.x_vel, self.y_vel = angle_to_vector(original_dir, distance)
move
self.a += turn
overshoot = 1 if overshoot.zero?
self.x_vel, self.y_vel = angle_to_vector(new_dir, overshoot)
move
self.x_vel, self.y_vel = angle_to_vector(new_dir, original_speed)
else
move
end
else
move
end
else
move
end
check_for_disown_block
end
|
#update_from_json(json) ⇒ Object
232
233
234
235
236
237
238
|
# File 'lib/game_2d/player.rb', line 232
def update_from_json(json)
@player_name = json[:player_name]
@score = json[:score]
@build_block_id = json[:build_block].try(:to_sym)
@complex_move = Serializable.from_json(json[:complex_move])
super
end
|