pete286/entity.jor

106 lines
2.9 KiB
Plaintext
Raw Normal View History

0 const EVTICK
1 const EVTOUCH
: world>tile 4 >> swap 4 >> swap ;
: tile>world 4 << swap 4 << swap ;
2019-05-19 17:34:27 +00:00
: +pos ( x1 y1 x2 y2 -- x y )
<rot + >rot + swap ;
: -pos ( x1 y1 x2 y2 -- x y )
negate swap negate swap +pos ;
: defentity ( x y dir anim -- ) array ' drop , , , tile>world , , ;
: entity.x 4 cells + ;
: entity.y 3 cells + ;
: entity.dir 2 cells + ;
: entity>sprite cell + @ execute ;
: entity>do ( entity event ) swap @ execute ;
: entity>pos dup entity.x @ swap entity.y @ ;
: entity.pos! ( x y entity ) <rot over entity.x ! entity.y ! ;
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
2019-05-19 17:34:27 +00:00
4 const NODIR
: dir>pos ( dir -- dx dy )
dup W = if drop -1 0 ret then
dup E = if drop 1 0 ret then
2019-05-19 17:34:27 +00:00
dup N = if drop 0 -1 ret then
S = if 0 1 else 0 0 then ;
: pos>dir ( dx dy -- dir )
dup 0 < if drop drop N else
0 > if drop S else
dup 0 < if drop W else
0 > if E else NODIR then then then then ;
: facing ( x1 y1 x2 y2 -- dir ) -pos pos>dir ;
: entity-dst ( e -- x y )
>r r@ entity.dir @ dir>pos
r@ entity.x @ <r entity.y @ world>tile +pos ;
: move-entity ( e -- )
dup entity.dir @ dir>pos ( e dx dy )
dup if swap drop swap entity.y
else drop swap entity.x then
swap 16 * over @ + 4 <rot move-to ;
: 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
( 3: mary stand ) 17 20 22 24 frame
( 4: mary walk ) 19 21 23 25 frame
2019-06-26 01:58:52 +00:00
( 5: car lights ) 29 27 26 28 frame
2019-07-01 16:31:00 +00:00
( 6: jeanne stand ) 30 32 34 36 frame
( 7: jeanne walk ) 31 33 35 37 frame
( 8: boat w/ pete ) 42 41 40 39 frame
: sprindex ( dir frame ) 2 << frames + + b@ ;
: defstatic ( frame -- ) create b, does> b@ sprindex ;
: defsingle ( sprindex -- ) create b, does> swap drop b@ ;
: 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}
2019-06-26 01:58:52 +00:00
5 defstatic {car-lit}
1 defstatic {pete-stand}
1 2 2 5 defanim {pete-walk}
13 defsingle {pete-table}
14 defsingle {chair}
15 defsingle {pete-bed}
16 defsingle {horse}
3 defstatic {mary}
3 4 2 5 defanim {mary-walk}
2019-07-01 16:31:00 +00:00
6 defstatic {jeanne}
6 7 2 5 defanim {jeanne-walk}
2019-05-17 02:25:13 +00:00
18 defsingle {phone}
38 defsingle {fridge}
43 defsingle {boat}
8 defstatic {boat-pete}
: sprite-bob ( x y sprindex -- x y sprindex )
ticks 40 % 20 < if
dup 39 >= over 43 <= and if swap 1 + swap then
then ;