class Entity::Gecko

Constants

BRAKE_SPEED

Amount to decelerate each tick when braking

BUILD_TIME

Game ticks it takes before a block’s HP is raised by 1

MAX_HP
MOVES_FOR_KEY_HELD

Attributes

build_block_id[R]
hp[R]

Public Class Methods

new(player_name = "<unknown>") click to toggle source
Calls superclass method Entity::new
# File lib/game_2d/entity/gecko.rb, line 38
def initialize(player_name = "<unknown>")
  super
  initialize_player
  @player_name = player_name
  @score = 0
  @hp = MAX_HP
  @build_block_id = nil
  @build_level = 0
end

Public Instance Methods

all_state() click to toggle source
Calls superclass method Entity#all_state
# File lib/game_2d/entity/gecko.rb, line 209
def all_state
  # Player name goes first, so we can sort on that
  super.unshift(player_name).push(
    score, @hp, build_block_id, @complex_move)
end
as_json() click to toggle source
Calls superclass method Entity#as_json
# File lib/game_2d/entity/gecko.rb, line 215
def as_json
  super.merge!(
    :player_name => player_name,
    :score => score,
    :hp => @hp,
    :build_block => @build_block_id,
    :complex_move => @complex_move.as_json
  )
end
brake() click to toggle source
# File lib/game_2d/entity/gecko.rb, line 122
def brake; slow_by BRAKE_SPEED; end
build() click to toggle source

Create the actual block

# File lib/game_2d/entity/gecko.rb, line 134
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
    if @space << bb # generates an ID
      @build_block_id = bb.registry_id
      @build_level = 0
    end
  end
end
build_block() click to toggle source
# File lib/game_2d/entity/gecko.rb, line 60
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
build_block_id=(new_id) click to toggle source
# File lib/game_2d/entity/gecko.rb, line 54
def build_block_id=(new_id)
  @build_block_id = new_id.try(:to_sym)
end
building?() click to toggle source
# File lib/game_2d/entity/gecko.rb, line 58
def building?; @build_block_id; end
check_for_disown_block() click to toggle source
# File lib/game_2d/entity/gecko.rb, line 154
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!() click to toggle source
# File lib/game_2d/entity/gecko.rb, line 71
def destroy!
  build_block.owner_id = nil if building?
end
disown_block() click to toggle source
# File lib/game_2d/entity/gecko.rb, line 152
def disown_block; @build_block_id, @build_level = nil, 0; end
fire(x_vel, y_vel) click to toggle source

Create the actual pellet

# File lib/game_2d/entity/gecko.rb, line 127
def fire(x_vel, y_vel)
  pellet = Entity::Pellet.new(@x, @y, 0, x_vel, y_vel)
  pellet.owner = self
  @space << pellet
end
flip() click to toggle source
# File lib/game_2d/entity/gecko.rb, line 124
def flip; self.a += 180; end
generate_move_from_click(x, y) click to toggle source

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

# File lib/game_2d/entity/gecko.rb, line 169
def generate_move_from_click(x, y)
  if y < cy # Firing up
    y_vel = -Math.sqrt(2 * (cy - y)).round
    x_vel = (cx - x) / y_vel
  else
    y_vel = 0
    if y == cy
      return if x == cx
      x_vel = (x <=> cx) * MAX_VELOCITY
    else
      range = x - cx
      x_vel = (Math.sqrt(1.0 / (2.0 * (y - cy))) * range).round
    end
  end
  [:fire, {:x_vel => x_vel, :y_vel => y_vel}]
end
harmed_by(other, damage=1) click to toggle source
# File lib/game_2d/entity/gecko.rb, line 66
def harmed_by(other, damage=1)
  self.hp -= damage
  die if hp <= 0
end
hp=(p) click to toggle source
# File lib/game_2d/entity/gecko.rb, line 48
def hp=(p); @hp = [[p, MAX_HP].min, 0].max; end
image_filename() click to toggle source
# File lib/game_2d/entity/gecko.rb, line 234
def image_filename; "gecko.png"; end
move_for_keypress(keypress) click to toggle source

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

# File lib/game_2d/entity/gecko.rb, line 192
def move_for_keypress(keypress)
  case keypress
    when Gosu::KbUp, Gosu::KbW
      return building? ? :rise_up : :flip
  end
end
moves_for_key_held() click to toggle source

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

# File lib/game_2d/entity/gecko.rb, line 205
def moves_for_key_held
  MOVES_FOR_KEY_HELD
end
rise_up() click to toggle source
# File lib/game_2d/entity/gecko.rb, line 162
def rise_up
  self.complex_move = Move::RiseUp.new(self)
end
should_fall?() click to toggle source
# File lib/game_2d/entity/gecko.rb, line 52
def should_fall?; underfoot.empty?; end
sleep_now?() click to toggle source
# File lib/game_2d/entity/gecko.rb, line 50
def sleep_now?; false; end
slide(dir) click to toggle source
# File lib/game_2d/entity/gecko.rb, line 114
def slide(dir)
  if opaque(next_to(dir)).empty?
    accelerate(*angle_to_vector(dir))
  else
    self.a = dir + 180
  end
end
slide_left() click to toggle source
# File lib/game_2d/entity/gecko.rb, line 111
def slide_left; slide(self.a - 90); end
slide_right() click to toggle source
# File lib/game_2d/entity/gecko.rb, line 112
def slide_right; slide(self.a + 90); end
update() click to toggle source
# File lib/game_2d/entity/gecko.rb, line 75
def update
  fail "No space set for #{self}" unless @space
  check_for_disown_block

  return if perform_complex_move

  if falling = should_fall?
    self.a = 0
    space.fall(self)
  end

  args = next_move
  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

  # Only go around corner if sitting on exactly one object
  blocks_underfoot = underfoot
  if blocks_underfoot.size == 1
    # Slide around if we're at the corner; otherwise, move normally
    slide_around(blocks_underfoot.first) or move
  else
    # Straddling two objects, or falling
    move
  end

  # Check again whether we've moved off of a block
  # we were building
  check_for_disown_block
end
update_from_json(json) click to toggle source
Calls superclass method Entity#update_from_json
# File lib/game_2d/entity/gecko.rb, line 225
def update_from_json(json)
  @player_name = json[:player_name] if json[:player_name]
  @score = json[:score] if json[:score]
  @hp = json[:hp] if json[:hp]
  @build_block_id = json[:build_block].try(:to_sym) if json[:build_block]
  @complex_move = Serializable.from_json(json[:complex_move]) if json[:complex_move]
  super
end