Class: Entity::Block

Inherits:
OwnedEntity show all
Defined in:
lib/game_2d/entity/block.rb

Constant Summary collapse

MAX_LEVEL =
5
HP_PER_LEVEL =
5
MAX_HP =
MAX_LEVEL * HP_PER_LEVEL

Constants included from EntityConstants

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

Instance Attribute Summary collapse

Attributes inherited from OwnedEntity

#owner_id

Attributes inherited from Entity

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

Instance Method Summary collapse

Methods inherited from OwnedEntity

#owner, #owner=

Methods inherited from Entity

#accelerate, #angle_to_vector, #bottom_cell_y, #direction_to, #doomed?, #draw, #draw_angle, #draw_zorder, #drop_diagonal, #empty_above?, #empty_on_left?, #empty_on_right?, #empty_underneath?, #entities_obstructing, #going_past_entity, #grab!, #grabbed?, #i_hit, #initialize, #left_cell_x, #move, #move_x, #move_y, #moving?, #next_to, #occupied_cells, #opaque, #pixel_x, #pixel_y, #release!, #right_cell_x, #sleep_now?, #top_cell_y, #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

Constructor Details

This class inherits a constructor from Entity

Instance Attribute Details

#hpObject

Returns the value of attribute hp.



10
11
12
# File 'lib/game_2d/entity/block.rb', line 10

def hp
  @hp
end

Instance Method Details

#all_stateObject



14
# File 'lib/game_2d/entity/block.rb', line 14

def all_state; super.push(hp); end

#as_jsonObject



15
# File 'lib/game_2d/entity/block.rb', line 15

def as_json; super.merge!(:hp => hp); end

#destroy!Object



54
55
56
# File 'lib/game_2d/entity/block.rb', line 54

def destroy!
  owner.disown_block if owner
end

#harmed_by(other) ⇒ Object



48
49
50
51
52
# File 'lib/game_2d/entity/block.rb', line 48

def harmed_by(other)
  puts "#{self}: Ouch!"
  self.hp -= 1
  @space.doom(self) if hp <= 0
end

#image_filenameObject



64
# File 'lib/game_2d/entity/block.rb', line 64

def image_filename; "#{level_name}.gif"; end

#levelObject



58
# File 'lib/game_2d/entity/block.rb', line 58

def level; (hp - 1) / HP_PER_LEVEL; end

#level_nameObject



60
61
62
# File 'lib/game_2d/entity/block.rb', line 60

def level_name
  %w(dirt brick cement steel unlikelium)[level]
end

#should_fall?Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/game_2d/entity/block.rb', line 22

def should_fall?
  return false if owner || !empty_underneath?

  case level
    when 0
      true
    when 1
      empty_on_left? || empty_on_right?
    when 2
      empty_on_left? && empty_on_right?
    when 3
      empty_on_left? && empty_on_right? && empty_above?
    when 4
      false
  end
end

#to_sObject



66
# File 'lib/game_2d/entity/block.rb', line 66

def to_s; "#{super} (#{@hp} HP)"; end

#updateObject



39
40
41
42
43
44
45
46
# File 'lib/game_2d/entity/block.rb', line 39

def update
  if should_fall?
    accelerate(0, 1)
  else
    self.x_vel = self.y_vel = 0
  end
  move
end

#update_from_json(json) ⇒ Object



17
18
19
20
# File 'lib/game_2d/entity/block.rb', line 17

def update_from_json(json)
  self.hp = json[:hp] if json[:hp]
  super
end