diff options
| -rw-r--r-- | sneed.p8 | 47 |
1 files changed, 21 insertions, 26 deletions
| @@ -9,6 +9,8 @@ pl = { | |||
| 9 | ["a"] = 0.7, | 9 | ["a"] = 0.7, |
| 10 | } | 10 | } |
| 11 | 11 | ||
| 12 | sprite_size = 8 | ||
| 13 | |||
| 12 | -- low quality raycast | 14 | -- low quality raycast |
| 13 | -- returns x, y :: the coordinates of the tile that was hit | 15 | -- returns x, y :: the coordinates of the tile that was hit |
| 14 | function shit_cast(x, y, dx, dy) | 16 | function shit_cast(x, y, dx, dy) |
| @@ -74,45 +76,38 @@ function cast(x, y, a) | |||
| 74 | end | 76 | end |
| 75 | 77 | ||
| 76 | -- returns | 78 | -- returns |
| 77 | -- + dist :: distance to the hit | 79 | -- + dist :: distance to the hit |
| 78 | -- + tile :: tile index | 80 | -- + sprite :: index in spritemap |
| 79 | -- + col :: tile column index | ||
| 80 | function ray(x, y, a) | 81 | function ray(x, y, a) |
| 81 | local tx, ty, lx, ly, dist = cast(x, y, a) | 82 | local tx, ty, lx, ly, dist = cast(x, y, a) |
| 82 | local tile = mget(tx, ty) | 83 | local sprite = mget(tx, ty) |
| 83 | 84 | ||
| 84 | if lx == 0 then | 85 | if lx == 0 then |
| 85 | return dist, tile, 1-ly | 86 | return dist, sprite_size * (sprite + 1 - ly) |
| 86 | elseif lx == 1 then | 87 | elseif lx == 1 then |
| 87 | return dist, tile, ly | 88 | return dist, sprite_size * (sprite + ly) |
| 88 | elseif ly == 0 then | 89 | elseif ly == 0 then |
| 89 | return dist, tile, lx | 90 | return dist, sprite_size * (sprite + lx) |
| 90 | else | 91 | else |
| 91 | return dist, tile, 1-lx | 92 | return dist, sprite_size * (sprite + 1 - lx) |
| 92 | end | 93 | end |
| 93 | end | 94 | end |
| 94 | 95 | ||
| 96 | function _init() | ||
| 97 | -- I use index 0 (black) in my sprites | ||
| 98 | palt(0, false) | ||
| 99 | end | ||
| 95 | 100 | ||
| 96 | function _draw() | 101 | function _draw() |
| 97 | cls(12) | 102 | cls(12) |
| 98 | local col | 103 | rectfill(0, 64, 128, 128, 3) |
| 99 | for col=0,1-1/128,1/128 do | 104 | local x |
| 100 | local c,d,da,h,x,y,t | 105 | for x = 0, 127 do |
| 101 | da = col*fov-fov/2 | 106 | local da = x * fov / 128 - fov / 2 |
| 102 | d,t,c = ray(pl.x,pl.y,pl.a+da) | 107 | local dist, sprite = ray(pl.x, pl.y, pl.a + da) |
| 103 | c *= 8 | 108 | local height = 128 / (dist * cos(da) + 0.1) |
| 104 | d = d*cos(da) | 109 | local y = (128 - height) / 2 |
| 105 | h = flr(128/d) | 110 | sspr(sprite, 0, 1, sprite_size, x, y, 1, height) |
| 106 | x = 128*col | ||
| 107 | y = (128-h)/2 | ||
| 108 | for row=0,1-1/8,1/8 do | ||
| 109 | local tc, sy, ey | ||
| 110 | tc = sget(8*t+c,row*8) | ||
| 111 | sy = y+row*h | ||
| 112 | ey = y+(row+1/8)*h | ||
| 113 | line(x,sy,x,ey,tc) | ||
| 114 | end | ||
| 115 | line(x, h+y, x, 128, 3) | ||
| 116 | end | 111 | end |
| 117 | 112 | ||
| 118 | ?"cpu "..(stat(1)*100).."%",0,0,7 | 113 | ?"cpu "..(stat(1)*100).."%",0,0,7 |