-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.lua
124 lines (107 loc) · 2.96 KB
/
player.lua
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
require 'arc'
Player = {
start_rads = 0,
active = nil,
toggle_time = nil, -- Time since last direction change, or nil if none.
trail = {},
trail_length = 0,
alive = true,
status = nil,
control = nil
}
function Player:update(dr)
self.active:update(dr)
if self.toggle_time ~= nil then
self.toggle_time = self.toggle_time + dr
end
self.status:updateCurrentTrail(self:length())
end
function Player:detectCollision(collider, dr)
if self.active:intersectsSelf() then
self.alive = false
else
for shape, delta in pairs(collider:collisions(self.active.co)) do
local arc = self.active
local other = shape.arc
local intersects = arc:intersectsArc(other)
if intersects > 0 then
-- OK arcs intersect but who hit who?
local regressedArc = arc:new()
regressedArc:update(-dr)
if intersects ~= regressedArc:intersectsArc(other) then
self.alive = false
end
end
end
end
end
function Player:keypressed(key, collider)
if self.control:isChangeDirectionKey(key) then
self:_changeDirection(collider)
end
end
function Player:touchpressed(x, y, collider)
if self.control:isChangeDirectionTouch(x, y) then
self:_changeDirection(collider)
end
end
function Player:_changeDirection(collider)
self.toggle_time = 0
self.status.playing = true
self.trail_length = self:length()
self.trail[#self.trail+1] = self.active
self.active = self.active:changeDirection()
self:addToCollider(collider)
end
function Player:addToCollider(collider)
self.active:addToCollider(collider)
end
function Player:draw(trail_length_font, trail_color)
local active = self.active
active:drawArc(trail_color)
if self.alive then
local end_size = 2 - (self.toggle_time ~= nil and math.min(1, self.toggle_time * 4) or 1)
active:drawEnd(trail_length_font, trail_color, end_size, self:length())
end
for i = 1, #self.trail do
self.trail[i]:drawArc(trail_color)
end
self.status:draw(trail_color)
end
function Player:playing()
return self.status.playing
end
function Player:length()
return self.trail_length + self.active:length()
end
function Player:finishedGame(score_delta)
return self.status:finishedGame(score_delta)
end
function Player:hasWon()
return self.status:hasWon()
end
function Player:reset(arc)
self.active = arc
self.toggle_time = nil
self.status.playing = false
self.trail = {}
self.trail_length = 0
self.alive = true
end
function Player:toJson()
local trail = {}
for i = 1, #self.trail do
local arc = self.trail[i]
trail[i] = arc:toJson()
end
trail[#trail+1] = self.active:toJson()
return {
trail = trail
}
end
function Player:new (o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end