-
Notifications
You must be signed in to change notification settings - Fork 0
/
laser.lua
164 lines (142 loc) · 4.45 KB
/
laser.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
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
require "vector"
require "effects"
require "color"
Laser = {}
function Laser:new( pos, dir, player )
local l = {}
setmetatable( l, self )
self.__index = self
l.pos = pos:copy()
l.dir = dir:normalize()
if player.shields > 0 then
l.color = player.color:combine( player.shieldColor )
else
l.color = player.color:copy()
end
l.w = 24
l.h = 4
l.player = player
l:loadSprite()
return l
end
function Laser:loadSprite()
if not Laser.sprite then
Laser.sprite = love.graphics.newCanvas( self.w, self.h )
love.graphics.push()
love.graphics.origin()
love.graphics.setCanvas( Laser.sprite )
love.graphics.setColor( 1, 1, 1 )
love.graphics.setLineWidth( self.h * love.window.getDPIScale() )
love.graphics.rectangle( "fill", 0, 0, self.w, self.h )
love.graphics.setCanvas()
love.graphics.pop()
end
end
function Laser:draw()
love.graphics.push()
love.graphics.origin()
love.graphics.setColor( self.color:toarray() )
love.graphics.draw( self.sprite, self.pos.x, self.pos.y, self.dir:angle() )
if self.debugText ~= nil then
love.graphics.setColor( 1, 1, 1 )
love.graphics.printf( self.debugText, -self.w/2, -self.h*4, self.w, "left", 0, love.window.getDPIScale(), love.window.getDPIScale() )
end
love.graphics.pop()
end
function Laser:die( withEffects, color )
-- remove this laser from the lasers array
for i, l in ipairs( LASERS ) do
if l == self then
Utils.removeAndReplace( LASERS, i, nil, "lasers" )
break
end
end
if withEffects then
EFFECTS[#EFFECTS + 1] = Spark:new( self.pos, self.dir, self.color:combine( color ) )
-- TODO: Sound effects?
end
-- TODO: spawn enemy/powerup
end
function Laser:update( dt, i )
self.pos = self.pos + (self.dir * LASER_VEL * dt)
for j, o in ipairs( LASERS ) do
-- All lasers except this one
if i ~= j then
if self:colliding( o ) then
if self.color.name ~= o.color.name then
local s1 = self:getDamage( o.color )
local s2 = o:getDamage( self.color )
self.player:addScore( s1 )
o.player:addScore( s2 )
EFFECTS[ #EFFECTS+1 ] = FloatingText:new( "+".. s1 + s2, self.color:combine( o.color ), self.pos ) --, self.player.pos )
end
self:die( true, o.color )
o:die()
return
end
end
end
-- Wall collisions
if self.pos.x < 0 then
self.dir = self.dir:reflect( Vector:new( 1, 0 ) )
self.pos.x = math.max( self.pos.x, 0 )
elseif self.pos.x > W then
self.dir = self.dir:reflect( Vector:new( -1, 0 ) )
self.pos.x = math.min( self.pos.x, W )
elseif self.pos.y < 0 then
self.dir = self.dir:reflect( Vector:new( 0, 1 ) )
self.pos.y = math.max( self.pos.y, 0 )
elseif self.pos.y > H then
self.dir = self.dir:reflect( Vector:new( 0, -1 ) )
self.pos.y = math.min( self.pos.y, H )
end
end
function Laser:colliding( o )
-- This collision detection is very sloppy, but it's good enough for now
-- TODO: Make this more accurate
return (o.pos - self.pos):length() < self.w/2
end
function Laser:lineSegment()
local hl = self.dir * (self.w/2)
return { self.pos - hl, self.pos + hl }
end
function Laser:getDamage( c )
if self.color.name == "orange" then
if c.name == "red" or c.name == "yellow" then
return 1
else
return 2
end
elseif self.color.name == "purple" then
if c.name == "red" or c.name == "blue" then
return 1
else
return 2
end
elseif self.color.name == "green" then
if c.name == "yellow" or c.name == "blue" then
return 1
else
return 2
end
elseif self.color.name == "red" then
if c.name == "orange" or c.name == "purple" then
return 1
else
return 2
end
elseif self.color.name == "blue" then
if c.name == "purple" or c.name == "green" then
return 1
else
return 2
end
elseif self.color.name == "yellow" then
if c.name == "green" or c.name == "orange" then
return 1
else
return 2
end
end
return 2
end