-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.lua
More file actions
73 lines (67 loc) · 2.15 KB
/
player.lua
File metadata and controls
73 lines (67 loc) · 2.15 KB
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
Class = require "hump.class"
vector = require "hump.vector"
Player = Class {
init = function(self, pos, walls)
self.pos = pos
self.walls = walls
self.direction = vector(1, 0)
end
}
function Player:update(dt)
local mousePosition = vector(love.mouse.getPosition())
self.direction = (mousePosition - self.pos):normalized()
self.laserPositions = {}
self.laserEndpoints = {}
self.laserVectors = {}
self.laserTs = {}
self.laserDirections = {}
local currentPos = self.pos + 8 * self.direction
local currentDirection = self.direction
print("----")
local oldPos, oldVec
for _ = 1, 3 do
local closestPos, closestVec, closestT
for _, block in ipairs(self.walls) do
local pos = block.pos
for _, vec in ipairs(block.shape) do
local cross = currentDirection:cross(vec)
if cross ~= 0 then
local t = ((pos - currentPos):cross(vec) / cross)
local u = ((pos - currentPos):cross(currentDirection) / cross)
local intersection = currentPos + (t * currentDirection)
if u >= 0 and u <= 1 and t > .1 then
if (not closestPos and not closestVec and not closestT) or t < closestT then
closestPos = pos
closestVec = vec
closestT = t
end
end
end
pos = pos + vec
end
end
if closestPos and closestVec and closestT then
oldPos = closestPos
oldVec = closestVec
table.insert(self.laserPositions, currentPos)
table.insert(self.laserEndpoints, closestPos)
table.insert(self.laserVectors, closestVec)
table.insert(self.laserTs, closestT)
table.insert(self.laserDirections, currentDirection)
currentPos = currentPos + currentDirection * closestT
currentDirection = (-currentDirection):mirrorOn(closestVec:perpendicular():normalize_inplace())
print(currentPos, currentDirection, closestT)
else
break
end
end
end
function Player:draw()
love.graphics.setColor(255, 0, 0)
love.graphics.circle("line", self.pos.x, self.pos.y, 8, 16)
for i, _ in ipairs(self.laserPositions) do
local endpoint = self.laserPositions[i] + self.laserTs[i] * self.laserDirections[i]
love.graphics.line(self.laserPositions[i].x, self.laserPositions[i].y, endpoint.x, endpoint.y)
end
end
return Player