-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample_-_pattern movement.lua
More file actions
79 lines (64 loc) · 1.82 KB
/
Example_-_pattern movement.lua
File metadata and controls
79 lines (64 loc) · 1.82 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
-- Pattern Movement
-- enum the instruction
DIR = {
NORTH = 1,
EAST = 2,
SOUTH = 4,
WEST = 5,
NORTHEAST = 6, -- not implemented,,
SOUTHEAST = 7,
SOUTHWEST = 8,
NORTHWEST = 9
}
-- the self class
npc = class()
-- init function of the self class
function npc:init(x,y)
self.active = true
self.position = vec2(320,200)
self.width = 32
self.height = 32
self.target = vec2(320,200)
self.speed = 2
self.path={DIR.EAST,DIR.SOUTH,DIR.WEST,DIR.NORTH}
self.pos = 1
end
-- create the self
player=npc(320,100)
function npc:update()
local angle = math.atan2(self.position.y-self.target.y,self.position.x-self.target.x)
self.position.x = self.position.x - math.cos(angle)
self.position.y = self.position.y - math.sin(angle)
-- if close to target, find next target
if self.position:dist(self.target)<5 then
self.pos = self.pos + 1
if self.pos > #self.path then self.pos = 0 end
if self.path[self.pos]==DIR.EAST then
self.target.x = self.target.x + 64
end
if self.path[self.pos]==DIR.SOUTH then
self.target.y = self.target.y + 64
end
if self.path[self.pos]==DIR.WEST then
self.target.x = self.target.x - 64
end
if self.path[self.pos]==DIR.NORTH then
self.target.y = self.target.y - 64
end
end
end
-- Use this function to perform your initial setup
function setup()
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
-- This sets the line thickness
strokeWidth(5)
-- move the self
player:update()
-- draw the pkayer
rect(player.position.x,player.position.y,player.width,player.height)
-- Do your drawing here
end