class Entity::Slime
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
Constants
- MAX_HP
- MAX_SLIME_COUNT
- MAX_SPEED
- SLEEP_AMOUNT
Attributes
hp[R]
sleep_count[R]
slime_count[R]
Public Class Methods
new()
click to toggle source
Calls superclass method
Entity::new
# 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
Public Instance Methods
advance()
click to toggle source
# 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_state()
click to toggle source
Calls superclass method
Entity#all_state
# File lib/game_2d/entity/slime.rb, line 27 def all_state; super.push(hp, sleep_count, slime_count); end
as_json()
click to toggle source
Calls superclass method
Entity#as_json
# 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)
click to toggle source
# 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)
click to toggle source
# 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
hp=(p)
click to toggle source
# File lib/game_2d/entity/slime.rb, line 25 def hp=(p); @hp = [[p, MAX_HP].min, 0].max; end
i_hit(others, velocity)
click to toggle source
# File lib/game_2d/entity/slime.rb, line 84 def i_hit(others, velocity) slime_them(others, velocity) end
image_filename()
click to toggle source
# File lib/game_2d/entity/slime.rb, line 101 def image_filename; "slime.png"; end
should_fall?()
click to toggle source
# File lib/game_2d/entity/slime.rb, line 43 def should_fall?; empty_underneath?; end
sleep_now?()
click to toggle source
# File lib/game_2d/entity/slime.rb, line 47 def sleep_now?; false; end
slime_them(others, increment)
click to toggle source
# 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_s()
click to toggle source
# File lib/game_2d/entity/slime.rb, line 118 def to_s; "#{super} (#{@hp} HP)"; end
trapped?()
click to toggle source
# File lib/game_2d/entity/slime.rb, line 45 def trapped?; !(empty_on_left? || empty_on_right?); end
turn_around()
click to toggle source
# File lib/game_2d/entity/slime.rb, line 82 def turn_around; self.a += 180; end
update()
click to toggle source
# 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)
click to toggle source
Calls superclass method
Entity#update_from_json
# 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