Class: Entity::Slime

Inherits:
Entity show all
Defined in:
lib/game_2d/entity/slime.rb

Overview

Slime is like a lemming(tm): it either falls, or it walks left or right until forced to reverse direction

As it comes into contact with other entities, it gradually increments its slime_count, until it maxes out. Then it harms whatever it just touched, and resets the count

Constant Summary collapse

MAX_HP =
8
MAX_SPEED =
18
SLEEP_AMOUNT =
180
MAX_SLIME_COUNT =
100

Constants included from EntityConstants

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

Instance Attribute Summary collapse

Attributes inherited from Entity

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

Instance Method Summary collapse

Methods inherited from Entity

#above, #accelerate, #angle_to_vector, #beneath, #bottom_cell_y, #cx, #cy, #destroy!, #direction, #direction_to, #doomed?, #draw_angle, #draw_animation, #draw_image, #draw_zorder, #drop_diagonal, #empty_above?, #empty_on_left?, #empty_on_right?, #empty_underneath?, #entities_obstructing, #going_past_entity, #grab!, #grabbed?, #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, #teleportable?, #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

Constructor Details

#initializeSlime

Returns a new instance of Slime.



19
20
21
22
23
# File 'lib/game_2d/entity/slime.rb', line 19

def initialize
  super
  self.a = 270
  @hp, @sleep_count, @slime_count = MAX_HP, 0, 0
end

Instance Attribute Details

#hpObject

Returns the value of attribute hp.



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

def hp
  @hp
end

#sleep_countObject (readonly)

Returns the value of attribute sleep_count.



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

def sleep_count
  @sleep_count
end

#slime_countObject (readonly)

Returns the value of attribute slime_count.



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

def slime_count
  @slime_count
end

Instance Method Details

#advanceObject



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/game_2d/entity/slime.rb', line 70

def advance
  blocks_underfoot = beneath
  if blocks_underfoot.size == 1
    # Slide around if we're at the corner; otherwise, move normally
    # Don't allow slide_around() to adjust our angle
    slide_around(blocks_underfoot.first, false) or move or turn_around
  else
    # Straddling two objects, or falling
    move or turn_around
  end
end

#all_stateObject



27
# File 'lib/game_2d/entity/slime.rb', line 27

def all_state; super.push(hp, sleep_count, slime_count); end

#as_jsonObject



28
29
30
31
32
33
34
# File 'lib/game_2d/entity/slime.rb', line 28

def as_json
  super.merge(
    :hp => hp,
    :sleep_count => @sleep_count,
    :slime_count => @slime_count
  )
end

#draw(window) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/game_2d/entity/slime.rb', line 103

def draw(window)
  img = draw_image(draw_animation(window))

  # Default image faces left
  # We don't rotate the slime; we just flip it horizontally
  img.draw(
    self.pixel_x + (a == 90 ? CELL_WIDTH_IN_PIXELS : 0), self.pixel_y, draw_zorder,
    (a == 90 ? -1 : 1) # X scaling factor
  )
  # 0.5, 0.5, # rotate around the center
  # 1, 1, # scaling factor
  # @color, # modify color
  # :add) # draw additively
end

#harmed_by(other, damage = 1) ⇒ Object



96
97
98
99
# File 'lib/game_2d/entity/slime.rb', line 96

def harmed_by(other, damage=1)
  self.hp -= damage
  @space.doom(self) if hp <= 0
end

#i_hit(others, velocity) ⇒ Object



84
85
86
# File 'lib/game_2d/entity/slime.rb', line 84

def i_hit(others, velocity)
  slime_them(others, velocity)
end

#image_filenameObject



101
# File 'lib/game_2d/entity/slime.rb', line 101

def image_filename; "slime.png"; end

#should_fall?Boolean

Returns:

  • (Boolean)


43
# File 'lib/game_2d/entity/slime.rb', line 43

def should_fall?; empty_underneath?; end

#sleep_now?Boolean

Returns:

  • (Boolean)


47
# File 'lib/game_2d/entity/slime.rb', line 47

def sleep_now?; false; end

#slime_them(others, increment) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/game_2d/entity/slime.rb', line 88

def slime_them(others, increment)
  @slime_count += increment
  if @slime_count > MAX_SLIME_COUNT
    @slime_count -= MAX_SLIME_COUNT
    others.each {|o| o.harmed_by(self) unless o.is_a? Slime}
  end
end

#to_sObject



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

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

#trapped?Boolean

Returns:

  • (Boolean)


45
# File 'lib/game_2d/entity/slime.rb', line 45

def trapped?; !(empty_on_left? || empty_on_right?); end

#turn_aroundObject



82
# File 'lib/game_2d/entity/slime.rb', line 82

def turn_around; self.a += 180; end

#updateObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/game_2d/entity/slime.rb', line 49

def update
  if should_fall?
    self.x_vel = @sleep_count = 0
    space.fall(self)
    move
  elsif @sleep_count.zero?
    slime_them(beneath, 1)
    if trapped?
      self.a += 180
      slime_them((a == 270) ? on_left : on_right, 1)
      @sleep_count = SLEEP_AMOUNT
    else
      self.y_vel = 0
      accelerate((a == 270) ? -1 : 1, nil, MAX_SPEED)
      advance
    end
  else
    @sleep_count -= 1
  end
end

#update_from_json(json) ⇒ Object



36
37
38
39
40
41
# File 'lib/game_2d/entity/slime.rb', line 36

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