Class: Player

Inherits:
Object
  • Object
show all
Defined in:
lib/share/player.rb

Overview

Player objects represent stick figures

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_xObject

Returns the value of attribute aim_x.



13
14
15
# File 'lib/share/player.rb', line 13

def aim_x
  @aim_x
end

#aim_yObject

Returns the value of attribute aim_y.



13
14
15
# File 'lib/share/player.rb', line 13

def aim_y
  @aim_y
end

#collideObject (readonly)

Returns the value of attribute collide.



16
17
18
# File 'lib/share/player.rb', line 16

def collide
  @collide
end

#collide_strObject (readonly)

Returns the value of attribute collide_str.



16
17
18
# File 'lib/share/player.rb', line 16

def collide_str
  @collide_str
end

#deadObject

Returns the value of attribute dead.



13
14
15
# File 'lib/share/player.rb', line 13

def dead
  @dead
end

#dead_ticksObject

Returns the value of attribute dead_ticks.



13
14
15
# File 'lib/share/player.rb', line 13

def dead_ticks
  @dead_ticks
end

#dxObject

Returns the value of attribute dx.



13
14
15
# File 'lib/share/player.rb', line 13

def dx
  @dx
end

#dyObject

Returns the value of attribute dy.



13
14
15
# File 'lib/share/player.rb', line 13

def dy
  @dy
end

#fire_ticksObject

Returns the value of attribute fire_ticks.



13
14
15
# File 'lib/share/player.rb', line 13

def fire_ticks
  @fire_ticks
end

#hObject (readonly)

Returns the value of attribute h.



16
17
18
# File 'lib/share/player.rb', line 16

def h
  @h
end

#idObject

Returns the value of attribute id.



13
14
15
# File 'lib/share/player.rb', line 13

def id
  @id
end

#img_indexObject (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_downloadObject

Returns the value of attribute map_download.



13
14
15
# File 'lib/share/player.rb', line 13

def map_download
  @map_download
end

#nameObject

Returns the value of attribute name.



13
14
15
# File 'lib/share/player.rb', line 13

def name
  @name
end

#projectileObject

Returns the value of attribute projectile.



13
14
15
# File 'lib/share/player.rb', line 13

def projectile
  @projectile
end

#scoreObject

Returns the value of attribute score.



13
14
15
# File 'lib/share/player.rb', line 13

def score
  @score
end

#stateObject

Returns the value of attribute state.



13
14
15
# File 'lib/share/player.rb', line 13

def state
  @state
end

#versionObject (readonly)

Returns the value of attribute version.



16
17
18
# File 'lib/share/player.rb', line 16

def version
  @version
end

#wObject (readonly)

Returns the value of attribute w.



16
17
18
# File 'lib/share/player.rb', line 16

def w
  @w
end

#wants_crouchObject

Returns the value of attribute wants_crouch.



13
14
15
# File 'lib/share/player.rb', line 13

def wants_crouch
  @wants_crouch
end

#was_crouchingObject

Returns the value of attribute was_crouching.



13
14
15
# File 'lib/share/player.rb', line 13

def was_crouching
  @was_crouching
end

#xObject

Returns the value of attribute x.



13
14
15
# File 'lib/share/player.rb', line 13

def x
  @x
end

#yObject

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_mapObject

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_stringObject



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

Returns:

  • (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_jumpObject



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_tickObject

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_collideObject



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_netObject



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

#tickObject

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_pckString

Creates name package str

only used by server

Returns:

  • (String)

    partial network packet



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_sObject



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_imgObject



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