Another Basic Problem Door Open/Shut

Blitz3D Forums/Blitz3D Programming/Another Basic Problem Door Open/Shut

Some advice:
1) NEVER USE GOTO - NEVER !

2) use a status to handle the door.

For example, when your door is closed, its status is D_CLOSED. When the door is in this status, and the player is near the door and press a key, then change the status to D_OPENING.

While in the status D_OPENING, open the door and continue to do so until the door is completely opened. When this is achieved, then change again the door status with D_OPENED; and so on.
Example:
;const values for door status
const D_OPENING = 1
const D_CLOSING = 2
const D_OPENED = 3
const D_CLOSED = 4

;main loop
while not keydown(1)
select door_status

case D_OPENING
;continue to open
opening_door()

case D_CLOSING
;continue to close
closing_door()

case D_CLOSED
;attempt to open the door
open_door()

case D_OPENED
;atempt to close the door
close_door()


end select
;end of main loop
wend
end

;===============================
function opening_door()
;===============================
If doory<91 Then 
doory=doory+1 
RotateEntity motellcd,0,doory,0
else
door_status = D_OPENED
endif

end function

;===============================
function closing_door()
;===============================
If doory2>0 Then 
doory2=doory2-1 
RotateEntity motellcd,0,doory2,0
else
door_status = D_CLOSED
endif
end function

;===============================
function open_door()
;===============================
If KeyHit(18) Then 
If EntityDistance#(p\entity,motellcd) < 20 
If EntityYaw# (motellcd) = 0 Then 
door_status = D_OPENING
endif
endif
endif
end function

;===============================
function close_door()
;===============================
If KeyHit(18) Then 
If EntityDistance#(p\entity,motellcd) < 20 then
If EntityYaw# (motellcd) = 0 Then 
door_status = D_CLOSING
endif
endif
endif
end function


Another advice is to use a type structure for your door, so to handle multiple doors having their proper flags:
type t_door
field status ;the door status
field speed# ;the speed used to move the door
field orientation ;x,y,z where the door should move to
field mesh ;the door model
field x#,y#,z# ;the position of the door in 3D world
end type
Global door.t_door

.
.
for door.t_door = each t_door
select door\status
case D_OPENING
opening_door(door)
.
.
.
next

function opening_door(door.t_door)
.
.
end function


Hope this has sense for you,
Sergio.