forked from rik-cross/pico-8-adventure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
part5-obstacles.p8
191 lines (168 loc) · 8.13 KB
/
part5-obstacles.p8
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
pico-8 cartridge // http://www.pico-8.com
version 16
__lua__
-- a table comtaining all game entities
entities = {}
function canwalk(x,y)
return not fget(mget(x/8,y/8),7)
end
-- creates and returns a new control component
function newcontrol(left,right,up,down,input)
local c = {}
c.left = left
c.right = right
c.up = up
c.down = down
c.input = input
return c
end
-- creates and returns a new intention component
function newintention()
local i = {}
i.left = false
i.right = false
i.up = false
i.down = false
return i
end
-- creates and returns a new position
function newposition(x,y,w,h)
local p = {}
p.x = x
p.y = y
p.w = w
p.h = h
return p
end
-- creates and returns a new sprite
function newsprite(x,y)
local s = {}
s.x = x
s.y = y
return s
end
-- creates and returns a new entity
function newentity(position,sprite,control,intention)
local e = {}
e.position = position
e.sprite = sprite
e.control = control
e.intention = intention
add(entities,e)
return e
end
function playerinput(ent)
ent.intention.left = btn(ent.control.left)
ent.intention.right = btn(ent.control.right)
ent.intention.up = btn(ent.control.up)
ent.intention.down = btn(ent.control.down)
end
function npcinput(ent)
ent.intention.left = true
end
controlsystem = {}
controlsystem.update = function()
for ent in all(entities) do
if ent.control ~= nil and ent.intention ~= nil then
ent.control.input(ent)
end
end
end
physicssystem = {}
physicssystem.update = function()
for ent in all(entities) do
local newx = ent.position.x
local newy = ent.position.y
if ent.position ~= nil and ent.intention ~= nil then
if ent.intention.left then newx -= 1 end
if ent.intention.right then newx += 1 end
if ent.intention.up then newy -= 1 end
if ent.intention.down then newy += 1 end
end
-- update x position if allowed to move
if canwalk(newx,ent.position.y) and
canwalk(newx,ent.position.y+ent.position.w-1) and
canwalk(newx+ent.position.h-1,ent.position.y) and
canwalk(newx+ent.position.h-1,ent.position.y+ent.position.w-1) then
ent.position.x = newx
end
-- update y position if allowed to move
if canwalk(ent.position.x,newy) and
canwalk(ent.position.x,newy+ent.position.w-1) and
canwalk(ent.position.x+ent.position.h-1,newy) and
canwalk(ent.position.x+ent.position.h-1,newy+ent.position.w-1) then
ent.position.y = newy
end
end
end
gs = {}
gs.update = function()
cls()
--centre camera on player
camera(-64+player.position.x+(player.position.w/2),
-64+player.position.y+(player.position.h/2))
map()
-- draw all entities with sprites and positions
for ent in all(entities) do
if ent.sprite ~= nil and ent.position ~= nil then
sspr(player.sprite.x, player.sprite.y,
player.position.w, player.position.h,
player.position.x, player.position.y)
end
end
camera()
--crosshair sprite
--spr(16,64-4,64-4)
end
function _init()
-- create a player entity
player = newentity(
-- create a position component
newposition(10,10,8,8),
-- create a sprite component
newsprite(8,0),
-- create a control component
newcontrol(0,1,2,3,playerinput),
-- create an intention component
newintention()
)
end
function _update()
--check player input
controlsystem.update()
-- move entities
physicssystem.update()
end
function _draw()
gs.update()
end
__gfx__
0000000000aaaa005555555533333333cccccccc6666666600000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000aaaaaa05555555533333333cccccccc6666666600000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700aa5aa5aa5555555533333333cccccccc6666666600000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000aaaaaaaa5555555533333333cccccccc6666666600000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000aaaaaaaa5555555533333333cccccccc6666666600000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700aa5555aa5555555533333333cccccccc6666666600000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000aaaaaa05555555533333333cccccccc6666666600000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000aaaa005555555533333333cccccccc6666666600000000000000000000000000000000000000000000000000000000000000000000000000000000
00088000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00088000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
88000088000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
88000088000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00088000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00088000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
__gff__
0000000080800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
__map__
0505050505050505050505050505050500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0502020203030303030303030303020500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0502020203030303030304040403020500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0502020203030303030303040403020500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0502020203030303030303030403020500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0502020203030303030303030403020500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0502020203030303030303030303020500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0502020202020202020202020202020500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0505050505050505050505050505050500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000