Author Topic: MoveTo class that can slide along boundaries (SCI0)  (Read 158 times)

0 Members and 1 Guest are viewing this topic.

Offline troflip

MoveTo class that can slide along boundaries (SCI0)
« on: February 02, 2017, 08:31:29 PM »
(SCI 1.1 doesn't really need this since it has true pathfinding).

This "SliderMoveTo" can be used in place of the "MoveTo" class. If the client hits a boundary, it'll try to adjust its direction a bit to move past it. It's kind of like an Avoider, except it's more predictable and not nearly as drastic (no back-tracking, or anything like that). It's basically just useful for the ego not getting "stuck" when moving along a wall that isn't perfectly aligned to their direction.

edit: Original version has some bugs, should be fixed now.

Code: [Select]

; How much we turn left or right trying to get around an obstacle.
(define SLIDE_ANGLE_OFFSET 40)
; How many times we can turn the above amount when trying to get around an obstacle.
(define NUM_SIDE_STEPS 2)

(procedure (ReinitSliderMove angleOffset &tmp theDX theDY newX newY theClient theLooper theHeading)

(= theClient (self client?))
(= theDX (- (self xOrig?) (theClient x?)))
(= theDY (- (self yOrig?) (theClient y?)))

; x = x * costheta - y sintheta
; y = x * sintheta + y costheta
(= newX (+ (- (CosMult angleOffset theDX) (SinMult angleOffset theDY)) (theClient x?)))
(= newY (+ (+ (SinMult angleOffset theDX) (CosMult angleOffset theDY)) (theClient y?)))

(self x: newX y: newY)
(= theHeading (GetAngle (theClient x?) (theClient y?) newX newY))
(theClient
heading: theHeading
)
(if (theClient looper?)
(= theLooper (theClient looper?))
(theLooper doit: theClient theHeading)
else
(DirLoop theClient theHeading)
)
(InitBresen self)
)

(class SliderMoveTo of Obj
(properties
client 0
caller 0
x 0
y 0
dx 0
dy 0
b-moveCnt 0
b-i1 0
b-i2 0
b-di 0
b-xAxis 0
b-incr 0
completed 0
xLast 0
yLast 0
; The original direction we were trying to go:
xOrig 0
yOrig 0
; The last direction we had to turn (1 or -1)
lastDirection -1
)

(method (init theClient theX theY theCaller &tmp theCycler)
(if (>= argc 1)
(= client theClient)
(if (>= argc 2)
(= xOrig theX)
(if (>= argc 3)
(= yOrig theY)
(if (>= argc 4) (= caller theCaller))
)
)
)
(= completed FALSE)
(= b-moveCnt 0)
(= xLast 0)
(= yLast 0)
(= theCycler (client cycler?))
(if theCycler (theCycler cycleCnt: 0))
(ReinitSliderMove 0)
)

(method (doit &tmp reinitAfter numSideSteps angleOffset multiplier)
(if (== b-moveCnt (client moveSpeed?))
(= xLast (client x?))
(= yLast (client y?))
)
(= reinitAfter FALSE)

(DoBresen self)
(= numSideSteps 1)
(= multiplier lastDirection)
(while (and (<= numSideSteps NUM_SIDE_STEPS) (client isStopped:))
(= reinitAfter TRUE)
; Couldn't move - try to the left and right a little
(ReinitSliderMove (* multiplier SLIDE_ANGLE_OFFSET numSideSteps))
(DoBresen self)
(*= multiplier -1)
(if (== multiplier lastDirection)
(++ numSideSteps)
)
)

(if (and (== x (client x?)) (== y (client y?)))
; Ok, we *really* couldn't go anywhere.
(self moveDone:)
(= reinitAfter FALSE)
)

(if reinitAfter
; reset to going straight for next time, so we actually make progress.
(ReinitSliderMove 0)
(= lastDirection (- multiplier)) ; since we toggle it before heading out of the loop
else
;(= lastDirection -1) ; reset, I guess
; Actually no, because this object may have been destroyed in moveDone:
)
)

(method (moveDone)
(= completed TRUE)
(if caller
(= gCastMotionCue TRUE)
else
(self motionCue:)
)
)

(method (setTarget newX newY)
(if argc (= x newX) (= y newY))
)

(method (onTarget)
(if
(and
(<= (Abs (- (client x?) x)) (client xStep?))
(<= (Abs (- (client y?) y)) (client yStep?))
)
(return TRUE)
)
)

(method (motionCue)
(client mover: NULL)
(if (and completed (IsObject caller)) (caller cue:))
(self dispose:)
)
)
« Last Edit: February 03, 2017, 03:41:10 PM by troflip »


Check out my website: http://icefallgames.com
Groundhog Day Competition

Offline MusicallyInspired

Re: MoveTo class that can slide along boundaries (SCI0)
« Reply #1 on: February 03, 2017, 09:20:43 AM »
Oooo.
Brass Lantern Prop Competition

Offline troflip

Re: MoveTo class that can slide along boundaries (SCI0)
« Reply #2 on: February 03, 2017, 03:41:23 PM »
Updated the original with some bug fixes.
Check out my website: http://icefallgames.com
Groundhog Day Competition


SMF 2.0.11 | SMF © 2015, Simple Machines
Simple Audio Video Embedder

Page created in 0.166 seconds with 21 queries.