forked from qwertyreddy/rubystein
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayer.rb
84 lines (68 loc) · 1.54 KB
/
player.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
require 'weapon'
class Player
include Damageable
STEP_SIZE = 12
ANGLE_SPEED = 6
FOV = 60.0 # Field of View
HALF_FOV = FOV / 2
DISTANCE_TO_PROJECTION = (Config::WINDOW_WIDTH / 2) / Math.tan((FOV / 2) * Math::PI / 180)
RAY_ANGLE_DELTA = (FOV / Config::WINDOW_WIDTH)
attr_accessor :x
attr_accessor :y
attr_accessor :height
attr_accessor :angle
attr_accessor :health
attr_accessor :weapon
attr_accessor :window
attr_accessor :score
attr_accessor :max_health
def initialize(window)
@x = 0.0
@y = 0.0
@angle = 0.0
@health = 100
@window = window
@score = 0
@max_health = 100
end
def angle_in_radians
@angle * Math::PI / 180
end
def turn_left
@angle = (@angle + ANGLE_SPEED) % 360
end
def turn_right
# The added 360 here will make sure that @angle >= 0
@angle = (360 + @angle - ANGLE_SPEED) % 360
end
def dx
# x = r cos(theta)
STEP_SIZE * Math.cos(self.angle_in_radians)
end
def dy
# y = r sin(theta)
STEP_SIZE * Math.sin(self.angle_in_radians)
end
def can_move_forward?(map)
return !map.hit?(@x + 4*dx, @y - 4*dy)
end
def can_move_backward?(map)
return !map.hit?(@x - 4*dx, @y + 4*dy)
end
def move_forward
@x += dx
@y -= dy
end
def move_backward
@x -= dx
@y += dy
end
def health_percent
@health * 100.0 / @max_health
end
def take_damage_from(player)
return if @health <= 0
@health -= 4 # TODO: @health -= player.weapon.damage
@health = 0 if @health < 0
end
end