-
Notifications
You must be signed in to change notification settings - Fork 0
/
enemy.lua
100 lines (76 loc) · 1.81 KB
/
enemy.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
--
-- enemy.lua
-- defines the Enemy class
--
Enemy = {}
Enemy.__index = Enemy
function Enemy.new(type, x, y)
local self = {}
setmetatable(self, Enemy)
local e = enemies[type]
-- position
self.x = x
self.y = y
self.width = e.width
self.height = e.height
self.offset = e.offset
self.rect = HC.rectangle(x+self.offset.x,y+self.offset.y,self.width,self.height)
-- descriptors
self.type = 'enemy'
self.name = e.name
self.description = e.description
self.img = e.image
self.health = e.health
self.active = true
-- moving
self.start = x
self.range = e.range
self.fin = self.start + self.range
self.step_x = e.step_x
self.vel = { x = e.step_x, y = 0 }
self.dwalk = 0
self.imgNum = 1
self:loadImages(e)
return self
end
function Enemy:loadImages(e)
-- load images for walkcycles
self.walkL = {}
self.walkL[1] = e.cycleImages[1]
self.walkL[2] = e.cycleImages[2]
self.walkL[3] = e.cycleImages[3]
self.walkR = {}
self.walkR[1] = e.cycleImages[4]
self.walkR[2] = e.cycleImages[5]
self.walkR[3] = e.cycleImages[6]
end
function Enemy:update(dt, bgPos)
self:walkcycle(dt)
if self.step_x ~= 0 then
if self.x >= self.fin + bgPos then
self.vel.x = -self.step_x
elseif self.x < self.start + bgPos then
self.vel.x = self.step_x
end
self.x = self.x + self.vel.x
self.rect:moveTo(self.x + self.width/2 + self.offset.x, self.y + self.height/2 + self.offset.y)
end
end
function Enemy:draw()
love.graphics.draw(self.img, self.x, self.y)
end
function Enemy:walkcycle(dt)
self.dwalk = self.dwalk + dt
-- while walking
if self.dwalk > 0.3 then
if self.vel.x < 0 then
self.img = self.walkL[self.imgNum]
else
self.img = self.walkR[self.imgNum]
end
-- increment imgNum
self.imgNum = self.imgNum + 1
if self.imgNum > 3 then self.imgNum = 1 end
self.dwalk = self.dwalk - 0.3
end
end