-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathranger.lua
More file actions
executable file
·55 lines (49 loc) · 1.49 KB
/
ranger.lua
File metadata and controls
executable file
·55 lines (49 loc) · 1.49 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
Ranger = {}
-- Constructor
function Ranger:new()
require 'character'
local char = Character:new()
local object = {
name = 'ranger',
image = love.graphics.newImage('img/player/knightblue.png'),
bullet_x = 0,
bullet_y = 0,
shooting = false,
target = {0,0}
}
setmetatable(self, {__index = char })
setmetatable(object, {__index = Ranger})
return object
end
function Ranger:draw()
Character.draw(self)
if self.shooting then
love.graphics.draw(self.bulletImage, self.bullet_x, self.bullet_y)
self:moveBullet(dt)
end
end
---------------------------------------
---------------------------------------
-- Shoot to target
function Ranger:shoot(mouseOverTile)
self.target = pixel_position(mouseOverTile[1] + 0.5, mouseOverTile[2] + 0.5)
self.bullet_x, self.bullet_y = (self.x + 0.5)*TILE_PIXEL_SIZE, (self.y + 0.5)*TILE_PIXEL_SIZE
self.shooting = true
end
-- bullet movement
function Ranger:moveBullet(dt)
if tile_distance({self.bullet_x, self.bullet_y}, self.target) > 0 then
if self.bullet_x < self.target[1] then
self.bullet_x = self.bullet_x + 5
elseif self.bullet_x > self.target[1] then
self.bullet_x = self.bullet_x - 5
end
if self.bullet_y < self.target[2] then
self.bullet_y = self.bullet_y + 5
elseif self.bullet_y > self.target[2] then
self.bullet_y = self.bullet_y - 5
end
else
self.shooting = false
end
end