-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.lua
129 lines (108 loc) · 3.7 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
124
125
126
127
128
129
-- Copyright (C) 2023 Alessandro Amatucci Girlanda
-- This file is part of Invaders in Space.
-- Invaders in Space is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
-- Invaders in Space is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
-- You should have received a copy of the GNU General Public License
-- along with Invaders in Space. If not, see <https://www.gnu.org/licenses/>.
Player = Entity:extend()
function Player:new(image, x, y)
Player.super.new(self, image, x, y)
self.health = PLAYER_HEALTH
self.width = PLAYER_WIDTH
self.height = PLAYER_HEIGHT
self.speed = PLAYER_SPEED
self:setQuads()
self.current_texture = 1
self.texture = self.quads[self.current_texture]
self.texture_change = 0.1
self.texture_timer = 0
self.immortal = false
self.hit_stop = 0.1
self.hit_timer = self.hit_stop
self.visibility = true
self.tutorial_animation = false
end
function Player:update(dt)
-- Move Player and keep it in the screen
if love.keyboard.isDown("left") then
self.x = self.x - self.speed * dt
if self.x <= 0 then
self.x = 0
end
elseif love.keyboard.isDown("right") then
self.x = self.x + self.speed * dt
if self.x + self.width >= WINDOW_WIDTH then
self.x = WINDOW_WIDTH - self.width
end
end
self:textureUpdate(dt)
end
function Player:tutorialUpdate(dt)
if self.tutorial_animation then
self.x = self.x + self.speed * dt
if (self.x <= self.start_x or self.x >= self.start_x + 50) then
self.speed = self.speed * (-1)
end
end
self:textureUpdate(dt)
end
function Player:draw()
if (self.visibility) then
love.graphics.draw(self.image, self.texture, self.x, self.y)
end
end
-- Player Animation
function Player:textureUpdate(dt)
if (self.hit_timer < self.hit_stop) then
-- Select 'hit' texture
self.current_texture = #self.quads
self:textureChange()
self.hit_timer = self.hit_timer + dt
-- Reset the 'immortal' state
if (self.hit_timer >= self.hit_stop) then
self.immortal = false
end
elseif (self.texture_timer >= self.texture_change) then
-- Change current texture/frame with the following one
self.current_texture = self.current_texture + 1
if (self.current_texture > #self.quads - 1) then
self.current_texture = 1
end
self:textureChange()
-- Update timer
self.texture_timer = self.texture_timer - self.texture_change
else
-- Increase timer
self.texture_timer = self.texture_timer + dt
end
end
function Player:textureChange()
self.texture = self.quads[self.current_texture]
end
-- Show player's health as the first quad of the player's picture
function Player:healthDraw()
local scale = 0.6
for i=0,self.health - 1 do
love.graphics.draw(self.image, self.quads[1], (self.width / 2 + i * (self.width + 5)) * scale, (self.height / 2) * scale, 0, scale, scale)
end
end
function Player:checkDeath()
if (self.health == 0) then
return true
end
return false
end
function Player:hit(change)
if (not self.immortal) then
self.immortal = true
playSound(player_hit_sound)
self:updateHealth(change)
self.hit_timer = 0
end
end