The idea here is this routine goes from your current angle-5 to current angle +5, in increments of 1. The result is an 11-entry stack, with either $FF for nothing found, or the type ID and distance of the object found. The renderer will then parse that stack to update the screen.
Feel free to correct/optimize, but let me know what, if anything, I did wrong so I learn. Thanks!
Code:
Feel free to correct/optimize, but let me know what, if anything, I did wrong so I learn. Thanks!
Code:
#define distance tempSwapArea ; 1 byte
#define rayCurrent distance+1 ; 2 bytes
#define angleCurrent rayEnd+2 ; 1 byte
#define angleStart angleCurrent+1 ; 1 byte
#define angleStop angleStart+1 ; 1 byte
#define iterVector angleStop+1 ; 2 bytes
#define rcStack iterVector+2 ; 2 bytes, a pointer to the start of the raycaster object stack
#define renderDistance 10 ; adjust this to change the render distance
#define priorAngle rcStack+2 ; 1 byte
engine_raycaster_Init:
ld hl,smallEditCol
ld (rcStack),hl ; reset pointer to start of raycaster stack
ld a,(playerDirection)
ld hl,priorAngle
cp (hl)
jr z,{1@}
ld a,b
ld b,5
add a,b
ld (angleStop),a
ld b,-10
add a,b
ld (angleStart),a
@: ld a,(angleStart) ; sorry about this bit, but needs to be here for the jump to work
ld (angleCurrent),a ; shouldnt be too time-consuming to do that
rc_loop_angle:
call _getTempVector
ld a,(playerPosX)
ld h,a
ld a,(playerPosY)
ld l,a
ld bc,(iterVector)
rc_loop_ray:
add hl,bc
call rc_getDistance
ld (distance),a
ld a,l
multAbyWidth
ld c,h
ld b,0
add hl,bc
call rc_getMap
ld a,(hl)
or a
jr z,rc_tiletest_skip
; rc_tilepush: if tile is something, push this object onto rcStack, renderer will handle it
ld de,(rcStack)
ld (de),a ; object type Id
inc de
ld a,(distance)
ld (de),a
inc de
ld (rcStack),de
jr rc_skip_writeempty
rc_tiletest_skip:
ld a,(distance)
cp renderDistance
jr z,rc_loop_angle_next
jr c,rc_loop_angle_next
jr rc_loop_ray
rc_loop_angle_next:
ld de,(rcStack)
ld a,$ff
ld (de),a
inc de
ld (rcStack),de
rc_skip_writeempty:
ld a,(angleCurrent)
inc a
ld (angleCurrent),a
jr rc_loop_angle
rc_getTempVector:
; using current angle, gets vectors. each vector is in 8.8 FP format, so we only save the .8 part
; input: a = angle
; output: new vectors loaded into (iterVector) word
push af
call calcSin ; a = angle
ld (iterVector),l ; hl = 8.8 FP
pop af
call calcCos
ld (iterVector+1),l
ret
rc_getMap:
; input: hl = offset in map
; output: hl points to tile
ld bc,(SlendMapPtr)
add hl,bc
ret
rc_getDistance:
; input: hl = current testing location
; output: a = distance from origin
; this should do distance formula. pretend it does
ret