It was necessary to store all of the TestLevel1Data into an array so that you can check to see how far the enemies can travel and still be over a platform. The SetupEnemies subroutine was created so that you can store the data necessary for moving the enemies.
You could take this a step further and create random placements for the enemies by looking at the TLA array and placing a 2 where a 0 exists above a 1 (make sense?) You can also put into the MoveEnemies subroutine a check where if they run into another enemy, they change directions.
This code includes some simple enemy movement:
Rem ***** Main Source File *****
sync on : sync rate 60
rem Setup display
set display mode 800,600,32
autocam off : hide mouse
`print "Welcome to Minamalist Mario"
`print "Press a button to start playing!"
`wait key
backdrop on : color backdrop rgb(0,0,150)
set ambient light 60 : color ambient light 50
dim bullet(6,6) : dim enemy(100,10) : dim TLA(134,12)
gosub InitVars
gosub MakeObjects
gosub CreateLevel
if TotalEnemies > 0 then gosub SetupEnemies
score=0:lives=3
rem Variables
Right = 90
Left = 270
Direction = Right
Jump = 0
rem Main loop
do
`Print Score Level and Lives on screen
set cursor 10,10: Print "SCORE:";Score; " LIVES:";lives
gosub PlayerInput
gosub MoveBullets
gosub MoveEnemies
rem Control camera
position camera x#,y#,z#-250
point camera x#,y#,z#
`gosub Debug : ` show details
rem Update screen
sync
rem End main loop
loop
end
` *********************************** Subroutines ************************************
PlayerInput:
rem Store old variables
oldx# = x#
oldy# = y#
upk = upkey()
rem Control character
if upk = 0
PressJump=0
else
if upk = 1 and Jump = 0 and PressJump = 0
if OnFloor > 0
PressJump = 1
Jump = 110
endif
endif
endif
if upk = 1 and OnFloor = 2 and PressJump = 1
y# = y# + 2
endif
if Jump > 0
dec Jump
y# = y# + 2
endif
if rightkey()=1
Direction=Right
x#=x#+1
endif
if leftkey()=1
Direction=Left
x#=x#-1
endif
y#=y#-1.0
position object player,x#,oldy#,z#
if object collision(1,0) >= 1000
dec x#,get object collision x()
inc y#,get object collision y()
endif
position object player,x#,y#,z#
if object collision(1,0)>= 1000 : ` check collision with world
OnFloor=1
if y#<object position y(object collision(1,0))
if upkey()=1
OnFloor=2
else
OnFloor=0
endif
endif
y#=oldy#
else
OnFloor=0
endif
position object 1,x#,y#,z#
yrotate object player,curveangle(Direction,object angle y(player),50)
if spacekey() = 1 and timer() > BulletTimer then gosub FireBullet
return
FireBullet:
bullet(CurrBullet,1) = object position x(player)
bullet(CurrBullet,2) = object position y(player)
bullet(CurrBullet,3) = object position z(player)
bullet(CurrBullet,4) = 1 : ` status = active
bullet(CurrBullet,5) = 600 + CurrBullet : ` object #
x# = object position x(player)
y# = object position y(player)
z# = object position z(player)
position object bullet(CurrBullet,5),x#,y#,z#
show object bullet(CurrBullet,5)
bullet(CurrBullet,6) = 8 : ` moving right
if direction = left then bullet(CurrBullet,6) = -8 : ` moving left
inc CurrBullet : if CurrBullet > 6 then CurrBullet = 0
BulletTimer = timer() + 200
return
MoveBullets:
for k = 0 to 6
if bullet(k,4) = 1 : ` is it active?
bullet(k,1) = bullet(k,1) + bullet(k,6) : ` move it
if object in screen(bullet(k,5)) = 1
` check for collision
collide = object collision(bullet(k,5),0)
if collide >= 1000
` collision with the world
hide object bullet(k,5)
bullet(k,4) = 0 : ` status = inactive
endif
` check collision with enemies
if collide >= 400 and collide <= (400 + TotalEnemies)
if enemy(collide - 400,4) = 1 : ` is it still active?
` yes, got an enemy!
enemy(collide - 400,4) = 0 : ` status = inactive
hide object enemy(collide - 400,5)
` hide the bullet and change it's status
hide object bullet(k,5)
bullet(k,4) = 0 : ` status = inactive
Score = Score+1
else
` enemy has already been destroyed - move the bullet
position object bullet(k,5),bullet(k,1),bullet(k,2),bullet(k,3)
endif
endif
if collide = player or collide = 0 or (collide >= 600 and collide <= 606)
` re-position the bullet
position object bullet(k,5),bullet(k,1),bullet(k,2),bullet(k,3)
endif
else
` bullet off screen - hide it
hide object bullet(k,5)
bullet(k,4) = 0 : ` status = inactive
endif
endif
next k
return
MoveEnemies:
for t = 0 to TotalEnemies
if Enemy(t,4) = 1 : ` is it still alive?
Enemy(t,1) = Enemy(t,1) + Enemy(t,8)
if Enemy(t,1) < Enemy(t,6) or Enemy(t,1) > Enemy(t,7) then Enemy(t,8) = Enemy(t,8) * -1 : `reverse direction
position object Enemy(t,5),Enemy(t,1),Enemy(t,2),0.0
endif
next t
return
InitVars:
CurrBullet = 0
Lives = 3
Score = 0
player = 1
for i = 0 to 6
bullet(i,4) = 0 : ` reset status to inactive
bullet(i,5) = 600 + i
next i
for i = 0 to 100
enemy(i,4) = 0 : ` status = inactive
enemy(i,5) = 400 + i : ` object # -- use objects 400 to 500 for enemies
next i
BulletTimer = timer()
return
MakeObjects:
rem Create a player object
make object sphere player,25,40,25
make object collision box player,-12.5,-15,-12.5,12.5,15,12.5,0
color object player,rgb(255,0,0)
` make bullets
for k = 600 to 606
make object sphere k,3
color object k,rgb(255,0,0) : ` make them red
make object collision box k,-1.5,-1.5,-1.5,1.5,1.5,1.5,1
hide object k
next k
return
remstart
Debug:
rem Print whether the player is walking, jumping, falling or climbing
set cursor 0,0
if OnFloor=0
if Jump=0
print "Falling"
else
print "Jumping"
endif
endif
if OnFloor=1
print "On Foot"
endif
if OnFloor=2
print "Climbing"
endif
` text 10,100,"x# = " + str$(object position x(player))
` text 10,120,"y# = " + str$(object position y(player))
` text 10,140,"z# = " + str$(object position z(player))
remend
return
SetupEnemies:
for t = 0 to TotalEnemies
` look for blocks below to set left / right paths
x = Enemy(t,9) : y = enemy(t,10) : tempx = x : tempy = y
if y > 1 and y < 12 and x > 1 and x < 134 : ` make sure we don't go out of bounds
okay = 0
repeat
dec tempx
if TLA(tempx,tempy) = 1
okay = 1
endif
until okay = 1
MaxLeft = tempx + 1 : ` see how far left we can go before we hit a block
tempx = x
okay = 0
repeat
inc tempx
if TLA(tempx,tempy) = 1
okay = 1
endif
until okay = 1
MaxRight = tempx - 1 : ` furthest right w/o hitting a block
`now we will check underneath to make sure there is a platform to walk on
tempx = x
dec tempy
okay = 0
repeat
dec tempx
if TLA(tempx,tempy) = 0
okay = 1
endif
until okay = 1
xtu = MaxLeft : if (tempx + 1) > MaxLeft then xtu = (tempx + 1)
Enemy(t,6) = (xtu * 50) : ` furthest left it can go left
tempx = x
okay = 0
repeat
inc tempx
if TLA(tempx,tempy) = 0
okay = 1
endif
until okay = 1
xtu = MaxRight : if (tempx - 1) < MaxRight then xtu = (tempx - 1)
Enemy(t,7) = (xtu * 50) : ` max right
endif
` set random speed
Enemy(t,8) = rnd(3) + 1 : ` random amount - moving right
if rnd(100) > 50 then Enemy(t,8) = Enemy(t,8) * -1 : ` randomly start moving left
next t
return
CreateLevel:
rem Create a level
LevelObject = 1000 : EnemyObj = 400 : NumReads = 0
`for y=134 to 1 step -1
for y = 12 to 1 step -1
for x = 1 to 134
read TestLevel1Data
TLA(x,y) = TestLevel1Data
if TestLevel1Data=1
make object box LevelObject,50,50,50
position object LevelObject,x*50,y*50,0
make object collision box LevelObject,-25,-25,-25,25,25,25,0
color object LevelObject,rgb(0,150,0)
inc LevelObject
endif
if TestLevel1Data = 3
make object box 644,50,50,50
position object 644,x*50,y*50,0
make object collision box 644,-25,-25,-25,25,25,25,0
color object 644,rgb(0,0,150)
endif
if TestLevel1Data = 9
position object player,x*50,y*50,0
x# = x * 50
y# = y * 50
z# = 0
endif
if TestLevel1Data = 2
if object exist(EnemyObj) = 1 then delete object EnemyObj
make object cube EnemyObj,20
color object EnemyObj,rgb(139,69,19)
set object ambience EnemyObj,rgb(0,255,0)
make object collision box EnemyObj,-10.0,-10.0,-10.0,10.0,10.0,10.0,0
Enemy(EnemyObj - 400,1) = x * 50 : Enemy(EnemyObj - 400,2) = y * 50
position object EnemyObj,Enemy(EnemyObj - 400,1),Enemy(EnemyObj - 400,2),0
Enemy(EnemyObj - 400,4) = 1 : ` status = active
Enemy(EnemyObj - 400,9) = x : Enemy(EnemyObj - 400,10) = y : ` store map locations
Inc EnemyObj,1
endif
next x
next y
TotalEnemies = EnemyObj - 400
return
TestLevel1Data:
data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,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