Enemies, or just a simple object one can hit

Post your coding questions such as 'how to' here.
Post Reply

Enemies, or just a simple object one can hit

Post by Mathias » Sun Oct 21, 2007 1:28 pm

I was messing around in CB the other day, trying to figure out how I could make an object respond to collisions with bullets.

So I have a bullets type set up, and an enemies type, which is set to spawn a new enemy whenever the first one dies. It pretty much looks like this.

Code: Select all

'Enemy stuff
  If enemylives=0 Then
    ScreenPositionObject newenemy\obj, 0,0    
    SetupCollision std_player, newenemy\obj, 1,1,2   
    ResetObjectCollision newenemy\obj
  For inewenemy.ENEMIES= Each ENEMIES       
        If CountCollisions(inewenemy\obj) Then
        'Hits counter
        DeleteObject inewenemy\obj
        Delete inewenemy
  Next inewenemy

          'Bullet stuff
  If MouseDown(1) And reload=0            
        newBullet.BULLETS= New(BULLETS)
        CloneObjectPosition newBullet\obj,std_player
        CloneObjectOrientation newBullet\obj,std_player              
        MoveObject newBullet\obj,70                                                

    'Update all BULLETS      
  For iBullet.BULLETS= Each BULLETS        
        MoveObject iBullet\obj,30 'speed=6        
            If Distance2(ibullet\obj,std_player) > 1000 Then
                DeleteObject ibullet\obj
                Delete ibullet             
  Next iBullet    
  If reload>0 Then reload=reload-1  
So how would I make my enemy respond to getting hit by bullets?

User avatar
Advanced Member
Posts: 485
Joined: Tue Aug 28, 2007 3:53 pm
Location: Suomi, Finland

Re: Enemies, or just a simple object one can hit

Post by tuhoojabotti » Mon Oct 22, 2007 1:26 am

when you update the bullets you must update all enemys (for each) and you could use objectoverlaps to see if bullet hits then but like panic=true and if panic=true then moveobject enemy,rand(-2,2) turnobject enemy(rand(10,100)) or something...
Imagedev.tuhoojabotti.com — “Programmer (noun): An organism that turns caffeine into code.”

User avatar
Posts: 650
Joined: Mon Aug 27, 2007 9:53 pm

Re: Enemies, or just a simple object one can hit

Post by CCE » Mon Oct 22, 2007 4:31 pm

You must update your enemies inside the bullet loop.
Like this

Code: Select all

' Enemy + bullets example by: cce
    Field obj 'object is saved here
End Type 

    Field obj 'object is saved here
	Field health 'enemys health variable
End Type 

FrameLimit 40 'limit speed

player=LoadObject("Media\soldier.bmp",72)	'player object
	'master object, all other enemies are cloned from this one
ShowObject master_enemy, OFF	'hide the master objecet
bullet_obj=LoadObject("Media\bullet.bmp") ' master bullet object
ShowObject bullet_obj,OFF

AddText "    Move with arrows, CTRL to shoot."
AddText ""
AddText "    Add enemies with your mouse."

    'Control player object with arrows
    If LeftKey() Then TurnObject player,5
    If RightKey() Then TurnObject player,-5
    If UpKey() Then MoveObject player,2
    If DownKey() Then MoveObject player,-2    

    'Shooting system
    If KeyDown(cbKeyRControl) And reload=0 Then    
        newbullet.SHOT = New(SHOT) 'create a new shot
        newbullet\obj=CloneObject(bullet_obj) 'clone from master bullet
        'place the bullet on the player
        CloneObjectPosition newbullet\obj,player            
        'put it on right orientation
        CloneObjectOrientation newbullet\obj,player         
        reload=5 'weapon reload        
    'update shooting
    If reload>0 Then reload=reload-1

	If MouseHit(1) Then	' If mouse is clicked
		newenemy.ENEMY = New(ENEMY)	' create a New enemy
		newenemy\obj = CloneObject(master_enemy) ' clone it from the master object
		newenemy\health = 75 'enemy health
		PositionObject newenemy\obj,MouseWX(),MouseWY() 'ON the mouse world coordinates
		RotateObject newenemy\obj,Rand(360) ' random direction

    'Update all the bullets
    For newbullet.SHOT = Each SHOT 'Loop through all the bullets
        MoveObject newbullet\obj,6
        If ObjectX(newbullet\obj)<-180 Or ObjectX(newbullet\obj)>180 Or ObjectY(newbullet\obj)<-130 Or ObjectY(newbullet\obj)>130 Then
            'bullet goes outside play-area --> mark it to be destroyed           
			kill_this_bullet = 1
			'The trick is To update te enemies inside the bullet loop
			For newenemy.ENEMY = Each ENEMY 'Loop through all the bullets
				If ObjectsOverlap(newenemy\obj,newbullet\obj) Then 'If objects overlap eachother
					newenemy\health - 25 'reduce some health from the enemy
					kill_this_bullet = 1 'mark the bullet To be removed
				' the actual AI And health check starts ON row 78
			Next newenemy
		If kill_this_bullet = 1 Then ' you cant just remove the bullet, it will cause MAV
		    DeleteObject newbullet\obj 'remove the object
		    Delete newbullet 'remove the whole bullet
			kill_this_bullet = 0 ' back To Default value
    Next newbullet

		' And now update the ai And stuff
		For newenemy.ENEMY = Each ENEMY 'Loop through all the bullets
				TurnObject newenemy\obj,Rnd(-3,3) 'some great AI
				MoveObject newenemy\obj, Rnd(-1,2)
				'If enemy leaves the boundaries
				If ObjectX(newenemy\obj)<-180 Or ObjectX(newenemy\obj)>180 Or ObjectY(newenemy\obj)<-130 Or ObjectY(newenemy\obj)>130 Then
					MoveObject newenemy\obj,-3 'move it back
					TurnObject newenemy\obj,180 'rotate it To the opposite direction
				If newenemy\health < 0 Then kill_this_enemy = 1 'If health goes below zero -> kill
				If kill_this_enemy = 1 Then 'If the enemy is marked To die, kill it now :(
					DeleteObject newenemy\obj
					Delete newenemy
					kill_this_enemy = 0
			Next newenemy

    'this before screen graphics (box)

    'draw the game boundaries
    Color cbOrange
    Box 20,20,360,260,OFF    

Until EscapeKey()

Post Reply