Module: Player

Includes:
EntityConstants
Included in:
Entity::Gecko, Entity::Ghost
Defined in:
lib/game_2d/player.rb

Overview

The base module representing what all Players have in common Moves can be enqueued by calling add_move The server instantiates classes that mix in this module, to represent each connected player

Constant Summary

Constants included from EntityConstants

EntityConstants::CELL_WIDTH_IN_PIXELS, EntityConstants::MAX_VELOCITY, EntityConstants::PIXEL_WIDTH, EntityConstants::WIDTH

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#complex_moveObject

Returns the value of attribute complex_move.



12
13
14
# File 'lib/game_2d/player.rb', line 12

def complex_move
  @complex_move
end

#player_nameObject

Returns the value of attribute player_name.



12
13
14
# File 'lib/game_2d/player.rb', line 12

def player_name
  @player_name
end

#scoreObject

Returns the value of attribute score.



12
13
14
# File 'lib/game_2d/player.rb', line 12

def score
  @score
end

Instance Method Details

#add_move(new_move) ⇒ Object

Accepts a hash, with a key :move => move_type



33
34
35
36
# File 'lib/game_2d/player.rb', line 33

def add_move(new_move)
  return unless new_move
  @moves << new_move
end

#dieObject



49
50
51
52
53
54
55
56
# File 'lib/game_2d/player.rb', line 49

def die
  ghost = Entity::Ghost.new(player_name)
  ghost.x, ghost.y, ghost.a, ghost.x_vel, ghost.y_vel, ghost.score =
    x, y, 0, x_vel, y_vel, score
  return unless space << ghost # coast to coast

  replace_player_entity ghost
end

#draw(window) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/game_2d/player.rb', line 60

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, # Centered X; above Y
    1.0, 1.0, Gosu::Color::YELLOW)
end

#draw_zorderObject



58
# File 'lib/game_2d/player.rb', line 58

def draw_zorder; ZOrder::Player end

#initialize_playerObject



14
15
16
17
# File 'lib/game_2d/player.rb', line 14

def initialize_player
  @moves = []
  @complex_move = nil
end

#next_moveObject



38
# File 'lib/game_2d/player.rb', line 38

def next_move; @moves.shift; end

#perform_complex_moveObject

Returns true if a complex move is in process, and took some action Returns nil if the complex move completed, or there isn’t one



22
23
24
25
26
27
28
29
30
# File 'lib/game_2d/player.rb', line 22

def perform_complex_move
  return unless @complex_move

  # returns true if more work to do
  return true if @complex_move.update(self)

  @complex_move.on_completion(self)
  @complex_move = nil
end

#replace_player_entity(new_entity) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/game_2d/player.rb', line 40

def replace_player_entity(new_entity)
  if (game = space.game).is_a? GameClient
    game.player_id = new_entity.registry_id if game.player_id == registry_id
  else
    game.replace_player_entity(player_name, new_entity.registry_id)
  end
  space.doom(self)
end

#to_sObject



68
69
70
# File 'lib/game_2d/player.rb', line 68

def to_s
  "#{player_name} (#{self.class.name} #{registry_id_safe}) at #{x}x#{y}"
end