Class: Entity::Slime
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
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
#bottom_cell_y_at, #constrain_velocity, #left_cell_x_at, #right_cell_x_at, #top_cell_y_at
#transparent?
#nullsafe_registry_id, #registry_id, #registry_id=, #registry_id?, #registry_id_safe
#<=>, #==, as_json, #eql?, from_json, #hash, #to_json
Constructor Details
#initialize ⇒ Slime
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
#hp ⇒ Object
Returns the value of attribute hp.
17
18
19
|
# File 'lib/game_2d/entity/slime.rb', line 17
def hp
@hp
end
|
#sleep_count ⇒ Object
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_count ⇒ Object
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
#advance ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/game_2d/entity/slime.rb', line 70
def advance
= beneath
if .size == 1
slide_around(.first, false) or move or turn_around
else
move or turn_around
end
end
|
#all_state ⇒ Object
27
|
# File 'lib/game_2d/entity/slime.rb', line 27
def all_state; super.push(hp, sleep_count, slime_count); end
|
#as_json ⇒ Object
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))
img.draw(
self.pixel_x + (a == 90 ? CELL_WIDTH_IN_PIXELS : 0), self.pixel_y, draw_zorder,
(a == 90 ? -1 : 1) )
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_filename ⇒ Object
101
|
# File 'lib/game_2d/entity/slime.rb', line 101
def image_filename; "slime.png"; end
|
#should_fall? ⇒ Boolean
43
|
# File 'lib/game_2d/entity/slime.rb', line 43
def should_fall?; empty_underneath?; end
|
#sleep_now? ⇒ 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_s ⇒ Object
118
|
# File 'lib/game_2d/entity/slime.rb', line 118
def to_s; "#{super} (#{@hp} HP)"; end
|
#trapped? ⇒ Boolean
45
|
# File 'lib/game_2d/entity/slime.rb', line 45
def trapped?; !(empty_on_left? || empty_on_right?); end
|
#turn_around ⇒ Object
82
|
# File 'lib/game_2d/entity/slime.rb', line 82
def turn_around; self.a += 180; end
|
#update ⇒ Object
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
|