Submission to challenge 12. No rights reserved.
; Avoidance AI demo done by u9 Mar 2008
; u9 AT kallnet D0T fo
; translated to PlayBasic by Shawn Hartnell
Constant DIST_LIMIT = 150
Constant AVOID_POWER = 4
Global ScreenHeight = GetScreenHeight()
Global ScreenWidth = GetScreenWidth()
Type object
x#
y#
EndType
Dim player As object
Dim ai As object
LoadFont "courier new",2,20,1
SetFont 2
Dim label As object
Global labelTime = 0
Global labelIdx = RndRange(0,3)
Dim labels$(3)
labels$(0) = "auch!"
labels$(1) = "stop that!"
labels$(2) = "go away"
labels$(3) = "not tonight"
SetFPS 60
OpenScreen 800,600,32,0
Do
;player is mouse controlled
player.x# = MouseX()
player.y# = MouseY()
AvoidObj( ai.object, player.object )
; Keep the little helpless circle inside the screen
If ai.x# < 20 Then ai.x# = 20
If ai.x# > ScreenWidth - 20 Then ai.x# = ScreenWidth - 20
If ai.y# < 20 Then ai.y# = 20
If ai.y# > ScreenHeight - 20 Then ai.y# = ScreenHeight - 20
; Just for fun, let player know he caught poor circle
If Distance( player.x# - ai.x#, player.y# - ai.y# ) < 20 And labelTime = 0
label.x# = ai.x#
label.y# = ai.y#
labelTime = 60
labelIdx = RndRange(0,3) ; what text to display
EndIf
if labelTime > 0 then labelTime = labelTime - 1
Cls 0
CircleC ai.x#, ai.y#, 10, False, ARGB(255,255,0,255)
BoxC player.x#-10, player.y#-10, player.x#+10, player.y#+10, False, ARGB(255,0,0,255)
if labelTime > 0
Text label.x#, label.y#, labels$(labelIdx)
endif
;Call graphics.settext( labels(labelIdx), label.x, label.y, font, argb(labelTime, 255, 255, 0) )
Sync
Loop
PSub AvoidObj( pray.object, hunter.object)
x# = pray.x# - hunter.x#
y# = pray.y# - hunter.y#
n# = Distance( x#, y# )
; If hunter too close
If n# < DIST_LIMIT and n# > 0
; Change d into level of danger (0 to 1)
d# = ( DIST_LIMIT-n# ) / DIST_LIMIT
pray.x# = pray.x# + x#/n# * d# * AVOID_POWER
pray.y# = pray.y# + y#/n# * d# * AVOID_POWER
EndIf
EndPSub
Function Distance( x#, y# )
n# = sqrt( x#*x# + y#*y# )
EndFunction n#