-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenemy.lua
118 lines (101 loc) · 2.54 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
-- Default enemy object.
local Enemy = {
name = 'enemy',
sprite = '',
xPos = 0,
yPos = 0,
originX = 0,
originY = 0,
width = 16,
height = 16,
damage = 1,
health = 100,
dir = 1,
flip = 1,
alive = true
}
-- Initialize enemy object.
function Enemy:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
-- Self explanatory.
function Enemy:attack(p)
local result = p.health - self.damage
return result
end
-- Prerequisites for movement.
-- local direction = -1
local initialtime = love.timer.getTime()
local axisY = false
local direction = 0
-- Default enemy movement.
function Enemy:walk(goalX, goalY, dt)
local stoptimer = 5 -- 5 seconds
local speed = 50
local timer = love.timer.getTime() - initialtime
if timer > stoptimer then
direction = math.random(1, 1000323)
initialtime = love.timer.getTime()
if direction % 3 == 0 then
self.dir = self.dir * -1
else
self.dir = self.dir * 1
end
-- dir = smartTimes % 3 == 0 and dir * -1 or dir * 1
axisY = axisY == false and true or false
end
if axisY == true then
goalY = goalY + speed * dt * self.dir
else
goalX = goalX + speed * dt * self.dir
end
return goalX, goalY, self.dir
end
-- Set enemy position.
function Enemy:setPos(xPos, yPos)
self.xPos = xPos
self.yPos = yPos
self.originX = xPos
self.originY = yPos
end
-- Flip sprite.
function Enemy:flipEnemy(flip)
self.flip = flip
end
-- Set enemy sprite.
function Enemy:setSprite(quad)
self.sprite = quad
end
-- Calculate distance between two points.
function dist(x1, y1, x2, y2)
return ((x2 - x1) ^ 2 + (y2 - y1) ^ 2) ^ 0.5
end
-- Function to return what coordinates to chase.
function Enemy:chase(playerX, playerY, dt)
local speed = 80
local actualX, actualY = self.xPos, self.yPos
local goalX = playerX - actualX
local goalY = playerY - actualY
local distance = math.sqrt(goalX * goalX + goalY * goalY)
if dist(actualX, actualY, playerX, playerY) ~= 0 then -- avoid division by zero
actualX = actualX + goalX / distance * speed * dt
actualY = actualY + goalY / distance * speed * dt
end
return actualX, actualY
end
-- Change enemy object damage.
function Enemy:changeDamage(damage)
self.damage = damage
end
-- Change enemy object direction.
function Enemy:changeDir(dir)
self.dir = dir
end
-- Change enemy type.
function Enemy:changeType(type)
self.name = type
end
return Enemy