Class: Entity::Ghost

Inherits:
Entity show all
Includes:
Comparable, Player
Defined in:
lib/game_2d/entity/ghost.rb

Constant Summary collapse

MOVES_FOR_KEY_HELD =
{
  Gosu::KbLeft  => :left,
  Gosu::KbA     => :left,
  Gosu::KbRight => :right,
  Gosu::KbD     => :right,
  Gosu::KbUp    => :up,
  Gosu::KbW     => :up,
  Gosu::KbDown  => :down,
  Gosu::KbS     => :down,
}

Constants included from EntityConstants

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

Instance Attribute Summary

Attributes included from Player

#complex_move, #player_name, #score

Attributes inherited from Entity

#a, #moving, #space, #x, #x_vel, #y, #y_vel

Instance Method Summary collapse

Methods included from Player

#add_move, #die, #draw, #draw_zorder, #initialize_player, #next_move, #perform_complex_move, #replace_player_entity, #to_s

Methods inherited from Entity

#above, #accelerate, #angle_to_vector, #beneath, #bottom_cell_y, #cx, #cy, #destroy!, #direction, #direction_to, #doomed?, #draw, #draw_angle, #draw_animation, #draw_zorder, #drop_diagonal, #empty_above?, #empty_on_left?, #empty_on_right?, #empty_underneath?, #entities_obstructing, #going_past_entity, #grab!, #grabbed?, #harmed_by, #i_hit, #left_cell_x, #move, #move_x, #move_y, #moving?, #next_to, #occupied_cells, #on_left, #on_right, #opaque, #pixel_x, #pixel_y, #release!, #right_cell_x, #slide_around, #slow_by, #slower_speed, #to_s, #top_cell_y, #underfoot, #vector_to_angle, #wake!, #warp

Methods included from ClassMethods

#bottom_cell_y_at, #constrain_velocity, #left_cell_x_at, #right_cell_x_at, #top_cell_y_at

Methods included from Transparency

#transparent?

Methods included from Registerable

#nullsafe_registry_id, #registry_id, #registry_id=, #registry_id?, #registry_id_safe

Methods included from Serializable

#<=>, #==, as_json, #eql?, from_json, #hash, #to_json, #to_s

Constructor Details

#initialize(player_name = "<unknown>") ⇒ Ghost

Returns a new instance of Ghost.



27
28
29
30
31
32
# File 'lib/game_2d/entity/ghost.rb', line 27

def initialize(player_name = "<unknown>")
  super
  initialize_player
  @player_name = player_name
  @score = 0
end

Instance Method Details

#all_stateObject



98
99
100
101
# File 'lib/game_2d/entity/ghost.rb', line 98

def all_state
  # Player name goes first, so we can sort on that
  super.unshift(player_name).push(score, @complex_move)
end

#as_jsonObject



103
104
105
106
107
108
109
# File 'lib/game_2d/entity/ghost.rb', line 103

def as_json
  super.merge!(
    :player_name => player_name,
    :score => score,
    :complex_move => @complex_move.as_json
  )
end

#downObject



63
# File 'lib/game_2d/entity/ghost.rb', line 63

def down; accelerate(0, 1); end

#draw_image(anim) ⇒ Object



120
121
122
123
# File 'lib/game_2d/entity/ghost.rb', line 120

def draw_image(anim)
  # Usually frame 0, occasionally frame 1
  anim[((Gosu::milliseconds / 100) % 63) / 62]
end

#generate_move_from_click(x, y) ⇒ Object

Called by GameWindow Should return the move to be sent via ClientConnection (or nil)



94
95
96
# File 'lib/game_2d/entity/ghost.rb', line 94

def generate_move_from_click(x, y)
  [:spawn, {:x => x, :y => y}]
end

#image_filenameObject



118
# File 'lib/game_2d/entity/ghost.rb', line 118

def image_filename; "ghost.png"; end

#leftObject



60
# File 'lib/game_2d/entity/ghost.rb', line 60

def left; accelerate(-1, 0); end

#move_for_keypress(keypress) ⇒ Object

Called by GameWindow Should return the move to be sent via ClientConnection (or nil) This is for queued keypresses, i.e. those that happen on key-down only (just once for a press), not continuously for as long as held down



89
# File 'lib/game_2d/entity/ghost.rb', line 89

def move_for_keypress(keypress); nil; end

#moves_for_key_heldObject

Called by GameWindow Should return a map where the keys are… keys, and the values are the corresponding moves to be sent via ClientConnection This is for non-queued keypresses, i.e. those that happen continuously for as long as held down



79
80
81
# File 'lib/game_2d/entity/ghost.rb', line 79

def moves_for_key_held
  MOVES_FOR_KEY_HELD
end

#rightObject



61
# File 'lib/game_2d/entity/ghost.rb', line 61

def right; accelerate(1, 0); end

#should_fall?Boolean

Returns:

  • (Boolean)


36
# File 'lib/game_2d/entity/ghost.rb', line 36

def should_fall?; false; end

#sleep_now?Boolean

Returns:

  • (Boolean)


34
# File 'lib/game_2d/entity/ghost.rb', line 34

def sleep_now?; false; end

#spawn(x, y) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/game_2d/entity/ghost.rb', line 65

def spawn(x, y)
  if base = @space.available_base_near(x, y)
    warn "#{self} spawning at #{base.x}, #{base.y}"
    self.complex_move = Move::Spawn.new
    self.complex_move.target_id = base.registry_id
  end
end

#teleportable?Boolean

Returns:

  • (Boolean)


38
# File 'lib/game_2d/entity/ghost.rb', line 38

def teleportable?; false; end

#upObject



62
# File 'lib/game_2d/entity/ghost.rb', line 62

def up; accelerate(0, -1); end

#updateObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/game_2d/entity/ghost.rb', line 40

def update
  fail "No space set for #{self}" unless @space

  return if perform_complex_move

  if args = next_move
    case (current_move = args.delete(:move).to_sym)
      when :left, :right, :up, :down
        send current_move
      when :spawn
        spawn args[:x], args[:y]
      else
        puts "Invalid move for #{self}: #{current_move}, #{args.inspect}"
    end
  else
    slow_by 1
  end
  super
end

#update_from_json(json) ⇒ Object



111
112
113
114
115
116
# File 'lib/game_2d/entity/ghost.rb', line 111

def update_from_json(json)
  @player_name = json[:player_name] if json[:player_name]
  @score = json[:score] if json[:score]
  @complex_move = Serializable.from_json(json[:complex_move]) if json[:complex_move]
  super
end