-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathenemy.lua
More file actions
172 lines (141 loc) · 4.53 KB
/
Copy pathenemy.lua
File metadata and controls
172 lines (141 loc) · 4.53 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
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
164
165
166
167
168
169
170
171
172
local Enemy = {}
Enemy.__index = Enemy
local Player = require("player")
local ActiveEnemies = {}
function Enemy.new(x, y)
local instance = setmetatable({}, Enemy) -- create a new instance of the Enemy class
instance.x = x
instance.y = y
instance.offsetY = -8
instance.rotation = 0
instance.flipCooldown = 0
instance.speed = 100
instance.speedMod = 1
instance.xVel = instance.speed
instance.damage = 1
instance.rageCounter = 0
instance.rageTrigger = 3
instance.state = "walk"
instance.animation = {
timer = 0,
rate = 0.2
}
instance.animation.run = {
total = 6,
current = 1,
img = Enemy.runAnim
}
instance.animation.walk = {
total = 6,
current = 1,
img = Enemy.walkAnim
}
instance.animation.draw = instance.animation.walk.img[1]
instance.physics = {}
instance.physics.body = love.physics.newBody(World, instance.x, instance.y, "dynamic")
instance.physics.body:setFixedRotation(true)
instance.physics.shape = love.physics.newRectangleShape(instance.width * 0.55, instance.height * 0.5)
instance.physics.fixture = love.physics.newFixture(instance.physics.body, instance.physics.shape)
instance.physics.body:setMass(25)
table.insert(ActiveEnemies, instance)
end
function Enemy.loadAssets()
Enemy.runAnim = {}
for i = 1, 6 do
Enemy.runAnim[i] = love.graphics.newImage("assets/enemy/run/run" .. i .. ".png")
end
Enemy.walkAnim = {}
for i = 1, 6 do
Enemy.walkAnim[i] = love.graphics.newImage("assets/enemy/walk/walk" .. i .. ".png")
end
Enemy.width = Enemy.runAnim[1]:getWidth()
Enemy.height = Enemy.runAnim[1]:getHeight()
end
function Enemy:incrementRage()
self.rageCounter = self.rageCounter + 1
if self.rageCounter > self.rageTrigger then
self.state = "run"
self.speedMod = 3
self.rageCounter = 0
else
self.state = "walk"
self.speedMod = 1
end
end
function Enemy:flipDirection()
if self.flipCooldown <= 0 then
self.xVel = -self.xVel
self.flipCooldown = 0.1 -- Set the cooldown to 2 seconds before the next flip is allowed
end
end
function Enemy:syncPhysics()
self.x, self.y = self.physics.body:getPosition()
self.physics.body:setLinearVelocity(self.xVel * self.speedMod, 100)
end
function Enemy:update(dt)
self:syncPhysics()
self:animate(dt)
-- Decrement the flip cooldown timer
if self.flipCooldown > 0 then
self.flipCooldown = self.flipCooldown - dt
end
end
function Enemy:animate(dt)
self.animation.timer = self.animation.timer + dt
if self.animation.timer > self.animation.rate then
self.animation.timer = 0
self:setNextFrame()
end
end
function Enemy:setNextFrame()
local anim = self.animation[self.state] -- this is a reference, it will mirror the changes made to self.animation
if(anim.current < anim.total) then
anim.current = anim.current + 1
else
anim.current = 1
end
self.animation.draw = anim.img[anim.current]
end
function Enemy:draw()
local scaleX = 1
if(self.xVel < 0) then
scaleX = -1
end
love.graphics.draw(self.animation.draw, self.x, self.y + self.offsetY, 0, scaleX, 1 , self.width/2, self.height/2)
-- love.graphics.draw(self.img, self.x, self.y, self.rotation, self.scaleX, 1 , self.width/2, self.height/2) -- shows rotation
-- self:drawDebugOutline() -- debug mode
end
function Enemy:drawDebugOutline()
love.graphics.setColor(255, 0, 0, 255)
love.graphics.polygon("line", self.physics.body:getWorldPoints(self.physics.shape:getPoints()))
love.graphics.setColor(255, 255, 255, 255)
end
function Enemy:drawAll()
for i, instance in ipairs(ActiveEnemies) do
instance:draw()
end
end
function Enemy:updateAll(dt)
for i, instance in ipairs(ActiveEnemies) do
instance:update(dt)
end
end
function Enemy.removeAll()
for i,v in ipairs(ActiveEnemies) do
v.physics.body:destroy()
end
ActiveEnemies = {}
end
function Enemy.beginContact(a, b, collision)
for i, instance in ipairs(ActiveEnemies) do
if a == instance.physics.fixture or b == instance.physics.fixture then
if a == Player.physics.fixture or b == Player.physics.fixture then
Player:takeDamage(instance.damage)
end
-- Using a tolerance for comparing floating-point numbers
instance:incrementRage()
instance:flipDirection()
end
end
end
return Enemy