Hey guys,
Been working again a bit on my 2D shooter.
Ive gotten to the stage where I am putting multiple different types of enemies and projectiles on the screen at once.
At the moment I have been copying and pasting code each time I create a new enemy or projectile, and I just go through the code with the Find and replace function and replace all the old names with the new.
Now I know this isn't what I should be doing. I feel the whole point of using UDTs correctly would be you just use the one set of code for all projectiles, and stop copying and pasting and rewriting everything.
If someone could go through my code here I would be very grateful. Sorry its over a 1000 lines. I think if you jump to the projectiles function you will see a good example of what Im trying to say. I have basically copied and pasted all the code for bullets and relabeled it rockets. And it works but I'm sure there is a better way as the bullets and rockets both share the same UDT (projectiles).
Anyway I hope the question is clear enough.
Thanks in advance.
// Project: Elixir
// Created: 20-08-12
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "Elixir" )
SetWindowSize( 640, 480, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 640, 480 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 )
// Initiate the main menu
global mainmenu_spr as integer
mainmenu_spr=Loadimage("mainmenu.gif")
type mainmenu
active as integer
id as integer
screen as string
endtype
dim mainmenu[1] as mainmenu
mainmenu[1].id = createsprite(mainmenu_spr)
mainmenu[1].active=0
mainmenu[1].screen="menu"
setspriteposition(mainmenu[1].id,0,0)
setspritedepth(mainmenu[1].id,500)
global selector_spr as integer
selector_spr=loadimage("potionselect.png")
type selector
id as integer
position as integer
pressed as integer
endtype
dim selector[1] as selector
selector[1].id = createsprite(selector_spr)
setspritedepth(selector[1].id,499)
setspriteposition(selector[1].id,100,400)
selector[1].position=1
selector[1].pressed=0
mainmenu()
// mainmenu loop ******************************* MAINMENU FUNCTION *******************************************************
function mainmenu()
setspritevisible(mainmenu[1].id,1)
setspritevisible(selector[1].id,1)
while mainmenu[1].active=1
if getrawkeypressed(38)=1
dec selector[1].position
endif
if getrawkeypressed(40)=1
inc selector[1].position
endif
if selector[1].position<1 then selector[1].position=4
if selector[1].position>4 then selector[1].position=1
if selector[1].position=1
if getrawkeystate(32)=1
mainmenu[1].active=0
endif
endif
if selector[1].position=4
if getrawkeystate(32)=1
end
endif
endif
setspriteposition (selector[1].id,150,200+45*selector[1].position)
sync()
endwhile
if mainmenu[1].active=0
setspritevisible(mainmenu[1].id,0)
setspritevisible(selector[1].id,0)
endif
endfunction
// LOAD ALL THE STUFF ************************************************************************************************
Loadimage(1,"Scientist48.png")
type playerdata
id as integer
idle as integer
statechange as integer
state as integer
facing as integer
name as string
x as integer
y as integer
oldx as integer
oldy as integer
firing as integer
gun as integer
reload as integer
changegun as integer
endtype
Dim player[1] as playerdata
player[1].id=1
player[1].x=1
player[1].y=1
player[1].idle=0
player[1].facing=1
player[1].statechange=0
player[1].state=0
player[1].firing=0
player[1].gun=1
player[1].name="Name"
player[1].reload=0
player[1].changegun=0
createsprite (1,1)
setspritedepth(1,1)
setspriteanimation (1,48,48,16)
playsprite(1,10,1,1,1)
setspriteshape(1,3)
// load the floor tile
loadimage(2,"floor.png")
Loadimage(3,"roof.png")
Loadimage(4,"wall.png")
// BULLETS AND PROJECTILE LOADS
global bullet_spr as integer
bullet_spr=loadimage ("bullet.png")
type projectile
name as string
id as integer
x as integer
y as integer
active as integer
damage as integer
state as integer
reload as integer
speed as integer
direction as integer
explodetime as integer
explode as integer
endtype
dim bullet[10] as projectile
for i=1 to 10
bullet[i].id=createsprite (bullet_spr)
setspritedepth(bullet[i].id,1)
setspriteanimation (bullet[i].id,10,10,3)
playsprite(bullet[i].id,10,1,1,1)
setspriteshape(bullet[i].id,3)
setspritevisible(bullet[i].id,0)
bullet[i].x=1
bullet[i].y=1
bullet[i].state=1
bullet[i].reload=2
bullet[i].speed=4
bullet[i].active=0
bullet[i].direction=0
bullet[i].explodetime=12
bullet[i].explode=0
bullet[i].damage=1
next i
// ROCKET INFORMATION
global rocket_spr as integer
rocket_spr=loadimage ("rocket.png")
dim rocket[10] as projectile
for i=1 to 10
rocket[i].id=createsprite (rocket_spr)
setspritedepth(rocket[i].id,1)
setspriteanimation (rocket[i].id,21,21,15)
playsprite(rocket[i].id,10,1,1,1)
setspriteshape(rocket[i].id,3)
setspritevisible(rocket[i].id,0)
rocket[i].x=1
rocket[i].y=1
rocket[i].state=1
rocket[i].reload=2
rocket[i].speed=3
rocket[i].active=0
rocket[i].direction=0
rocket[i].explodetime=12
rocket[i].explode=0
rocket[i].damage=5
next i
//the floordata is held in this array. 18 tiles across, and 13 down.
Dim Floordata [13,9]
// randomize the position of the start hatch and the escape, they must be 5 tiles away along the x and y
type door
x as integer
y as integer
endtype
dim hatch[3] as door
loadimage(5,"hatch.png")
global zombie_spr as integer
zombie_spr=loadimage("zombie.png")
type zombie
active as integer
hp as integer
id as integer
x as integer
y as integer
oldx as integer
oldy as integer
idle as integer
facing as integer
damage as integer
state as integer
attackcounter as integer
statechange as integer
charging as integer
endtype
Global level=1
// hud image
loadimage(7,"hud.png")
createsprite(600,7)
SetSpritePosition(600,0,0)
setspritedepth(600,501)
// dog with bees in mouth enemy
global dog_spr as integer
dog_spr=loadimage("dogbee.png")
type dog
active as integer
hp as integer
id as integer
x as integer
y as integer
oldx as integer
oldy as integer
idle as integer
facing as integer
state as integer
attackcounter as integer
statechange as integer
liningshot as integer
endtype
// Build the level
buildlevel()
// fill the level with enemies
populate()
// add a virtual joystick
AddVirtualJoystick(1,590,440,80)
addvirtualbutton(1,50,440,60)
addvirtualbutton(2,130,440,60)
do
input()
enemies()
projectiles()
collision ()
playeranimation()
setdepths ()
Print( ScreenFPS() )
print(totalzombies)
Sync()
loop
// *************************** INPUT FUNCTION *******************************************
// Input function
function input ()
// record the old x and y for collision.
player[1].oldx=player[1].x
player[1].oldy=player[1].y
// set the player to idle animation and set the statechange to the current state.
player[1].idle=1
player[1].statechange=player[1].state
// KEY_LEFT 37
// KEY_UP 38
// KEY_RIGHT 39
// KEY_DOWN 40
// check for input from virtual joystick
if getvirtualjoystickx(1)<>0
if getvirtualjoystickx(1)<0
player[1].x=player[1].x-2
player[1].idle=0
elseif getvirtualjoystickx(1)>0
player[1].x=player[1].x+2
player[1].idle=0
endif
endif
if getvirtualjoysticky(1)<>0
if getvirtualjoysticky(1)>0
player[1].y=player[1].y+2
player[1].idle=0
elseif getvirtualjoysticky(1)<0
player[1].y=player[1].y-2
player[1].idle=0
endif
endif
joyx=getvirtualjoystickx(1)
joyy=getvirtualjoysticky(1)
if joyx<0 then joyx=joyx*-1
if joyy<0 then joyy=joyy*-1
if joyx>joyy
if getvirtualjoystickx(1)<0
player[1].facing=4
player[1].state=1
endif
if getvirtualjoystickx(1)>0
player[1].facing=3
player[1].state=3
endif
endif
if joyy>joyx
if getvirtualjoysticky(1)<0
player[1].facing=1
player[1].state=2
endif
if getvirtualjoysticky(1)>0
player[1].facing=2
player[1].state=4
endif
endif
if player[1].idle=1
player[1].state=5
endif
//code for phone
if GetVirtualButtonState(2)=1
player[1].firing=1
else
player[1].firing=0
endif
//check for changeweappn
if player[1].changegun=0
// code for phone
if GetVirtualButtonState(1)=1
player[1].changegun=1
if player[1].reload=0 then player[1].reload=1
endif
endif
if player[1].reload=0 and player[1].changegun=1
player[1].reload=1
if player[1].changegun=1
player[1].changegun=0
if player[1].gun=1
player[1].gun=2
else
player[1].gun=1
endif
endif
endif
print (player[1].gun)
print (player[1].state)
print (player[1].statechange)
setspriteposition(1,player[1].x,player[1].y)
endfunction
// *************************** PLAYER ANIMATION FUNCTION *******************************************
// function animates the player
function playeranimation()
//checks whether the animation state has changed. Animates based on idle and facing.
if player[1].statechange<>player[1].state
if player[1].idle=0
if player[1].facing=3 then playsprite(1,10,1,8,9)
if player[1].facing=4 then playsprite(1,10,1,11,12)
if player[1].facing=2 then playsprite(1,10,1,5,6)
if player[1].facing=1 then playsprite(1,10,1,2,3)
endif
if player[1].idle=1
if player[1].facing=3 then playsprite(1,10,1,7,7)
if player[1].facing=4 then playsprite(1,10,1,10,10)
if player[1].facing=2 then playsprite(1,10,1,4,4)
if player[1].facing=1 then playsprite(1,10,1,1,1)
endif
endif
Endfunction
// *************************** BUILDLEVEL FUNCTION *******************************************
// level building function
function buildlevel ()
//the new floordata lengths for 48,48 are 13 across and 9 down (leaving room for txt on bottom and hud on top
//randomize the hatch locations
hatch[1].x=random(2,12)
hatch[1].y=random(2,8)
// this code places the second hatch away from the first
if hatch[1].x>6
hatch[2].x=hatch[1].x-random(3,5)
else
hatch[2].x=hatch[1].x+random(3,6)
endif
if hatch[1].y>5
hatch[2].y=hatch[1].y-random(3,4)
else
hatch[2].y=hatch[1].y+random(2,3)
endif
// failsafe for errors
if hatch[2].y<2 then hatch[2].y=2
if hatch[2].y>8 then hatch[2].y=8
if hatch[2].x<2 then hatch[2].x=2
if hatch[2].x>12 then hatch[2].x=12
//set all the floordata to roof
for i=1 to 13
for o =1 to 9
floordata[i,o]=2
next o
next i
// create a worm of floor tiles, starting at hatch position and repeat until it hits the other hatch. it wont go over border.
// also the worm should never go backwards
wormx=hatch[1].x
wormy=hatch[1].y
exitx=hatch[2].x
exity=hatch[2].y
repeat
floordata[wormx,wormy]=1
chance=random(1,2)
if chance =1
chance2=random(1,2)
if chance2=1 then wormx=wormx+1
if chance2=2 then wormx=wormx-1
endif
if chance =2
chance2=random(1,2)
if chance2=1 then wormy=wormy+1
if chance2=2 then wormy=wormy-1
endif
if wormx>12 then wormx=12
if wormy>8 then wormy=8
if wormx<2 then wormx=2
if wormy<2 then wormy=2
until wormx=exitx and wormy=exity
// this is a function that decides whether to draw roofs or walls
for i=2 to 12
for o=1 to 08
// if roof then check to see if should be wall
if floordata[i,o]>1
if floordata[i,o+1]=1 then floordata[i,o]=3
endif
next o
next i
// this is a repeat of the two fors to draw the sprites. Maxtiles is a variable name for the current sprite index of the tile.
// maxtiles gets incremented to 2 so it doenst take the player position of 1.
maxtiles=1
for i=1 to 13
for o=1 to 9
inc maxtiles
createsprite (maxtiles,floordata[i,o]+1)
if i=hatch[1].x
if o=hatch[1].y
setspriteimage(maxtiles,5)
setspriteanimation(maxtiles,48,48,2)
hatch_spr=maxtiles
floordata[i,o]=1
endif
endif
if i=hatch[2].x
if o=hatch[2].y
setspriteimage(maxtiles,5)
setspriteanimation(maxtiles,48,48,2)
hatch2_spr=maxtiles
floordata[i,o]=1
endif
endif
setspriteposition (maxtiles,((i-1)*48)+9,o*48)
if floordata[i,o]=1 then setspritedepth (maxtiles,500)
if floordata[i,o]>1 then setspritedepth (maxtiles,500-getspritey(maxtiles))
next o
next i
// set the player position to the hatch position
player[1].x=getspritex(hatch_spr)
player[1].y=getspritey(hatch_spr)
playsprite(hatch_spr,1,0,2,1)
endfunction
// *************************** COLLISION FUNCTION *******************************************
function collision()
// this function checks for player and enemy collision with walls and roofs
// lets use maxtiles and the same i and o loop.
maxtiles=1
for i=1 to 13
for o=1 to 9
inc maxtiles
if floordata[i,o]>1
// checks for collision with PLAYER walls and roofs *******************
if floordata[i,o]=3
if player[1].x<getspritex(maxtiles)+42 and player[1].x>getspritex(maxtiles)-38
if player[1].y<getspritey(maxtiles)+15 and player[1].y>getspritey(maxtiles)-25
if player[1].oldx<getspritex(maxtiles)+42 and player[1].oldx>getspritex(maxtiles)-38
player[1].y=player[1].oldy
endif
if player[1].oldy<getspritey(maxtiles)+15 and player[1].oldy>getspritey(maxtiles)-25
player[1].x=player[1].oldx
endif
endif
endif
endif
if floordata[i,o]=2
if player[1].x<getspritex(maxtiles)+42 and player[1].x>getspritex(maxtiles)-38
if player[1].y<getspritey(maxtiles)+25 and player[1].y>getspritey(maxtiles)-25
if player[1].oldx<getspritex(maxtiles)+42 and player[1].oldx>getspritex(maxtiles)-38
player[1].y=player[1].oldy
endif
if player[1].oldy<getspritey(maxtiles)+48 and player[1].oldy>getspritey(maxtiles)-25
player[1].x=player[1].oldx
endif
endif
endif
endif
SetSpritePosition(1,player[1].x,player[1].y)
// CHECK FOR COLLISION WITH ZOMBIES and WALLS AND ROOFS *******
for p=1 to totalzombies
if floordata[i,o]=3
if zombie[p].x<getspritex(maxtiles)+42 and zombie[p].x>getspritex(maxtiles)-38
if zombie[p].y<getspritey(maxtiles)+15 and zombie[p].y>getspritey(maxtiles)-25
if zombie[p].oldx<getspritex(maxtiles)+42 and zombie[p].oldx>getspritex(maxtiles)-38
zombie[p].y=zombie[p].oldy
endif
if zombie[p].oldy<getspritey(maxtiles)+15 and zombie[p].oldy>getspritey(maxtiles)-25
zombie[p].x=zombie[p].oldx
endif
endif
endif
endif
if floordata[i,o]=2
if zombie[p].x<getspritex(maxtiles)+42 and zombie[p].x>getspritex(maxtiles)-42
if zombie[p].y<getspritey(maxtiles)+25 and zombie[p].y>getspritey(maxtiles)-25
if zombie[p].oldx<getspritex(maxtiles)+42 and zombie[p].oldx>getspritex(maxtiles)-42
zombie[p].y=zombie[p].oldy
endif
if zombie[p].oldy<getspritey(maxtiles)+48 and zombie[p].oldy>getspritey(maxtiles)-25
zombie[p].x=zombie[p].oldx
endif
endif
endif
endif
setspriteposition(zombie[p].id,zombie[p].x,zombie[p].y)
next p
// CHECK FOR COLLISION WITH DOGS and WALLS AND ROOFS *******
for p=1 to totaldogs
if floordata[i,o]=3
if dog[p].x<getspritex(maxtiles)+42 and dog[p].x>getspritex(maxtiles)-38
if dog[p].y<getspritey(maxtiles)+15 and dog[p].y>getspritey(maxtiles)-25
if dog[p].oldx<getspritex(maxtiles)+42 and dog[p].oldx>getspritex(maxtiles)-38
dog[p].y=dog[p].oldy
endif
if dog[p].oldy<getspritey(maxtiles)+15 and dog[p].oldy>getspritey(maxtiles)-25
dog[p].x=dog[p].oldx
endif
endif
endif
endif
if floordata[i,o]=2
if dog[p].x<getspritex(maxtiles)+42 and dog[p].x>getspritex(maxtiles)-42
if dog[p].y<getspritey(maxtiles)+25 and dog[p].y>getspritey(maxtiles)-25
if dog[p].oldx<getspritex(maxtiles)+42 and dog[p].oldx>getspritex(maxtiles)-42
dog[p].y=dog[p].oldy
endif
if dog[p].oldy<getspritey(maxtiles)+48 and dog[p].oldy>getspritey(maxtiles)-25
dog[p].x=dog[p].oldx
endif
endif
endif
endif
setspriteposition(dog[p].id,dog[p].x,dog[p].y)
next p
endif
next o
next i
endfunction
// *************************** ENEMIES FUNCTION *******************************************
function enemies()
// check whether the zombie should idle or move
for i=1 to totalzombies
print (zombie[i].state)
// check whether zombie should die, sets active to 0
if zombie[i].hp<1
if zombie [i].active=1
zombie[i].active=0
playsprite (zombie[i].id,10,0,13,16)
endif
endif
if zombie[i].active=1
//set the zombie statechange
zombie[i].statechange=zombie[i].state
// set the oldzombie pos
zombie[i].oldx=zombie[i].x
zombie[i].oldy=zombie[i].y
// BEHAVIOUR CODE TO IDLE, MOVE or LAUNCH ATTACK
if zombie[i].state<3
//CHANCE TO IDLE OR MOVE IF NOT CHARGING
if zombie[i].charging=0
chance=random(1,30)
if chance=1
if zombie[i].idle=0
zombie[i].idle=1
chance=random(1,3)
if chance=1
zombie[i].facing=random(1,4)
endif
elseif zombie[i].idle=1
zombie[i].idle=0
endif
endif
endif
if zombie[i].idle=1 then zombie[i].state=2
if zombie[i].idle=0 then zombie[i].state=1
// if not idling then move in a direction based on facing
// also check if player is within a rectange of 50 by 100 pixels of the facing direction, if so, charge!
//facing down
if zombie[i].facing=1
if player[1].y>zombie[i].y and player[1].y<zombie[i].y+200
if player[1].x<zombie[i].x+50 and player[1].x>zombie[i].x-50
inc zombie[i].y
if player[1].x>zombie[i].x then inc zombie[i].x
if player[1].x<zombie[i].x then dec zombie[i].x
zombie[i].charging=1
else
if zombie[i].idle=0 then inc zombie[i].y
zombie[i].charging=0
endif
else
if zombie[i].idle=0 then inc zombie[i].y
zombie[i].charging=0
endif
endif
//facing up
if zombie[i].facing=2
if player[1].y<zombie[i].y and player[1].y>zombie[i].y-200
if player[1].x<zombie[i].x+50 and player[1].x>zombie[i].x-50
dec zombie[i].y
if player[1].x>zombie[i].x then inc zombie[i].x
if player[1].x<zombie[i].x then dec zombie[i].x
zombie[i].charging=1
else
if zombie[i].idle=0 then dec zombie[i].y
zombie[i].charging=0
endif
else
if zombie[i].idle=0 then dec zombie[i].y
zombie[i].charging=0
endif
endif
//facing left
if zombie[i].facing=3
if player[1].x<zombie[i].x and player[1].x>zombie[i].x-200
if player[1].y<zombie[i].y+50 and player[1].y>zombie[i].y-50
dec zombie[i].x
if player[1].y>zombie[i].y then inc zombie[i].y
if player[1].y<zombie[i].y then dec zombie[i].y
zombie[i].charging=1
else
if zombie[i].idle=0 then dec zombie[i].x
zombie[i].charging=0
endif
else
if zombie[i].idle=0 then dec zombie[i].x
zombie[i].charging=0
endif
endif
//facing right
if zombie[i].facing=4
if player[1].x>zombie[i].x and player[1].x<zombie[i].x+200
if player[1].y<zombie[i].y+50 and player[1].y>zombie[i].y-50
inc zombie[i].x
if player[1].y>zombie[i].y then inc zombie[i].y
if player[1].y<zombie[i].y then dec zombie[i].y
zombie[i].charging=1
else
if zombie[i].idle=0 then inc zombie[i].x
zombie[i].charging=0
endif
else
if zombie[i].idle=0 then inc zombie[i].x
zombie[i].charging=0
endif
endif
setspriteposition(zombie[i].id,zombie[i].x,zombie[i].y)
//checks whether the zombie state has changed. Animates based on idle and facing.
if zombie[i].statechange<>zombie[i].state
if zombie[i].idle=0
if zombie[i].facing=4 then playsprite(zombie[i].id,10,1,8,9)
if zombie[i].facing=3 then playsprite(zombie[i].id,10,1,11,12)
if zombie[i].facing=1 then playsprite(zombie[i].id,10,1,5,6)
if zombie[i].facing=2 then playsprite(zombie[i].id,10,1,2,3)
endif
endif
if zombie[i].idle=1
if zombie[i].facing=4 then playsprite(zombie[i].id,10,1,7,7)
if zombie[i].facing=3 then playsprite(zombie[i].id,10,1,10,10)
if zombie[i].facing=1 then playsprite(zombie[i].id,10,1,4,4)
if zombie[i].facing=2 then playsprite(zombie[i].id,10,1,1,1)
endif
//check if close enough to launch an attack
if player[1].x<zombie[i].x+40 and player[1].x>zombie[i].x-30
if player[1].y<zombie[i].y+40 and player[1].y>zombie[i].y-30
print ("attack")
if player[1].x=>zombie[i].x
xdif=player[1].x-zombie[i].x
endif
if player[1].x<zombie[i].x
xdif=zombie[i].x-player[1].x
endif
if player[1].y=>zombie[i].y
ydif=player[1].y-zombie[i].y
endif
if player[1].y<zombie[i].y
ydif=zombie[i].y-player[1].y
endif
if xdif>ydif
if player[1].x>=zombie[i].x
zombie[i].state=4
zombie[i].facing=4
playsprite(zombie[i].id,4,0,21,22)
else
zombie[i].state=5
zombie[i].facing=3
playsprite(zombie[i].id,4,0,23,24)
endif
else
if player[1].y>=zombie[i].y
zombie[i].state=6
zombie[i].facing=1
playsprite(zombie[i].id,4,0,19,20)
else
zombie[i].state=7
zombie[i].facing=2
playsprite(zombie[i].id,4,0,17,18)
endif
endif
endif
endif
endif
// attack movement direction
if zombie[i].state>3
if zombie[i].attackcounter>0
dec zombie[i].attackcounter
if zombie[i].state=7 then zombie[i].y=zombie[i].y-1
if zombie[i].state=6 then zombie[i].y=zombie[i].y+1
if zombie[i].state=5 then zombie[i].x=zombie[i].x-1
if zombie[i].state=4 then zombie[i].x=zombie[i].x+1
setspriteposition(zombie[i].id,zombie[i].x,zombie[i].y)
else
zombie[i].state=2
zombie[i].idle=0
zombie[i].charging=1
zombie[i].attackcounter=12
endif
endif
// if charging, definately not idleing
if zombie[i].charging=1 then zombie[i].idle=0
endif
next i
// -----------------------------------------------------DOGS-------------------------------------------
for i=1 to totaldogs
// check whether dog should die, sets active to 0
if dog[i].hp<1
if dog[i].active=1
playsprite(dog[i].id,10,0,25,27)
dog[i].active=0
endif
endif
if dog[i].active=1
//set the Dog statechange
dog[i].statechange=dog[i].state
// set the old pos
dog[i].oldx=dog[i].x
dog[i].oldy=dog[i].y
// BEHAVIOUR CODE TO IDLE, MOVE or LAUNCH ATTACK
if dog[i].state<3
//CHANCE TO IDLE OR MOVE IF NOT LINING A SHOT
if dog[i].liningshot=0
chance=random(1,30)
if chance=1
if dog[i].idle=0
dog[i].idle=1
dog[i].facing=random(1,2)
elseif dog[i].idle=1
dog[i].idle=0
endif
endif
endif
if dog[i].idle=1 then dog[i].state=2
if dog[i].idle=0 then dog[i].state=1
// if not idling then move in a direction based on facing
// also check if player is within a rectange of 150 by 200 pixels of the facing direction, if so, charge!
//facing left
if dog[i].facing=1
if player[1].y>dog[i].y and player[1].y<dog[i].y+200
if player[1].x<dog[i].x+50 and player[1].x>dog[i].x-50
inc dog[i].y
if player[1].x>dog[i].x then inc dog[i].x
if player[1].x<dog[i].x then dec dog[i].x
dog[i].liningshot=1
else
if dog[i].idle=0 then inc dog[i].y
dog[i].liningshot=0
endif
else
if dog[i].idle=0 then inc dog[i].y
dog[i].liningshot=0
endif
endif
//facing right
if dog[i].facing=2
if player[1].y<dog[i].y and player[1].y>dog[i].y-200
if player[1].x<dog[i].x+50 and player[1].x>dog[i].x-50
dec dog[i].y
if player[1].x>dog[i].x then inc dog[i].x
if player[1].x<dog[i].x then dec dog[i].x
dog[i].liningshot=1
else
if dog[i].idle=0 then dec dog[i].y
dog[i].liningshot=0
endif
else
if dog[i].idle=0 then dec dog[i].y
dog[i].liningshot=0
endif
endif
setspriteposition(dog[i].id,dog[i].x,dog[i].y)
//checks whether the Dog state has changed. Animates based on idle and facing.
if dog[i].statechange<>dog[i].state
if dog[i].idle=0
if dog[i].facing=1 then playsprite(dog[i].id,10,1,17,20)
if dog[i].facing=2 then playsprite(dog[i].id,10,1,21,24)
endif
if dog[i].idle=1
if dog[i].facing=1 then playsprite(dog[i].id,10,1,1,4)
if dog[i].facing=2 then playsprite(dog[i].id,10,1,5,8)
endif
endif
//check if close enough to launch an attack
if player[1].x<dog[i].x+40 and player[1].x>dog[i].x-30
if player[1].y<dog[i].y+40 and player[1].y>dog[i].y-30
print ("attack")
if player[1].x=>dog[i].x
xdif=player[1].x-dog[i].x
endif
if player[1].x<dog[i].x
xdif=dog[i].x-player[1].x
endif
if player[1].y=>dog[i].y
ydif=player[1].y-dog[i].y
endif
if player[1].y<dog[i].y
ydif=dog[i].y-player[1].y
endif
if xdif>ydif
if player[1].x>=dog[i].x
dog[i].state=4
dog[i].facing=1
playsprite(dog[i].id,4,0,9,12)
else
dog[i].state=5
dog[i].facing=2
playsprite(dog[i].id,4,0,13,16)
endif
else
if player[1].y>=dog[i].y
dog[i].state=6
dog[i].facing=1
playsprite(dog[i].id,4,0,9,12)
else
dog[i].state=7
dog[i].facing=2
playsprite(dog[i].id,4,0,13,16)
endif
endif
endif
endif
endif
// attack movement direction
if dog[i].state>3
if dog[i].attackcounter>0
dec dog[i].attackcounter
if dog[i].state=7 then dog[i].y=dog[i].y-1
if dog[i].state=6 then dog[i].y=dog[i].y+1
if dog[i].state=5 then dog[i].x=dog[i].x-1
if dog[i].state=4 then dog[i].x=dog[i].x+1
setspriteposition(dog[i].id,dog[i].x,dog[i].y)
else
dog[i].state=2
dog[i].idle=0
dog[i].liningshot=1
dog[i].attackcounter=12
endif
endif
// if charging, definately not idleing
if dog[i].liningshot=1 then dog[i].idle=0
endif
next i
endfunction
// *************************** POPULATE FUNCTION *******************************************
function populate()
// set a random amout of zombies based on level number
global totalzombies
totalzombies=level+random(1,10)
dim zombie[totalzombies] as zombie
for i=1 to totalzombies
zombie[i].id=createsprite(zombie_spr)
setspritedepth(zombie[i].id,2)
setspriteanimation (zombie[i].id,48,48,24)
playsprite(zombie[i].id,10,1,1,1)
setspriteshape(zombie[i].id,3)
zombie[i].x=0
zombie[i].y=1
zombie[i].state=1
zombie[i].statechange=1
zombie[i].facing=1
zombie[i].idle=1
zombie[i].active=1
zombie[i].hp=2
zombie[i].damage=1
zombie[i].attackcounter=12
zombie[i].charging=0
repeat
maxtiles=1
for p=1 to 13
for o=1 to 9
inc maxtiles
// if floor
if floordata[p,o]=1
chance=random(1,30)
if chance=1
zombie[i].x=getspritex(maxtiles)
zombie[i].y=getspritey(maxtiles)
setspriteposition(zombie[i].id,zombie[i].x,zombie[i].y)
endif
endif
next o
next p
until zombie[i].x>1
next i
// set a random amout of dogs based on level number
global totaldogs
totaldogs=level+random(1,2)
dim dog[totaldogs]as dog
for i=1 to totaldogs
dog[i].id=createsprite(dog_spr)
setspritedepth(dog[i].id,2)
setspriteanimation (dog[i].id,32,32,27)
playsprite(dog[i].id,10,1,1,1)
setspriteshape(dog[i].id,3)
dog[i].x=0
dog[i].y=1
dog[i].state=1
dog[i].statechange=1
dog[i].facing=1
dog[i].idle=1
dog[i].active=1
dog[i].hp=1
dog[i].attackcounter=12
dog[i].liningshot=0
repeat
maxtiles=1
for p=1 to 13
for o=1 to 9
inc maxtiles
// if floor
if floordata[p,o]=1
chance=random(1,30)
if chance=1
dog[i].x=getspritex(maxtiles)
dog[i].y=getspritey(maxtiles)
setspriteposition(dog[i].id,dog[i].x,dog[i].y)
endif
endif
next o
next p
until dog[i].x>1
next i
endfunction
// *************************** PROJECTILES FUNCTION *******************************************
function projectiles ()
if player[1].reload>0
inc player[1].reload
if player[1].reload=12 then player[1].reload=0
endif
//check if firing
if player[1].firing=1
//check reload
if player[1].reload=0
player[1].reload=1
//check which gun
//************ BASIC GUN SCRIPT*********
if player[1].gun=1
for i=1 to 10
if bullet[i].active=0
bullet[i].active=1
bullet[i].direction=player[1].facing
bullet[i].x=player[1].x
bullet[i].y=player[1].y
// offsets for coming out of gun
if bullet[i].direction=1
bullet[i].x=bullet[i].x+20
bullet[i].y=bullet[i].y+12
endif
if bullet[i].direction=2
bullet[i].x=bullet[i].x+6
bullet[i].y=bullet[i].y+12
endif
if bullet[i].direction=4
bullet[i].x=bullet[i].x+2
bullet[i].y=bullet[i].y+14
endif
if bullet[i].direction=3
bullet[i].x=bullet[i].x+20
bullet[i].y=bullet[i].y+14
endif
setspriteposition(bullet[i].id,bullet[i].x,bullet[i].y)
setspritevisible(bullet[i].id,1)
playsprite(bullet[i].id,10,1,1,1)
exit
endif
next i
endif
// ************* script for rockets (2) ***********************
if player[1].gun=2
for i=1 to 10
if rocket[i].active=0
rocket[i].active=1
rocket[i].direction=player[1].facing
rocket[i].x=player[1].x
rocket[i].y=player[1].y
// offsets for coming out of gun
if rocket[i].direction=1
rocket[i].x=rocket[i].x+14
rocket[i].y=rocket[i].y+4
playsprite(rocket[i].id,1,1,10,12)
endif
if rocket[i].direction=2
rocket[i].x=rocket[i].x
rocket[i].y=rocket[i].y+18
playsprite(rocket[i].id,1,1,7,9)
endif
if rocket[i].direction=4
rocket[i].x=rocket[i].x-2
rocket[i].y=rocket[i].y+10
playsprite(rocket[i].id,1,1,4,6)
endif
if rocket[i].direction=3
rocket[i].x=rocket[i].x+14
rocket[i].y=rocket[i].y+10
playsprite(rocket[i].id,1,1,1,3)
endif
setspriteposition(rocket[i].id,rocket[i].x,rocket[i].y)
setspritevisible(rocket[i].id,1)
exit
endif
next i
endif
endif
endif
// move the bullets and collision check for walls and objects
for i=1 to 10
if bullet[i].active=1
if bullet[i].direction=4 then bullet[i].x=bullet[i].x-bullet[i].speed
if bullet[i].direction=1 then bullet[i].y=bullet[i].y-bullet[i].speed
if bullet[i].direction=3 then bullet[i].x=bullet[i].x+bullet[i].speed
if bullet[i].direction=2 then bullet[i].y=bullet[i].y+bullet[i].speed
setspriteposition(bullet[i].id,bullet[i].x,bullet[i].y)
// check for hit walls
maxtiles=1
for p=1 to 13
for o=1 to 9
inc maxtiles
//check y offset
if floordata[p,o]>1
if getspritecollision(maxtiles,bullet[i].id)=1
if floordata[p,o]=3
if bullet[i].y>getspritey(maxtiles)+5
if bullet[i].y<getspritey(maxtiles)+24
bullet[i].active=2
bullet[i].explode=1
playsprite(bullet[i].id,10,1,2,3)
endif
endif
endif
if floordata[p,o]=2
//check if above is also a wall, if so, hitbox is equal to just a hit. If not, take 5 off y
if o>1
if floordata[p,o-1]>1
bullet[i].active=2
bullet[i].explode=1
playsprite(bullet[i].id,10,1,2,3)
endif
if floordata[p,o-1]=1
if bullet[i].y>getspritey(maxtiles)+5
if bullet[i].y<getspritey(maxtiles)+32
bullet[i].active=2
bullet[i].explode=1
playsprite(bullet[i].id,10,1,2,3)
endif
endif
endif
endif
endif
setspriteposition(bullet[i].id,bullet[i].x,bullet[i].y)
endif
endif
next o
next p
//check for collision with zombies, explode bullet and damage zombie
for o=1 to totalzombies
if zombie[o].active=1
if getspritecollision(zombie[o].id,bullet[i].id)=1
bullet[i].active=2
bullet[i].explode=1
playsprite(bullet[i].id,10,1,2,3)
zombie[o].hp=zombie[o].hp-bullet[i].damage
endif
endif
next o
//check for collision with dogs, explode bullet and damage dog
for o=1 to totaldogs
if dog[o].active=1
if getspritecollision(dog[o].id,bullet[i].id)=1
bullet[i].active=2
bullet[i].explode=1
playsprite(bullet[i].id,10,1,2,3)
dog[o].hp=dog[o].hp-bullet[i].damage
endif
endif
next o
endif
// this section does the explosion animation of the bullet striking something
if bullet[i].active=2
if bullet[i].explode>0 then inc bullet[i].explode
if bullet[i].explode=bullet[i].explodetime
setspritevisible(bullet[i].id,0)
bullet[i].explode=0
bullet[i].active=0
endif
endif
next i
// move the rocket and collision check for walls and objects
// *******************ROCKET CODE*********************************************************************
for i=1 to 10
if rocket[i].active=1
if rocket[i].direction=4 then rocket[i].x=rocket[i].x-rocket[i].speed
if rocket[i].direction=1 then rocket[i].y=rocket[i].y-rocket[i].speed
if rocket[i].direction=3 then rocket[i].x=rocket[i].x+rocket[i].speed
if rocket[i].direction=2 then rocket[i].y=rocket[i].y+rocket[i].speed
setspriteposition(rocket[i].id,rocket[i].x,rocket[i].y)
// check for hit walls
maxtiles=1
for p=1 to 13
for o=1 to 9
inc maxtiles
//check y offset
if floordata[p,o]>1
if getspritecollision(maxtiles,rocket[i].id)=1
if floordata[p,o]=3
if rocket[i].y>getspritey(maxtiles)-5
if rocket[i].y<getspritey(maxtiles)+24
rocket[i].explode=1
rocket[i].active=2
playsprite(rocket[i].id,10,1,13,15)
endif
endif
endif
if floordata[p,o]=2
//check if above is also a wall, if so, hitbox is equal to just a hit. If not, take 5 off y
if o>1
if floordata[p,o-1]>1
rocket[i].active=2
rocket[i].explode=1
playsprite(rocket[i].id,10,1,13,15)
endif
if floordata[p,o-1]=1
if rocket[i].y>getspritey(maxtiles)-5
if rocket[i].y<getspritey(maxtiles)+32
rocket[i].active=2
rocket[i].explode=1
playsprite(rocket[i].id,10,1,13,15)
endif
endif
endif
endif
endif
setspriteposition(rocket[i].id,rocket[i].x,rocket[i].y)
endif
endif
next o
next p
//check for collision with zombies, explode rocket and damage zombie
for o=1 to totalzombies
if zombie[o].active=1
if getspritecollision(zombie[o].id,rocket[i].id)=1
rocket[i].active=2
rocket[i].explode=1
playsprite(rocket[i].id,10,1,13,15)
zombie[o].hp=zombie[o].hp-rocket[i].damage
endif
endif
next o
//check for collision with dogs, explode rocket and damage dogs
for o=1 to totaldogs
if dog[o].active=1
if getspritecollision(dog[o].id,rocket[i].id)=1
rocket[i].active=2
rocket[i].explode=1
playsprite(rocket[i].id,10,1,13,15)
dog[o].hp=dog[o].hp-rocket[i].damage
endif
endif
next o
endif
// this section does the explosion animation of the rocket striking something
if rocket[i].active=2
if rocket[i].explode>0 then inc rocket[i].explode
if rocket[i].explode=rocket[i].explodetime
setspritevisible(rocket[i].id,0)
rocket[i].explode=0
rocket[i].active=0
endif
endif
next i
endfunction
// set depths. An attempt to fix sprite depths based on the Y value and an offset. Floor tiles are 500.
//**********************************************************SET DEPTHS***********************************************************
function setdepths ()
setspritedepth (1,500-getspritey(1))
for i=1 to totalzombies
if zombie[i].active=1
setspritedepth (zombie[i].id,500-getspritey(zombie[i].id))
elseif zombie[i].active=0
setspritedepth (zombie[i].id,500-getspritey(zombie[i].id)+10)
endif
next i
for i=1 to 10
if bullet[i].active=1
setspritedepth (bullet[i].id,500-getspritey(bullet[i].id)+10)
endif
if rocket[i].active=1
setspritedepth (rocket[i].id,500-getspritey(rocket[i].id)+10)
endif
next i
for i=1 to totaldogs
if dog[i].active=1
setspritedepth (dog[i].id,500-getspritey(dog[i].id))
elseif dog[i].active=0
setspritedepth (dog[i].id,500-getspritey(dog[i].id)+10)
endif
next i
endfunction