51 lines
1.3 KiB
Plaintext
51 lines
1.3 KiB
Plaintext
|
0 const EVTICK
|
||
|
1 const EVTOUCH
|
||
|
|
||
|
: defentity ( x y dir anim -- ) array ' drop , , , 4 << , 4 << , ;
|
||
|
: entity.x 4 cells + ;
|
||
|
: entity.y 3 cells + ;
|
||
|
: entity.dir 2 cells + ;
|
||
|
: entity>sprite cell + @ execute ;
|
||
|
: entity>do ( entity event ) swap @ execute ;
|
||
|
|
||
|
var entity-defstate
|
||
|
: entitydo-ev ( [cp ifhere] ev -- )
|
||
|
entity-defstate @ if swap [ ' then , ]
|
||
|
else 1 entity-defstate ! :noname swap then
|
||
|
' dup , lit ' = , [ ' if , ] ;
|
||
|
: :touch EVTOUCH entitydo-ev ; immediate
|
||
|
: :tick EVTICK entitydo-ev ; immediate
|
||
|
: ;entity ( entity cp ifhere -- )
|
||
|
[ ' then , ] ' drop , [ ' ; , ]
|
||
|
0 entity-defstate ! swap ! ; immediate
|
||
|
|
||
|
0 const W
|
||
|
1 const E
|
||
|
2 const N
|
||
|
3 const S
|
||
|
|
||
|
: dir>pos ( dir -- dx dy )
|
||
|
dup W = if drop -1 0 ret then
|
||
|
dup E = if drop 1 0 ret then
|
||
|
N = if 0 -1
|
||
|
else 0 1 then ;
|
||
|
|
||
|
: frame ( s n e w ) b, b, b, b, ;
|
||
|
array frames
|
||
|
( 0: car ) 3 1 0 2 frame
|
||
|
( 1: pete stand ) 11 9 7 5 frame
|
||
|
( 2: pete walk ) 12 10 8 6 frame
|
||
|
|
||
|
: sprindex ( dir frame ) 2 << frames + + b@ ;
|
||
|
: defstatic ( frame -- ) create b, does> b@ sprindex ;
|
||
|
: defanim ( frame... framecount ticks-per-frame -- )
|
||
|
create b, dup b, 0 for b, next
|
||
|
does> ( dir a -- )
|
||
|
dup dup 1 + b@ swap b@ ( dir a count tpf )
|
||
|
ticks swap / swap % ( dir a index )
|
||
|
2 + + b@ sprindex ;
|
||
|
|
||
|
0 defstatic {car}
|
||
|
1 defstatic {pete-stand}
|
||
|
1 2 2 5 defanim {pete-walk}
|