-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.lua
More file actions
117 lines (94 loc) · 3.1 KB
/
Copy pathplayer.lua
File metadata and controls
117 lines (94 loc) · 3.1 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
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
module(..., package.seeall)
require 'vector'
require 'shot'
require 'physics'
Player = {}
function new(o)
-- set up the player at the center of the screen
o = o or {}
o.radius = 10
-- start at the center of the screen
o.x = love.graphics.getWidth() / 2
o.y = love.graphics.getHeight() / 2
o.velocity = vector.newWithMagnitudeAngle(0, 0)
o.angle = 0
o.shots = {}
o.alive = true
o.image = love.graphics.newCanvas(32, 32)
o.image:renderTo(function()
love.graphics.setLineWidth(2.5)
local polygon = { 6, 31,
26, 31,
16, 2,
6, 31 }
love.graphics.polygon('line', polygon)
end)
o.gameover = love.graphics.newCanvas()
o.gameover:renderTo(function()
love.graphics.setColor(255, 255, 255, 255)
love.graphics.setFont(love.graphics.newFont(96))
love.graphics.printf('GAME OVER', 200, 300, 10000, 'left')
end)
setmetatable(o, Player)
return o
end
function Player:shoot()
self.shots[#self.shots+1] = shot.new(self)
end
function Player:draw()
if self.alive then
love.graphics.setColor(255, 255, 255, 255)
love.graphics.draw(self.image, self.x, self.y, self.angle, 1, 1, 16, 16)
else
love.graphics.setColor(255, 255, 255, 255)
love.graphics.draw(self.gameover)
end
for _, shot in ipairs(self.shots) do shot:draw() end
end
function Player:update(dt)
-- handle acceleration/deceleration
if love.keyboard.isDown('up') then
self.velocity = self.velocity:add(vector.newWithMagnitudeAngle(-dt * 3, -self.angle))
elseif love.keyboard.isDown('down') then
self.velocity = self.velocity:add(vector.newWithMagnitudeAngle(dt * 3, -self.angle))
elseif love.keyboard.isDown('left') then
self.angle = self.angle - dt * 3
elseif love.keyboard.isDown('right') then
self.angle = self.angle + dt * 3
end
-- handle rotation
self.angle = self.angle % (2 * math.pi)
local obj = { prev_x = self.x - 6,
prev_y = self.y,
width = 20,
height = 30,
original = self }
-- handle movement
self.x, self.y = physics.update(self.x, self.y, self.velocity, dt)
-- handle wrapping
self.x = self.x % love.graphics.getWidth()
self.y = self.y % love.graphics.getHeight()
obj.x, obj.y = self.x, self.y
collisionDetector:addObject(obj)
collidableObjects[#collidableObjects+1] = obj
-- now handle the shots
for _, shot in ipairs(self.shots) do shot:update(dt) end
-- remove any shots which are too old
for i in pairs(self.shots) do
if self.shots[i]:shouldBeRemoved() then
table.remove(self.shots, i)
end
end
end
-- double dispatch for post-collision handling
function Player:collideWith(other)
return other:collideWithPlayer(self)
end
-- ignore interaction with myself or shots
function Player:collideWithShot(s) end
function Player:collideWithPlayer(p) end
-- end the game when this happens
function Player:collideWithAsteroid(ast)
self.alive = false
end
Player.__index = Player