Post by bluatigro on Jun 24, 2018 3:45:33 GMT
a FSM can also be used to control NPC's in game
WindowWidth = 700
WindowHeight = 500
nomainwin
global red , green , orange
global north , west , south , east , bike
global frame , state , shift , clicked
dim x( 5 ) , y( 5 )
shift = 16
red = 1 * shift
green = 2 * shift
orange = 3 * shift
north = 0
west = 1
south = 2
east = 3
bike = 4
x( north ) = 200 : y( north ) = 0
x( west ) = 50 : y( west ) = 150
x( south ) = 200 : y( south ) = 300
x( east ) = 350 : y( east ) = 150
x( bike ) = 200 : y( bike ) = 150
state = north + red
frame = 2
clicked = 0
open "yrafic light sim" for graphics as #m
#m "trapclose [quit]"
#m "when leftBottonDown [leftbutton]"
call light north , red
call light west , red
call light south , red
call light east , red
call light bike , red
timer 250 , [tmr]
wait
[leftbutton]
clicked = abs( not( 0 ) )
wait
[tmr]
frame = frame - 1
select case state
case north + red
call light north , red
if frame < 0 then
frame = 30
state = north + green
end if
case north + green
call light north , green
if frame < 0 then
frame = 10
state = north + orange
end if
case north + orange
call light north , orange
if frame < 0 then
frame = 2
state = west + red
call light north , red
end if
case west + red
call light west , red
if frame < 0 then
frame = 30
state = west + green
end if
case west + green
call light west , green
if frame < 0 then
frame = 10
state = west + orange
end if
case west + orange
call light west , orange
if frame < 0 then
frame = 2
state = south + red
call light west , red
end if
case south + red
call light south , red
if frame < 0 then
frame = 30
state = south + green
end if
case south + green
call light south , green
if frame < 0 then
frame = 10
state = south + orange
end if
case south + orange
call light south , orange
if frame < 0 then
frame = 2
state = east + red
call light south , red
end if
case east + red
call light east , red
if frame < 0 then
frame = 30
state = east + green
end if
case east + green
call light east , green
if frame < 0 then
frame = 10
state = east + orange
end if
case east + orange
call light east , orange
if frame < 0 then
frame = 2
if clicked then
state = bike + red
clicked = 0
else
state = north + red
end if
call light east , red
end if
case bike + red
call light bike , red
if frame < 0 then
frame = 30
state = bike + green
end if
case bike + green
call light bike , green
if frame < 0 then
frame = 10
state = bike + orange
end if
case bike + orange
call light bike , orange
if frame < 0 then
frame = 2
state = north + red
call light bike , red
end if
case else
end select
wait
sub light no , clr
#m "up"
#m "goto " ; x( no ) ; " " ; y( no )
#m "color black"
#m "backcolor black"
#m "down"
#m "boxfilled " ; x( no ) + 30 _
; " " ; y( no ) + 90
#m "up"
select case clr
case red
#m "color red"
#m "backcolor red"
#m "goto " ; x( no ) + 15 ; " " ; y( no ) + 15
#m "down"
#m "circlefilled 15"
case orange
#m "color yellow"
#m "backcolor yellow"
#m "goto " ; x( no ) + 15 ; " " ; y( no ) + 45
#m "down"
#m "circlefilled 15"
case green
#m "color green"
#m "backcolor green"
#m "goto " ; x( no ) + 15 ; " " ; y( no ) + 75
#m "down"
#m "circlefilled 15"
case else
end select
#m "flush"
end sub
[quit]
close #m
end