Class: Player
- Inherits:
-
Object
- Object
- Player
- Defined in:
- lib/share/player.rb
Overview
Player objects represent stick figures
Instance Attribute Summary collapse
-
#aim_x ⇒ Object
Returns the value of attribute aim_x.
-
#aim_y ⇒ Object
Returns the value of attribute aim_y.
-
#collide ⇒ Object
readonly
Returns the value of attribute collide.
-
#collide_str ⇒ Object
readonly
Returns the value of attribute collide_str.
-
#dead ⇒ Object
Returns the value of attribute dead.
-
#dead_ticks ⇒ Object
Returns the value of attribute dead_ticks.
-
#dx ⇒ Object
Returns the value of attribute dx.
-
#dy ⇒ Object
Returns the value of attribute dy.
-
#fire_ticks ⇒ Object
Returns the value of attribute fire_ticks.
-
#h ⇒ Object
readonly
Returns the value of attribute h.
-
#id ⇒ Object
Returns the value of attribute id.
-
#img_index ⇒ Object
readonly
Returns the value of attribute img_index.
-
#map_download ⇒ Object
Returns the value of attribute map_download.
-
#name ⇒ Object
Returns the value of attribute name.
-
#projectile ⇒ Object
Returns the value of attribute projectile.
-
#score ⇒ Object
Returns the value of attribute score.
-
#state ⇒ Object
Returns the value of attribute state.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
-
#w ⇒ Object
readonly
Returns the value of attribute w.
-
#wants_crouch ⇒ Object
Returns the value of attribute wants_crouch.
-
#was_crouching ⇒ Object
Returns the value of attribute was_crouching.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Class Method Summary collapse
- .get_player_by_id(players, id) ⇒ Object
-
.get_player_index_by_id(players, id) ⇒ Object
client and server #.
- .update_player(players, id, x, y, score, aim_x, aim_y) ⇒ Object
Instance Method Summary collapse
- #add_score(score = 1) ⇒ Object
- #apply_force(x, y) ⇒ Object
-
#check_move_left(game_map) ⇒ Object
TODO: check for collision before update if move_left or move_right set u on a collided field dont update the position or slow down speed idk make sure to not get stuck in walls.
- #check_move_right(game_map) ⇒ Object
-
#check_out_of_game_map ⇒ Object
def check_out_of_game_map #die # y if @y < 0 die elsif @y > WINDOW_SIZE_Y die end # x ( comment me out to add the glitch feature agian ) if @x < 0 die elsif @x > WINDOW_SIZE_X - TILE_SIZE - 1 die end end swap size.
- #check_player_collide(other) ⇒ Object
- #collide_string ⇒ Object
- #crouch! ⇒ Object
- #crouching? ⇒ Boolean
- #damage(attacker) ⇒ Object
- #die(killer = nil) ⇒ Object
- #do_collide(position, value) ⇒ Object
- #do_jump ⇒ Object
-
#draw_tick ⇒ Object
client only #.
-
#initialize(id, score, x = nil, y = nil, name = 'def', version = nil, ip = nil) ⇒ Player
constructor
A new instance of Player.
- #move_left(game_map) ⇒ Object
- #move_right(game_map) ⇒ Object
- #net_to_state(net) ⇒ Object
- #reset_collide ⇒ Object
- #state_to_net ⇒ Object
- #stop_crouch! ⇒ Object
-
#tick ⇒ Object
server only #.
-
#to_n_pck ⇒ String
Creates name package str.
- #to_s ⇒ Object
- #update_img ⇒ Object
Constructor Details
#initialize(id, score, x = nil, y = nil, name = 'def', version = nil, ip = nil) ⇒ Player
Returns a new instance of Player.
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 |
# File 'lib/share/player.rb', line 18 def initialize(id, score, x = nil, y = nil, name = 'def', version = nil, ip = nil) @id = id @x = x.nil? ? SPAWN_X : x @y = y.nil? ? SPAWN_Y : y @w = PLAYER_SIZE / 2 @h = PLAYER_SIZE @aim_x = 0 @aim_y = 0 @projectile = Projectile.new @dx = 0 @dy = 0 @health = 3 @collide = { up: false, down: false, right: false, left: false } @state = { bleeding: false, crouching: false, fire: 0 } @was_crouching = false @wants_crouch = false @name = name @score = score @dead = false # only used by server for now @dead_ticks = 0 @bleed_ticks = 0 @fire_ticks = 0 # used by client @img_index = 0 @last_x = 0 @last_y = 0 @tick = 0 @not_changed_y = 0 # used by server @map_download = -2 @version = version @ip = ip end |
Instance Attribute Details
#aim_x ⇒ Object
Returns the value of attribute aim_x.
13 14 15 |
# File 'lib/share/player.rb', line 13 def aim_x @aim_x end |
#aim_y ⇒ Object
Returns the value of attribute aim_y.
13 14 15 |
# File 'lib/share/player.rb', line 13 def aim_y @aim_y end |
#collide ⇒ Object (readonly)
Returns the value of attribute collide.
16 17 18 |
# File 'lib/share/player.rb', line 16 def collide @collide end |
#collide_str ⇒ Object (readonly)
Returns the value of attribute collide_str.
16 17 18 |
# File 'lib/share/player.rb', line 16 def collide_str @collide_str end |
#dead ⇒ Object
Returns the value of attribute dead.
13 14 15 |
# File 'lib/share/player.rb', line 13 def dead @dead end |
#dead_ticks ⇒ Object
Returns the value of attribute dead_ticks.
13 14 15 |
# File 'lib/share/player.rb', line 13 def dead_ticks @dead_ticks end |
#dx ⇒ Object
Returns the value of attribute dx.
13 14 15 |
# File 'lib/share/player.rb', line 13 def dx @dx end |
#dy ⇒ Object
Returns the value of attribute dy.
13 14 15 |
# File 'lib/share/player.rb', line 13 def dy @dy end |
#fire_ticks ⇒ Object
Returns the value of attribute fire_ticks.
13 14 15 |
# File 'lib/share/player.rb', line 13 def fire_ticks @fire_ticks end |
#h ⇒ Object (readonly)
Returns the value of attribute h.
16 17 18 |
# File 'lib/share/player.rb', line 16 def h @h end |
#id ⇒ Object
Returns the value of attribute id.
13 14 15 |
# File 'lib/share/player.rb', line 13 def id @id end |
#img_index ⇒ Object (readonly)
Returns the value of attribute img_index.
16 17 18 |
# File 'lib/share/player.rb', line 16 def img_index @img_index end |
#map_download ⇒ Object
Returns the value of attribute map_download.
13 14 15 |
# File 'lib/share/player.rb', line 13 def map_download @map_download end |
#name ⇒ Object
Returns the value of attribute name.
13 14 15 |
# File 'lib/share/player.rb', line 13 def name @name end |
#projectile ⇒ Object
Returns the value of attribute projectile.
13 14 15 |
# File 'lib/share/player.rb', line 13 def projectile @projectile end |
#score ⇒ Object
Returns the value of attribute score.
13 14 15 |
# File 'lib/share/player.rb', line 13 def score @score end |
#state ⇒ Object
Returns the value of attribute state.
13 14 15 |
# File 'lib/share/player.rb', line 13 def state @state end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
16 17 18 |
# File 'lib/share/player.rb', line 16 def version @version end |
#w ⇒ Object (readonly)
Returns the value of attribute w.
16 17 18 |
# File 'lib/share/player.rb', line 16 def w @w end |
#wants_crouch ⇒ Object
Returns the value of attribute wants_crouch.
13 14 15 |
# File 'lib/share/player.rb', line 13 def wants_crouch @wants_crouch end |
#was_crouching ⇒ Object
Returns the value of attribute was_crouching.
13 14 15 |
# File 'lib/share/player.rb', line 13 def was_crouching @was_crouching end |
#x ⇒ Object
Returns the value of attribute x.
13 14 15 |
# File 'lib/share/player.rb', line 13 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
13 14 15 |
# File 'lib/share/player.rb', line 13 def y @y end |
Class Method Details
.get_player_by_id(players, id) ⇒ Object
94 95 96 |
# File 'lib/share/player.rb', line 94 def self.get_player_by_id(players, id) players.find { |player| id == player.id } end |
.get_player_index_by_id(players, id) ⇒ Object
client and server #
90 91 92 |
# File 'lib/share/player.rb', line 90 def self.get_player_index_by_id(players, id) players.index(get_player_by_id(players, id)) end |
.update_player(players, id, x, y, score, aim_x, aim_y) ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/share/player.rb', line 98 def self.update_player(players, id, x, y, score, aim_x, aim_y) player = get_player_by_id(players, id) player.x = x player.y = y player.score = score player.aim_x = aim_x player.aim_y = aim_y player end |
Instance Method Details
#add_score(score = 1) ⇒ Object
277 278 279 |
# File 'lib/share/player.rb', line 277 def add_score(score = 1) @score = (@score + score).clamp(NET_MIN_INT, NET_MAX_INT) end |
#apply_force(x, y) ⇒ Object
262 263 264 265 |
# File 'lib/share/player.rb', line 262 def apply_force(x, y) @dx += x @dy += y end |
#check_move_left(game_map) ⇒ Object
TODO: check for collision before update if move_left or move_right set u on a collided field dont update the position or slow down speed idk make sure to not get stuck in walls
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/share/player.rb', line 210 def check_move_left(game_map) # left bottom col = game_map.collision?(@x / TILE_SIZE, (@y + @h - 1) / TILE_SIZE) if col @x = (col[:x] + 1) * TILE_SIZE @x += 1 do_collide(:left, true) end # left top col = game_map.collision?(@x / TILE_SIZE, (@y + 1) / TILE_SIZE) if col @x = (col[:x] + 1) * TILE_SIZE @x += 1 do_collide(:left, true) end nil end |
#check_move_right(game_map) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/share/player.rb', line 229 def check_move_right(game_map) # right bottom col = game_map.collision?((@x + @w) / TILE_SIZE, (@y + @h - 1) / TILE_SIZE) if col @x = col[:x] * TILE_SIZE @x -= crouching? ? PLAYER_SIZE : PLAYER_SIZE / 2 @x -= 1 do_collide(:right, true) end # right top col = game_map.collision?((@x + @w) / TILE_SIZE, (@y + 1) / TILE_SIZE) if col @x = col[:x] * TILE_SIZE @x -= crouching? ? PLAYER_SIZE : PLAYER_SIZE / 2 @x -= 1 do_collide(:right, true) end nil end |
#check_out_of_game_map ⇒ Object
def check_out_of_game_map #die
# y
if @y < 0
die
elsif @y > WINDOW_SIZE_Y
die
end
# x ( comment me out to add the glitch feature agian )
if @x < 0
die
elsif @x > WINDOW_SIZE_X - TILE_SIZE - 1
die
end
end swap size
173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/share/player.rb', line 173 def check_out_of_game_map # y if @y.negative? die elsif @y >= WINDOW_SIZE_Y - TILE_SIZE die end # x ( comment me out to add the glitch feature agian ) if @x.negative? @x = WINDOW_SIZE_X - @w - 2 elsif @x > WINDOW_SIZE_X - @w - 1 @x = 0 end end |
#check_player_collide(other) ⇒ Object
140 141 142 143 144 145 146 147 148 149 |
# File 'lib/share/player.rb', line 140 def check_player_collide(other) # $console.log "x: #{@x} y: #{@y} ox: #{other.x} oy: #{other.y}" # x crash is more rare so make it the outer condition if other.x + other.w > @x && other.x < @x + @w && (other.y + other.h > @y && other.y < @y + @h) # $console.log "collide!" return @x < other.x ? -7 : 7 end 0 end |
#collide_string ⇒ Object
281 282 283 284 285 286 |
# File 'lib/share/player.rb', line 281 def collide_string str = "collide:\n" str += "down: #{@collide[:down]} up: #{@collide[:up]}\n" str += "left: #{@collide[:left]} right: #{@collide[:right]}" str end |
#crouch! ⇒ Object
124 125 126 127 128 |
# File 'lib/share/player.rb', line 124 def crouch! @state[:crouching] = true @w = PLAYER_SIZE @h = PLAYER_SIZE / 2 end |
#crouching? ⇒ Boolean
136 137 138 |
# File 'lib/share/player.rb', line 136 def crouching? @state[:crouching] end |
#damage(attacker) ⇒ Object
151 152 153 154 155 156 |
# File 'lib/share/player.rb', line 151 def damage(attacker) @bleed_ticks = 3 @health -= 1 $console.log "'#{attacker.id}:#{attacker.name}' damaged '#{@id}:#{@name}'" die(attacker) if @health <= 0 end |
#die(killer = nil) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/share/player.rb', line 188 def die(killer = nil) if killer.nil? $console.log("player ID=#{@id} name='#{@name}' died") else if killer.id == id killer.score -= 1 else killer.score += 1 end killer.score = killer.score.clamp(0, NET_MAX_INT) $console.log("player '#{@id}:#{@name}' was killed by '#{killer.id}:#{killer.name}'") end @x = SPAWN_X @y = SPAWN_Y @health = 3 end |
#do_collide(position, value) ⇒ Object
288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/share/player.rb', line 288 def do_collide(position, value) if position == :right && @dx.positive? @dx = 0 elsif position == :left && @dx.negative? @dx = 0 elsif position == :down && @dy.positive? @dy = 0 elsif position == :up && @dy.negative? @dy = 0 end @collide[position] = value end |
#do_jump ⇒ Object
267 268 269 270 271 272 273 274 275 |
# File 'lib/share/player.rb', line 267 def do_jump return unless @collide[:down] @dy = if @dead -5 else crouching? ? -15 : -20 end end |
#draw_tick ⇒ Object
client only #
58 59 60 61 |
# File 'lib/share/player.rb', line 58 def draw_tick @tick += 1 update_img end |
#move_left(game_map) ⇒ Object
250 251 252 253 254 |
# File 'lib/share/player.rb', line 250 def move_left(game_map) # @dx = -8 @x -= crouching? ? 4 : 8 check_move_left(game_map) end |
#move_right(game_map) ⇒ Object
256 257 258 259 260 |
# File 'lib/share/player.rb', line 256 def move_right(game_map) # @dx = 8 @x += crouching? ? 4 : 8 check_move_right(game_map) end |
#net_to_state(net) ⇒ Object
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
# File 'lib/share/player.rb', line 354 def net_to_state(net) @state = case net when 'b' { bleeding: true, crouching: false, fire: 0 } when 'c' { bleeding: false, crouching: true, fire: 0 } when 's' { bleeding: true, crouching: true, fire: 0 } when 'x' { bleeding: true, crouching: false, fire: 1 } when 'y' { bleeding: true, crouching: false, fire: 2 } when 'z' { bleeding: true, crouching: false, fire: 3 } when '1' { bleeding: false, crouching: false, fire: 1 } when '2' { bleeding: false, crouching: false, fire: 2 } when '3' { bleeding: false, crouching: false, fire: 3 } else { bleeding: false, crouching: false, fire: 0 } end end |
#reset_collide ⇒ Object
301 302 303 |
# File 'lib/share/player.rb', line 301 def reset_collide @collide = { up: false, down: false, right: false, left: false } end |
#state_to_net ⇒ Object
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 |
# File 'lib/share/player.rb', line 326 def state_to_net @w = PLAYER_SIZE / 2 @h = PLAYER_SIZE if @state[:bleeding] && crouching? 's' elsif @state[:bleeding] && @state[:fire] == 1 'x' elsif @state[:bleeding] && @state[:fire] == 2 'y' elsif @state[:bleeding] && @state[:fire] == 3 'z' elsif @state[:bleeding] 'b' elsif crouching? @w = PLAYER_SIZE @h = PLAYER_SIZE / 2 'c' elsif @state[:fire] == 1 '1' elsif @state[:fire] == 2 '2' elsif @state[:fire] == 3 '3' else '0' end end |
#stop_crouch! ⇒ Object
130 131 132 133 134 |
# File 'lib/share/player.rb', line 130 def stop_crouch! @state[:crouching] = false @w = PLAYER_SIZE / 2 @h = PLAYER_SIZE end |
#tick ⇒ Object
server only #
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/share/player.rb', line 112 def tick move_x(@dx) move_y(@dy) @dx = normalize_zero(@dx) # @dy = normalize_zero(@dy) check_out_of_game_map return unless @bleed_ticks.positive? @bleed_ticks -= 1 state[:bleeding] = @bleed_ticks.zero? == false end |
#to_n_pck ⇒ String
Creates name package str
only used by server
312 313 314 315 |
# File 'lib/share/player.rb', line 312 def to_n_pck name = @name.ljust(NAME_LEN, ' ') "#{@id.to_s(16)}#{net_pack_int(@score)}#{name}" end |
#to_s ⇒ Object
317 318 319 320 321 322 323 324 |
# File 'lib/share/player.rb', line 317 def to_s pos = "#{net_pack_bigint(@x, 2)}#{net_pack_bigint(@y, 2)}" proj = @projectile.r.to_i.to_s # HACK: nil to "0" fake_y = @projectile.y.positive? ? @projectile.y : 0 proj += "#{net_pack_bigint(@projectile.x, 2)}#{net_pack_bigint(fake_y, 2)}" aim = "#{net_pack_bigint(@aim_x, 2)}#{net_pack_bigint(@aim_y, 2)}" "#{@id.to_s(16)}#{net_pack_int(@score)}#{state_to_net}#{proj}#{aim}#{pos}" end |
#update_img ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/share/player.rb', line 63 def update_img return if @tick % 5 != 0 new_x = true if @x != @last_x if @y != @last_y new_y = true @not_changed_y = 0 else @not_changed_y += 1 end if new_x || new_y @img_index += 1 @img_index = 0 if @img_index > 4 # $console.log "img updated to: #{@img_index}" end @last_x = @x @last_y = @y # if @not_changed_y > 10 # $console.log "player is chillin" # @img_index = 5 # end end |