-
Notifications
You must be signed in to change notification settings - Fork 1
/
oop1.p8
201 lines (179 loc) · 5.15 KB
/
oop1.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
192
193
194
195
196
197
198
199
200
201
pico-8 cartridge // http://www.pico-8.com
version 27
__lua__
-- trying out oop
-- by apa64
-- consts
b = {
left = 0, -- ⬅️
right = 1, -- ➡️
up = 2, -- ⬆️
down = 3, -- ⬇️
fire1 = 4, -- 🅾️
fire2 = 5 -- ❎
}
c = {
black = 0,
dark_blue = 1,
dark_purple = 2,
dark_green = 3,
brown = 4,
dark_gray = 5,
light_gray = 6,
white = 7,
red = 8,
orange = 9,
yellow = 10,
green = 11,
blue = 12,
indigo = 13,
pink = 14,
peach = 15
}
local pixels = {}
function _init()
add(pixels, pixel:new({ vx = 0.3 }))
add(pixels, controlpixel:new({ x = 63, y = 63, color = c.red }))
splash1 = splash:new(
{
x = 23, y = 23, w = 80, h = 80,
color = c.dark_gray, text_color = c.orange,
text = "press x to start!",
game_start_function = "todo give game start point here!"
}
)
end
-- this is kind of game_update(), we need splash_update() for splash state
function _update()
-- do thing for each element in pixels
foreach(pixels,
function(self)
self:update()
end
)
end
--[[
function splash_draw()
cls()
splash1:draw()
end
--]]
function _draw()
cls()
splash1:draw()
-- same kind of thing as in update but with for and all
for p in all(pixels) do
p:draw()
end
end
-->8
-- ###################### entity
-- generic prototype object, kind of a class
_entity = {
-- member vars with default values
x = 0, -- left x
y = 0, -- top y
w = 0, -- width
h = 0 -- height
}
-- the constructor creates a new object
-- params: o is an initializer table
-- https://www.lexaloffle.com/bbs/?pid=21527#p
-- inheritance: https://www.lua.org/pil/16.2.html
function _entity:new(o)
local o = o or {} -- use initializer or create a new object if not provided
-- a table may have an associated table called metatable (mt). the fields of the metatable are used to look up if key is not in this table.
setmetatable(o, self)
-- __index indicates which table is accessed when a key is not found in this. in pixel:new() call self == pixel (prototype)
self.__index = self
return o
end
-- checks collision with another entity with x,y,w,h
function _entity:collide(other)
return other.x < self.x + self.w
and self.x < other.x + other.w
and other.y < self.y + self.h
and self.y < other.y + other.h
end
-->8
-- ####################### pixel
-- prototype: pixel, inherited from entity
pixel = _entity:new(
{
w = 1, -- width
h = 1, -- height
vx = 0, -- speed x
vy = 0, -- speed y
color = c.white
-- more: width, height, sprite, vmax, dvmax, friction/gravity...
}
)
-- draws the object
function pixel:draw()
pset(self.x, self.y, self.color)
end
-- updates the object
function pixel:update()
self:move()
-- check collisions
-- interact with env
end
-- calculates new position based on speed, wrap around screen
function pixel:move()
self.x = (self.x + self.vx) % 128
self.y = (self.y + self.vy) % 128
end
-- prototype: controllable pixel, inherit from pixel
controlpixel = pixel:new(
{
dv = 0.1, -- speed change per keypress
vmax = 1, -- max speed
friction = 0.96 -- 0.01 -- slow down per update
}
)
-- updates the object
function controlpixel:update()
-- todo check collisions
-- todo interact with env
self:input()
self:move()
-- slow down, bug: does not go quite to zero
--[[
if (self.vx != 0) self.vx = sgn(self.vx) * (abs(self.vx) - self.friction)
if (self.vy != 0) self.vy = sgn(self.vy) * (abs(self.vy) - self.friction)
-- ]]
self.vx *= self.friction
self.vy *= self.friction
end
-- handles button presses
function controlpixel:input()
if (btn(b.left) and abs(self.vx) < self.vmax) self.vx -= self.dv
if (btn(b.right) and abs(self.vx) < self.vmax) self.vx += self.dv
if (btn(b.up) and abs(self.vy) < self.vmax) self.vy -= self.dv
if (btn(b.down) and abs(self.vy) < self.vmax) self.vy += self.dv
end
-->8
-- ###################### splash
-- splash screen prototype
splash = _entity:new(
{
text = nil,
color = c.light_gray,
text_color = c.black
}
)
-- todo handle input to go to next state
function splash:update()
-- if (btnp(b.fire2)) game_start()
end
function splash:draw()
rectfill(self.x, self.y, self.x + self.w, self.y + self.h, self.color)
print(self.text, 63 - flr((#self.text - 1) * 4 / 2), 60, self.text_color)
end
__gfx__
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000