-
Notifications
You must be signed in to change notification settings - Fork 2
/
gunner.lua
129 lines (105 loc) · 3.53 KB
/
gunner.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
local Character = require 'character'
local steer = require 'steer'
Gunner = Character:extend()
local LOAD_FRAMES = 20
local ATTACK_FRAMES = 40
function Gunner:new(x, y, life, damage, loyalty)
Gunner.super.new(self, x, y, life, damage, loyalty)
self.state = STATE_IDLE
self.target = nil
-- Motion
self.velocity = vector(0, 0)
self.max_velocity = 1.0
-- Distances
self.sight_distance = 1000
self.attack_distance = 370
-- Timers
self.loading_timer = 0
self.attacking_timer = 0
-- sprite
self.sprite = sodapop.newAnimatedSprite(x, y)
self.sprite.flipX = self.loyalty == self.LOYALTY_USER
local spritesheet = "assets/images/enemy_gunner.png"
if self.loyalty==self.LOYALTY_USER then
spritesheet = "assets/images/player_gunner.png"
end
self.sprite:addAnimation(STATE_IDLE,
{ image = love.graphics.newImage(spritesheet),
frameWidth=120, frameHeight=120, stopAtEnd=false, frames={ {1, 1, 4, 1, .2} } })
self.sprite:addAnimation(STATE_MOVING,
{ image = love.graphics.newImage(spritesheet),
frameWidth=120, frameHeight=120, stopAtEnd=false, frames={ {1, 4, 4, 4, .2} } })
self.sprite:addAnimation(STATE_LOADING,
{ image = love.graphics.newImage(spritesheet),
frameWidth=120, frameHeight=120, stopAtEnd=false, frames={ {1, 1, 4, 1, .2} } })
self.sprite:addAnimation(STATE_ATTACKING,
{ image = love.graphics.newImage(spritesheet),
frameWidth=120, frameHeight=120, stopAtEnd=false, frames={ {1, 3, 4, 3, .2} } })
self.sprite:addAnimation(STATE_DEAD,
{ image = love.graphics.newImage(spritesheet),
frameWidth=120, frameHeight=120, stopAtEnd=true, frames={ {1, 2, 4, 2, .2} } })
end
function Gunner:update(dt)
Gunner.super.update(self, dt)
if self.state == STATE_IDLE then
self:look()
elseif self.state == STATE_MOVING then
self:move()
elseif self.state == STATE_LOADING then
self:load()
elseif self.state == STATE_ATTACKING then
self:attack()
elseif self.state == STATE_DEAD then
self.dead_for = self.dead_for + dt
end
self:clamp()
end
function Gunner:look()
self:seek_target()
if not (self.target == nil) then
self:changeState(STATE_MOVING)
self.sprite.flipX = self.target.position.x < self.position.x
end
end
function Gunner:load()
if self.loading_timer >= LOAD_FRAMES then
self.loading_timer = 0
self:changeState(STATE_ATTACKING)
else
self.loading_timer = self.loading_timer + 1
end
end
function Gunner:attack()
self:seek_target()
if self.target~=nil and self.attacking_timer >= ATTACK_FRAMES then
self.attacking_timer = 0
self:shoot()
self.target:receiveDamage(self.damage)
self:changeState(STATE_IDLE)
soundManager:playSfx("laser", 0.2)
else
self.attacking_timer = self.attacking_timer + 1
end
end
function Gunner:shoot()
table.insert(gameworld_projectiles,
Projectile(8,
self.position.x,
self.position.y,
self.target.position.x,
self.target.position.y,
0.2,
self.loyalty))
end
function Gunner:seek_target()
self.target = nil
local closer = self.sight_distance
for i, dem in ipairs(self:getEnemiesList()) do
local distance = self.position:dist(dem.position)
if distance < closer and (not dem:isDead()) then
closer = distance
self.target = dem
end
end
end
return Gunner