FIGHTING AGAINST THE 'BAD GUYS'

PIECE: star 1x0.33x1

Now, our avatar (at these Medieval Adventures) will be created using the piece soldiersword because, using the sword, he will "to kill" some soldiers and another "bad guys".

But, how can we create the code for the fight ? Who will be the winner?

We created a convention. Each character having a sword has a level of "power" - capacity to fight.

This power, for the avatar will appear in the color of his sword - the people that knows the martial art of "Judo" knows these colors:

The level of the enemies are not visible (all their swords are blue) and the player accepts or not the risk to fight (defined by a collision) with someone having a highter level and died, or will "walk" a little more trying to upgrade his power before the fight.

How to upgrade his power?

The user needs to find and click a "magic star" or to find a "wizard" and win a proposed challenge. This challenge can be a quiz, a 2D game etc.

Are you a little confuse? Let's go to see what we did in the example that can be downloaded:

The code for the "star click" is:

...
   if(gm3D.Geometry == TheCanvas.Resources["star1G"]){
  //Change sword color
   if(avpower==0) { newcol = new DiffuseMaterial(Brushes.Yellow) ;} 
    if(avpower==1 ){ newcol = new DiffuseMaterial(Brushes.Green);}
    if(avpower==2){ newcol = new DiffuseMaterial(Brushes.Brown);}
    if(avpower==3){ newcol = new DiffuseMaterial(Brushes.Purple);}
    if(avpower==4){ newcol = new DiffuseMaterial(Brushes.Black);}
    ModelVisual3D mv3D = VP.Children[0] as ModelVisual3D;
    Model3DGroup  m3Dg = mv3D.Content as  Model3DGroup;
    Model3DGroup m3ddg  =  m3Dg.Children[4] as Model3DGroup;//avatar
     GeometryModel3D gm3d = m3ddg.Children[1] as GeometryModel3D; //sword
     gm3d.Material = newcol;
     doorbell.Play();
     avpower=avpower+1;
    ChatReceived.Text="Congratulations! You now have a new level of power! LEVEL: "+avpower.ToString()  ;
   Matrix3D star1 = new Matrix3D(0.1,0,0,0,0,0.1,0,0,0,0,0.1,0,0,-99,0,1); 
   MatrixTransform3D mstar1= new MatrixTransform3D( star1);
   //ModelVisual3D mv3D = VP.Children[0] as ModelVisual3D;//declared before
    //Model3DGroup  m3Dg = mv3D.Content as  Model3DGroup;
   m3Dg.Children[12].Transform= mstar1  ;
  return HitTestResultBehavior.Stop;//Exit 
  }


...

The code for the encounter with the wizard has 2 parts, the first is at the collision, inside the "main loop":

...
//avatar x wizard1
   if(wizard1==1){
   Vector3D distavw1 = Point3D.Subtract(new Point3D(TheX,TheY,TheZ),new Point3D(-7, 101.5, -10));
   if(distavw1.Length < 4){
    mtrans= new MatrixTransform3D(transold);
     ModelVisual3D mv3D = VP.Children[0] as ModelVisual3D;
  Model3DGroup  m3Dg = mv3D.Content as  Model3DGroup;
  m3Dg.Children[4].Transform= mtrans  ;
    TheX=TheXold;
    TheZ=TheZold; 
    trans = transold;
     doorbell.Play();
   ChatReceived.Text="To upgrade your power  click the key having the number of the correct answer: A big river of China is: 1 - Amazon   2 - Mississipi  3 - Yellow. " ;
 answer=1;
  }
 }
