This repository has been archived by the owner on Dec 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
zombiesurvival.rb
202 lines (171 loc) · 3.8 KB
/
zombiesurvival.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
require "bundler/setup"
require "gaminator"
require "./objects/living_object.rb"
require "./objects/zombie.rb"
require "./objects/player.rb"
require "./objects/particle.rb"
require "./game_master.rb"
require "./objects/map.rb"
class ZombieSurvival
def initialize(width, height)
@time = 0
@zombies = []
@game_master = GameMaster.new(self,@zombies)
@ticks = 0
@width = width
@height = height
@seconds = 0
@kills = 0
@player = SmartPlayer.new(self)
@tick_count = 0
@particles = []
@particles_tick_count = 0
@map = Map.new(width, height, @player)
initialize_player
end
def initialize_player
@player = SmartPlayer.new(self)
end
def input_map
{
?a => :move_left,
?d => :move_right,
?w => :move_up,
?s => :move_down,
?k => :try_shooting,
?q => :exit,
}
end
def move_left
@player.move(:left)
end
def move_right
@player.move(:right)
end
def move_up
@player.move(:up)
end
def move_down
@player.move(:down)
end
def tick
@tick_count += 1
move_zombies
@game_master.tick
@particles_tick_count -= 1
@particles = [] if @particles_tick_count < 0
check_hits
exit unless @player.alive?
end
def objects
[@player] + @zombies.find_all(&:alive?) + @particles + @map.objects
end
def sleep_time
0.05
end
def textbox_content
"Your HP: %d \t\t\t\tZombie killed: %d" % [@player.hp, @kills]
end
def wait?
false
end
def exit
Kernel.exit
end
def exit_message
"Your brain was really delicious :3 You've killed %d zombies though" % @kills
end
def width
77
end
def height
22
end
def is_allowed?(x,y)
!@map.is_colliding?(x,y)
end
def try_shooting
draw_shoot(@player.direction)
end
def object_positions
objects.map{|o| [o.x, o.y]}
end
def check_hits
@zombies.each do |zombie|
@player.hit if zombie.x == @player.x && zombie.y == @player.y
end
end
private
def draw_shoot(direction)
x,y = @player.x, @player.y
particles_positions.each do |pos|
@particles << Particle.new(*pos)
end
@particles_tick_count = Particle::LIVE_TIME
end
def move_zombies
@zombies.each do |zombie|
if zombie.can_move?(@tick_count)
zombie.try_moving(@player)
end
end
end
def zombie_at_position(x,y)
@zombies.find do |zombie|
zombie.x == x && zombie.y == y
end
end
def particles_positions
x, y, direction = @player.x, @player.y, @player.direction
objects = object_positions
positions = []
collision_x, collision_y = nil, nil
case direction
when :left
x -= 1
x.downto(0) do |x_pos|
if object_positions.include?([x_pos,y])
collision_x, collision_y = x_pos, y
break
end
positions << [x_pos, y]
end
when :right
x += 1
x.upto(width) do |x_pos|
if object_positions.include?([x_pos,y])
collision_x, collision_y = x_pos, y
break
end
positions << [x_pos, y]
end
when :up
y -= 1
y.downto(0) do |y_pos|
if object_positions.include?([x,y_pos])
collision_x, collision_y = x, y_pos
break
end
positions << [x, y_pos]
end
when :down
y += 1
y.upto(height) do |y_pos|
if object_positions.include?([x,y_pos])
collision_x, collision_y = x, y_pos
break
end
positions << [x, y_pos]
end
end
if (zombie = zombie_at_position(collision_x, collision_y))
zombie.hit
unless zombie.alive?
@kills +=1
@zombies.delete(zombie)
end
end
positions
end
end
Gaminator::Runner.new(ZombieSurvival, :rows => 30, :cols => 80).run