-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.lua
More file actions
executable file
·99 lines (72 loc) · 1.99 KB
/
player.lua
File metadata and controls
executable file
·99 lines (72 loc) · 1.99 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
require 'vector.lua'
local p = love.physics
local g = love.graphics
local a = love.audio
Player = class('Player')
function Player:initialize(position)
self.mass = 12
self.radius = 15
self.force = 35
self.dead = false
self.atExit = false
self.w = 150
self.h = 150
self.time = 0
if world then
self.body = p.newBody(world, position.x + self.w/2, position.y+ self.w/2, self.mass, 0) --place the body in the center of the world, with a mass of 15
self.shape = p.newRectangleShape(self.body, 0, 0, 30, 80)
--self.shape = p.newCircleShape(self.body, 0, 0, self.radius) --the ball has a radius of 20
self.shape:setData(self)
self.shape:setFriction(0.1)
end
local img = Assets.LoadImage('texture01.png')
self.walk = newAnimation(img, 500, 540, self.w, self.h, 0.2, 8, 3)
-- dieSound = dieSound or a.newSource('assets/sounds/die.wav')
-- dieSound:setVolume(0.3)
-- dieSound:setLooping(true)
end
function Player:kill()
self.dead = true
end
function Player:getPosition()
return Vector:new(self.body:getX(), self.body:getY())
end
function Player:collisionBegin(o,c)
if o and o.wall and self.lastCollision ~= o then
self.lastCollision = o
self.force = -self.force
end
end
function Player:collisionPersist(o,c)
vx, vy = self.body:getLinearVelocity()
if math.abs(vx) < 130 then
self.body:applyForce(self.force, 0)
end
end
function Player:collisionEnd(o,c)
end
function Player:update(dt)
if self.body:getY() - self.radius >= g.getHeight() then
self:die()
end
if self.body:getY() > 650 then
-- dieSound:play()
end
self.walk:update(dt)
self.time = self.time + dt
end
function Player:die()
self.dead = true
end
function Player:draw()
local pos = self:getPosition()
g.setColor(255,255,255,255)
if self.force > 0 then
self.walk:draw(pos.x - self.w/2, pos.y - self.h/2, 0, 1, 1)
else
self.walk:draw(pos.x + self.w/2, pos.y - self.h/2, 0, -1, 1)
end
g.setColor(0,0,255,255)
-- g.polygon("fill", self.shape:getPoints())
-- g.circle("fill", pos.x, pos.y, 4, 32)
end