Basically it shows a
simple way to make stuff run away from an object. Use the arrow keys to move the sphere around and watch all of the little cubes run away! The cubes you hit will turn red. You can change how fast the cubes run with the cube_speed variable.
Enjoy
It should run in DBC and DBPro.
rem **********************************
rem
rem RUNAWAY CUBES!
rem
rem By Sixty Squares
rem
rem **********************************
rem Setup Demo
sync on
sync rate 60
autocam off
rem SET VARIABLES.
rem Set the cubes to run away at a pace of 1.
Cube_speed=2
rem Set the camera's follow height to 500.
Camera_Height=500
rem The player is object 1.
player=1
rem Set the number of cubes to 500.
numofcubes=500
rem Set the size of the matrix to 5000
MatSize=5000
rem Set the cubes' sight radius to 200
Sight_Radius=100
rem Make the grassy image for the matrix
create bitmap 1,100,100
for d = 1 to 1000
ink rgb(0,100+rnd(155),0),0
line rnd(100),rnd(100),rnd(100),rnd(100)
next d
get image 1,0,0,100,100
blur bitmap 1,3
delete bitmap 1
rem Make the matrix (meant to look like a terrain)
Make matrix 1,MatSize,MatSize,60,60
position matrix 1,MatSize/-2,0,Matsize/-2
prepare matrix texture 1,1,1,1
rem Make the player sphere
make object sphere player,20
rem Make the cubes that will run away from you
for e = 3 to numofcubes+2
make object cube e,5
position object e,rnd(500),0,rnd(500)
next e
rem Begin the loop
do
rem Print the instructions for the player.
ink rgb(255,0,0),0
set cursor 10,10
print "USE THE ARROW KEYS TO MOVE THE SPHERE."
set cursor 10,50
print "USE S/W to increase/decrease the camera's height."
rem Player movement
move object player,3*(upkey()-downkey())
yrotate object player,wrapvalue(ay#+3*(rightkey()-leftkey()))
x#=object position x(player)
y#=object position y(player)
z#=object position z(player)
ay#=object angle y(player)
rem Change camera height + position camera.
Camera_Height=Camera_Height+5*(keystate(31)-keystate(17))
if camera_Height<100 then Camera_Height=100
if camera_Height>900 then Camera_Height=900
position camera x#,Camera_Height,z#
xrotate camera 90
yrotate camera ay#
rem ================================================================================================================================
REM CONTROL CUBES
for e = 3 to numofcubes+2
rem Hide cubes that are not in the screen to increase speed of game
if object in screen(e)=0 then hide object e else show object e
rem Get the XYZ positions of the cubes
ex#=object position x(e)
ey#=object position y(e)
ez#=object position z(e)
rem If the player gets too close to the cubes...
IF DIST(player,e)<=Sight_Radius
rem Make the cube look at the player (this won't be seen because we change it right afterwards
point object e,x#,ey#,z#
rem Now, make the cube look in the opposite direction that he was looking in. This will be away from the player
yrotate object e,wrapvalue(object angle y(e)-180)
rem Make the cubes run away. Cube_speed is the variable that decides how fast the cubes run. This can be changed at the top.
move object e,cube_speed
endif
rem Update positions
ex#=object position x(e)
ey#=object position y(e)
ez#=object position z(e)
rem Make sure the cubes don't leave the matrix.
if ex#>MatSize/2 then ex#=MatSize/2
if ez#>MatSize/2 then ez#=MatSize/2
if ex#<MatSize/-2 then ex#=MatSize/-2
if ez#<MatSize/-2 then ez#=MatSize/-2
rem Update the cube's new position (it will only change if the cube was found leaving the matrix.
position object e,ex#,ey#,ez#
rem Turn red if you're caught by the player....
IF OBJECT COLLISION(player,e)
color object e,rgb(255,0,0)
endif
next e
sync
loop
rem ===============================================================================================
rem Distance function. Used for finding the distance between two objects.
function dist(a,b)
d#=sqrt((object position x(a)-object position x(b))^2+(object position z(a)-object position z(b))^2)
endfunction d#
Version 2.0-- the cubes go back to their original positions!
rem **********************************
rem
rem RUNAWAY CUBES!
rem
rem By Sixty Squares
rem
rem **********************************
rem Setup Demo
sync on
sync rate 60
autocam off
rem SET VARIABLES.
rem Set the cubes to run away at a pace of 2.
Cube_speed=2
rem Set the camera's follow height to 500.
Camera_Height=500
rem The player is object 1.
player=1
rem Set the number of cubes to 500.
numofcubes=500
rem Set the size of the matrix to 5000
MatSize=5000
rem Set the cubes' sight radius to 200
Sight_Radius=100
rem Set the factor which by the cubes' speed will be reduced when they are 'walking' back to their original spots. We'll set it to 40%.
Speed_Factor#=0.40
rem Make an array to hold the cubes' original positions
DIM Cubes#(NumOfCubes+2,2)
remstart
WHAT EACH ARRAY INDEX IS
1=The cube's x position.
2=The cube's z position.
remend
rem Make the grassy image for the matrix
create bitmap 1,100,100
for d = 1 to 1000
ink rgb(0,100+rnd(155),0),0
line rnd(100),rnd(100),rnd(100),rnd(100)
next d
get image 1,0,0,100,100
blur bitmap 1,3
delete bitmap 1
rem Make the matrix (meant to look like a terrain)
Make matrix 1,MatSize,MatSize,60,60
position matrix 1,MatSize/-2,0,Matsize/-2
prepare matrix texture 1,1,1,1
rem Make the player sphere
make object sphere player,20
rem Make the cubes that will run away from you
for e = 3 to numofcubes+2
make object cube e,5
position object e,rnd(700),0,rnd(700)
rem Store the cubes' original positions in an array.
Cubes#(e,1)=object position x(e)
Cubes#(e,2)=object position z(e)
next e
rem Begin the loop
do
rem Print the instructions for the player.
ink rgb(255,0,0),0
set cursor 10,10
print "USE THE ARROW KEYS TO MOVE THE SPHERE."
set cursor 10,50
print "USE S/W to increase/decrease the camera's height."
rem Player movement
move object player,3*(upkey()-downkey())
yrotate object player,wrapvalue(ay#+3*(rightkey()-leftkey()))
x#=object position x(player)
y#=object position y(player)
z#=object position z(player)
ay#=object angle y(player)
rem Change camera height + position camera.
Camera_Height=Camera_Height+5*(keystate(31)-keystate(17))
if camera_Height<100 then Camera_Height=100
if camera_Height>900 then Camera_Height=900
position camera x#,Camera_Height,z#
xrotate camera 90
yrotate camera ay#
rem ================================================================================================================================
REM CONTROL CUBES
for e = 3 to numofcubes+2
rem Hide cubes that are not in the screen to increase speed of game
if object in screen(e)=0 then hide object e else show object e
rem Get the XYZ positions of the cubes
ex#=object position x(e)
ey#=object position y(e)
ez#=object position z(e)
rem If the player gets too close to the cubes...
IF DIST(player,e)<=Sight_Radius
rem Make the cube look at the player (this won't be seen because we change it right afterwards
point object e,x#,ey#,z#
rem Now, make the cube look in the opposite direction that he was looking in. This will be away from the player
yrotate object e,wrapvalue(object angle y(e)-180)
rem Make the cubes run away. Cube_speed is the variable that decides how fast the cubes run. This can be changed at the top.
move object e,cube_speed
ENDIF
rem If the cubes are far enough away from the player... (I add Cube_Speed*Speed_Factor# because that is how fast they move back to their positions.---
rem if I didn't do this then the cubes would look like they were vibrating, which is BAD.)
IF DIST(player,e)>Sight_Radius + Cube_Speed * Speed_Factor
rem Make the cube look at its original position
point object e,Cubes#(e,1),ey#,Cubes#(e,2)
rem Now, if the cube isn't aready at its original position (or close enough to it)..
if PointDist( ex# , ez# , Cubes#(e,1) , cubes#(e,2) ) > Cube_Speed * Speed_Factor#
rem then make it go to its original position.
move object e,Cube_Speed * Speed_Factor#
endif
endif
rem Update positions
ex#=object position x(e)
ey#=object position y(e)
ez#=object position z(e)
rem Make sure the cubes don't leave the matrix.
if ex#>MatSize/2 then ex#=MatSize/2
if ez#>MatSize/2 then ez#=MatSize/2
if ex#<MatSize/-2 then ex#=MatSize/-2
if ez#<MatSize/-2 then ez#=MatSize/-2
rem Update the cube's new position (it will only change if the cube was found leaving the matrix.
position object e,ex#,ey#,ez#
rem Turn red if you're caught by the player....
IF OBJECT COLLISION(player,e)
color object e,rgb(255,0,0)
endif
next e
sync
loop
rem ===============================================================================================
rem Distance function. Used for finding the distance between two objects.
function dist(a,b)
d#=sqrt((object position x(a)-object position x(b))^2+(object position z(a)-object position z(b))^2)
endfunction d#
rem Another Distance Function. Used for finding the distance between two points.
function PointDist(x1#,z1#,x2#,z2#)
d#=sqrt((x1#-x2#)^2+(z1#-z2#)^2)
endfunction d#