-
Notifications
You must be signed in to change notification settings - Fork 2
/
fighter.rb
193 lines (153 loc) · 3.32 KB
/
fighter.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
require './world'
# Represents the fighters that interact with the server.
class Fighter
# Publicly accessible methods.
attr_accessor :connection, :name, :location, :number, :health
# Create the fighter
def initialize(connection)
self.connection = connection
self.name = ask_name
self.health = 100
@world = World.instance
self.number = @world.join self
self.location = @world.starting_location!
end
# Place the fighter in the world.
def spawn
connection.puts "\nSpawning in the world!\n"
@world.render
process_input
end
# Character to represent this fighter.
def icon
self.number
end
# Is the fighter at the given coordinates?
def located_here?(x,y)
self.location == [x,y]
end
def alive?
health > 0
end
private
###
# User interaction
###
# Get the fighter's name.
def ask_name
begin
connection.puts "What's your name? (10 alphanum/spaces only, please.)"
# Only grab 10 characters.
name = connection.gets.chomp[0..10]
end while !only_chars? name
name
end
# Check these are only word characters and spaces.
def only_chars?(text)
text.match(/^\w[\w| ]+$/i)
end
# Handle the input from the fighter.
def process_input
while movement = connection.gets.chomp
begin
parse movement
end
end
end
# Parse a string into a movement.
def parse(movement)
case movement
when up?
move :up
when left?
move :left
when down?
move :down
when right?
move :right
when quit?
confirm_quit
else
invalid_direction
end
end
###
# Movement text
###
def up?
/[w|W]/
end
def left?
/[a|A]/
end
def down?
/[s|S]/
end
def right?
/[d|D]/
end
def quit?
/[q|Q]/
end
###
# Movement
###
def move(direction)
send direction
@world.fighter_moved
end
# Move the fighter up in the world, wrapping around the edges.
def up
self.x = ((x-1)+10)%10
end
# Move the fighter left in the world, wrapping around the edges.
def left
self.y = ((y-1)+10)%10
end
# Move the fighter down in the world, wrapping around the edges.
def down
self.x = (x+1)%10
end
# Move the fighter right in the world, wrapping around the edges.
def right
self.y = (y+1)%10
end
# Fighter gave an invalid movement; let them know.
def invalid_direction
connection.puts "Invalid movement. Please use WASD to move!"
# Show the text for a short time before showing the world again.
sleep 0.5
@world.render
end
###
# Coordinates
###
def x
location[0]
end
def x=(point)
location[0] = point
end
def y
location[1]
end
def y=(point)
location[1] = point
end
###
# Quitting
###
# Make sure the user wants to quit.
def confirm_quit
connection.puts "Are you sure you want to quit?"
if yes_entered?
connection.close
else
@world.render
end
end
# Does the fighter enter text starting with a 'y'?
def yes_entered?
/y|Y/.match(connection.gets.chomp)
end
end