-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmicro.fs
145 lines (117 loc) · 2.99 KB
/
micro.fs
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
\ micro.fs
\ this is a Forth/Ficl/Retro-40 port
\ of my S7/Scheme/lambda8 port
\ of Matt Hughson's (@mhughson) TIC-80 port
\ of his Pico-8 Micro Platformer Starter Kit tech demo
\ the original introduction, complete with
\ its header, follows:
\ title: Micro-Platformer Starter Kit
\ author: Matt Hughson (@mhughson)
\ desc: Platforming engine in ~100 lines of code.
\ script: lua
\ the goal of this cart is to
\ demonstrate a very basic platforming
\ engine in under 100 lines of *code*,
\ while still maintaining an organized
\ and documented game.
\
\ it isn't meant to be a demo of doing
\ as much as possible, in as little
\ code as possible. the 100 line limit
\ is just meant to encourage people
\ that "hey, you can make a game' with
\ very little coding!"
\
\ this will hopefully give new users a
\ simple and easy to understand
\ starting point for their own
\ platforming games.
\
\ NOTE: collision routine is based on
\ mario bros 2 and mckids, where we use
\ collision points rather than a box.
\ this has some interesting bugs but if
\ it was good enough for miyamoto, its
\ good enough for me!
17 ficl-vocabulary micro-platformer
also micro-platformer definitions
0 DEFINE-SFX jump.wav
: solid? ( tile# -- f ) DUP 1 = SWAP 2 = OR ;
0.1e FCONSTANT gravity
VARIABLE x
VARIABLE y
VARIABLE dx
VARIABLE dy
VARIABLE grounded?
VARIABLE jump-velocity
\ moving sideways
: walk ( -- )
0e dx F!
SCANCODE_A pressed? IF -1e dx F! THEN
SCANCODE_D pressed? IF 1e dx F! THEN
;
: destination-h ( -- )
x F@ F>S dx F@ F0> IF 7 ELSE 0 THEN + 8 /
y F@ F>S 7 + 8 /
m@
;
VARIABLE startx
: move ( -- ) x F@ startx F! dx F@ x F+! ;
: ?pushback ( -- ) destination-h solid? IF startx F@ x F! THEN ;
\ moving downwards
: accumulate-gravity ( -- ) gravity dy F+! ;
: fall ( -- ) dy F@ y F+! ;
: destination-down ( -- )
FALSE grounded? !
x F@ F>S 4 + 8 /
y F@ F>S 8 + 8 /
m@
;
: ?hit-floor ( -- )
dy F@ 0e F>= destination-down solid? AND
IF
y F@ 8e F/ F>S 8 * S>F y F!
0e dy F!
TRUE grounded? !
THEN
;
\ moving upwards
: ?jump ( -- )
SCANCODE_W just-pressed? SCANCODE_SPACE just-pressed? OR
grounded? @ AND
IF dy F@ jump-velocity F@ F- dy F! 0 sfx THEN
;
: destination-up ( -- )
x F@ F>S 4 + 8 /
y F@ F>S 8 /
m@
;
: ?hit-ceiling ( -- )
dy F@ 0e F<= destination-up solid? AND
IF
y F@ 8e F+ 8e F/ F>S 8 * S>F y F!
0e dy F!
THEN
;
\ initialization
: init-player ( -- )
24e x F!
24e y F!
0e dx F!
0e dy F!
0 grounded? !
3.8e jump-velocity F!
;
\ hooks
: <init> ( -- )
s" micro.spr" load-sprites
s" micro.map" load-map
init-player
;
: ?exit ( -- ) SCANCODE_Q pressed? IF retro-40 THEN ;
: <update> ( -- ) ?jump walk move ?pushback accumulate-gravity fall ?hit-floor ?hit-ceiling ?exit ;
: <draw> ( -- ) 0 cls 0 0 map x F@ F>S y F@ F>S 0 spr s" v1.0 2016 - @matthughson" 15 0 0 PUTS ;
PREVIOUS DEFINITIONS
ALSO micro-platformer
INSTALL micro
PREVIOUS