TGC Codebase Backup



Detecting simple enemy hits by Sir Spaghetti Code

14th Aug 2004 19:23
Summary

This is the way that I detect the player hitting an enemy and vice versa after APPENDing. For the Newbs!



Description

This is the way that I detect the player hitting an enemy and vice versa. This is more handy if you have APPENDed more animation to your objects, therefore, you do not want to register damage when your "Idle" animation touches the enemy. This is a very simple code to make sure the program can determine if the player or enemy is attacking.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    rem set up the screen
sync on
sync rate 60

rem set up fog and other good stuff
rem set fog
fog color rgb(28,28,0)
color backdrop rgb(28,28,28)

rem make matrix
make matrix 1,10000,10000,20,20
load image "lead.bmp",1
prepare matrix texture 1,1,1,1

rem load player
player = 1
load object "L-Knight-Idle.x",player

rem append additional animations
append object "L-Knight-Move.x",player,25
append object "L-Knight-Attack1.x",player,50
append object "H-Knight-Impact.x",player,75
yrotate object player,180
fix object pivot player
position object 1,15,0,15
set object collision to boxes 1

rem load enemy object and append attack animation
load object "L-Alien Rollerbug-Idle.x",2
append object "L-Alien Rollerbug-Attack1.x",2,25
position object 2,150,0,150
set object collision to boxes 2

health = 100
enemyhealth = 100

rem set up attack integer
attack = 0

rem main game loop
do
  
  rem Set the attack flag back to "0". you will see more in a second
  attack = 0

  rem set up controls for character
  If Upkey()=1
      XTest# = Newxvalue(X#,CameraAngleY#,3)
      ZTest# = Newzvalue(Z#,CameraAngleY#,3)
      If XTest#>0 and XTest#<10000 and ZTest#>0 and ZTest#<10000
         Move object 1,3
         loop object 1,25,50
      Endif
      if XTest#<=0 and XTest#>=10000 and ZTest#<=0 and ZTest#>=10000
         position object 1,X#,0,Z#
      endif
   Endif

   if downkey()=1
      move object 1,-3
      loop object 1,25,50
   endif

   rem OK. now I am inputing the attack code. notice how I have put a flag up on  
   rem the "Attack" integer
   if spacekey()=1
      loop object 1,50,75
      soundInt = sound playing(2)
      if soundInt = 0 then play sound 2
      attack = 1
   endif

   rem Now, we know if the player is attacking, because now we have a flag for it.

   rem check for defeated monster

   rem OK, what we are doing here is to see if the attack flag is on (1)
   rem if it is, then we decrease enemy's health by 1.
   rem If the character is NOT attacking (attack = 0), then the player 
   rem loses 1 point of health

   if object collision(1,3) 
      if attack = 1
         enemyhealth = enemyhealth - 1
      endif      
      if attack = 0
         health = health - 0.1
         loop object 1,75,85
      endif
   endif
loop

rem There you have it! That wasn't hard was it? That simple code is very useful for attacking appended loaded objects!