...
The user will answer the question. We capture his keyboard key press:
...
if (e.Key == Key.D1 ){
   if(answer==1){
    doorbell.Play();
    ChatReceived.Text="Wrong answer! Try again.";
   } 
  }
  if (e.Key == Key.D2 ){
   if(answer==1){
    doorbell.Play();
   ChatReceived.Text="Wrong answer! Try again.";
  }
 } 
  if (e.Key == Key.D3 ){
  if(answer==1){
   //Change sword color
    if(avpower==0) { newcol = new DiffuseMaterial(Brushes.Yellow) ;} 
    if(avpower==1 ){ newcol = new DiffuseMaterial(Brushes.Green);}
    if(avpower==2){ newcol = new DiffuseMaterial(Brushes.Brown);}
    if(avpower==3){newcol = new DiffuseMaterial(Brushes.Purple);}
    if(avpower==4){newcol = new DiffuseMaterial(Brushes.Black);}
    ModelVisual3D mv3D = VP.Children[0] as ModelVisual3D;
   Model3DGroup  m3Dg = mv3D.Content as  Model3DGroup;
   Model3DGroup m3ddg  =  m3Dg.Children[4] as Model3DGroup;
    GeometryModel3D gm3d = m3ddg.Children[1] as GeometryModel3D;
     gm3d.Material = newcol;
    doorbell.Play();
    avpower=avpower+1;
   answer=0;
   ChatReceived.Text="Congratulations! You now have a new level of power! LEVEL: "+avpower.ToString()  ;
  Matrix3D wiz1 = new Matrix3D(0.1,0,0,0,0,0.1,0,0,0,0,0.1,0,0,-99,0,1); 
  MatrixTransform3D mwiz1= new MatrixTransform3D( wiz1);
//ModelVisual3D mv3D = VP.Children[0] as ModelVisual3D; //declared before
//Model3DGroup  m3Dg = mv3D.Content as  Model3DGroup;
 m3Dg.Children[11].Transform= mwiz1  ;
 
 wizard1=0;
  } 
 }
...

Obvious that we need to define many flags...

And, when the avatar collides/"fights" with the soldier (at the demo, only at the second "vista") that has a "power" = 2, we need to put the avatar OR the soldier in the "horizontal", at floor:

//avatar x soldier2
   Vector3D distavsold2 = Point3D.Subtract(new Point3D(TheX,TheY,TheZ),new Point3D(14, 101.5, 0));
   if(distavsold2.Length < 4 && soldier2==1){
   soldier2=0;
  if(avpower<2){
   trans = new Matrix3D(0.1,0,0,0,0,0.1,0,0,0,0,0.1,0,TheX ,100.4,TheZ,1); 
    Quaternion qt = new Quaternion(new Vector3D(1, 0, 0),0);
  Point3D pt = new Point3D(TheX   ,100.4,TheZ);
  trans.RotateAt(qt,pt);
   
  mtrans= new MatrixTransform3D(trans);
   ModelVisual3D mv3D = VP.Children[0] as ModelVisual3D;
  Model3DGroup  m3Dg = mv3D.Content as  Model3DGroup;
 m3Dg.Children[4].Transform= mtrans  ;
     
  ChatReceived.Text="SORRY... YOU ARE DEAD! GAME OVER!";
  doorbell.Play();
  avdead=1;
 }
  if( avpower>=2){
   trans = new Matrix3D(0.1,0,0,0,0,0.1,0,0,0,0,0.1,0,14 ,100.4,0,1); 
    Quaternion qt = new Quaternion(new Vector3D(1, 0, 0),0);
  Point3D pt = new Point3D(14   ,100.4,0);
  trans.RotateAt(qt,pt);
   
  mtrans= new MatrixTransform3D(trans);
  ModelVisual3D mv3D = VP.Children[0] as ModelVisual3D;
  Model3DGroup  m3Dg = mv3D.Content as  Model3DGroup;
  m3Dg.Children[13].Transform=  mtrans  ;
  ChatReceived.Text="YOU ARE THE WINNER OF  THIS FIGHT! CONGRATULATIONS! GO AHEAD! GO! GO!";
fanfare.Play();
   }
  }
...

And we have some more flags to define and change status...

The better idea is to analise all the code...

For the demo, create a folder having the name of the .ZIP file and download it - right-clicking the link - and unzip inside the folder :

tu33v.zip

OOPS! In the demo we forgot to change the "Health" to zero when the avatar dies... A little bug.

Liberate the firewall and play this web3d application:

We are doing our tests at a: Notebook DELL Latitude D600 - Intel Pentium M755 2.0 GHZ - Memory: 512 MB - VideoCard ATI Radeon 9000 32MB DDR 4xAGP - Resolution 1400x1050 - Communication: ADSL 256 kbps (Download: 45 kBps, Upload: 14 kBps)


PREVIOUS LESSON NEXT LESSON
T.CONTENTS HOMEPAGE