-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenemies.lua
More file actions
80 lines (66 loc) · 1.95 KB
/
enemies.lua
File metadata and controls
80 lines (66 loc) · 1.95 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
---@diagnostic disable: lowercase-global, undefined-global
constants = require 'constants'
ENEMY_SPEED = 100
ENEMY_HP = 10
ENEMY_MANA = 10
ENEMY_COOLDOWN = 2
enemy = {
x = VIRTUAL_WIDTH / 2 - 25,
y = 50,
hp = ENEMY_HP,
mana = ENEMY_MANA,
speed = ENEMY_SPEED,
fireCooldown = ENEMY_COOLDOWN
}
enemiesTable = {}
enemy.direction = 1
enemy.dirTimer = 0
function handleEnemyMovement(dt)
enemy.dirTimer = enemy.dirTimer - dt
if enemy.dirTimer <= 0 then
enemy.direction = (math.random(2) == 1) and -1 or 1
enemy.speed = math.random(40, 120)
enemy.dirTimer = math.random() * 2 + 0.5
end
enemy.x = enemy.x + enemy.direction * enemy.speed * dt
end
function drawEnemy()
love.graphics.setColor(RED)
drawEnemyShip('fill', enemy.x + 25, enemy.y + 10)
if enemy.x < 50 then
drawEnemyShip('fill', enemy.x + VIRTUAL_WIDTH + 25, enemy.y + 10)
elseif enemy.x > VIRTUAL_WIDTH - 50 then
drawEnemyShip('fill', enemy.x - VIRTUAL_WIDTH + 25, enemy.y + 10)
end
end
function enemyShoots(enemy, dt)
enemy.fireCooldown = enemy.fireCooldown - dt
if enemy.mana <= 0 then
enemy.mana = ENEMY_MANA
end
if enemy.fireCooldown <= 0 and enemy.mana > 0 then
createEnemyBullet(eBulletsTable, enemy, 314)
enemy.mana = enemy.mana - 1
enemy.fireCooldown = 0.5
end
end
function drawEnemyShip(mode, x, y)
local w1 = math.atan(2 / 5)
local w2 = math.atan(5 / 5)
local k1 = math.sqrt(4 + 25)
local k2 = math.sqrt(25 + 25)
local a = 10
local b = a * math.tan(w1 / 2)
local c = a * math.tan(k1 / 2)
local d = a * math.tan(k2 / 2)
local vertices = {
a, b, c, d, d, c, b, a,
-b, a, -d, c, -c, d, -a, b,
-a, -b, -c, -d, -d, -c, -b, -a,
b, -a, d, -c, c, -d, a, -b
}
love.graphics.push()
love.graphics.translate(x + 0.4, y + 0.4)
love.graphics.polygon('line', vertices)
love.graphics.pop()
end