topical media & game development

talk show tell print

student-papervision-samples-CF07.ax

student-papervision-samples-CF07.ax [swf] flex


  package  {
          import org.papervision3d.objects.DisplayObject3D;
          import org.papervision3d.scenes.Scene3D;
          import student_papervision_samples_PaperBase;
          
          import org.papervision3d.lights.PointLight3D;
          import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
          import org.papervision3d.materials.utils.MaterialsList;
          import org.papervision3d.materials.WireframeMaterial;
          import org.papervision3d.objects.primitives.Cube;
          import org.papervision3d.objects.primitives.Sphere;
          import org.papervision3d.objects.primitives.Plane;
          import org.papervision3d.objects.parsers.MD2;
          import org.papervision3d.cameras.Camera3D;
          import org.papervision3d.materials.BitmapFileMaterial;
          
          import caurina.transitions.Tweener;
          
          import flash.events.KeyboardEvent;
          import flash.events.MouseEvent;
          import flash.text.TextField;
          import flash.text.TextFormat;
  
          public class @ax-student-papervision-samples-CF07 extends student_papervision_samples_PaperBase {
                  public var updown:Boolean = false;
                  public var leftdown:Boolean = false;
                  public var downdown:Boolean = false;
                  public var rightdown:Boolean = false;
                  public var spacedown:Boolean = false;
                  public var standing:Boolean = true;
                  public var gameStart:Boolean = false;
                  public var speed:Number = 150; //                        Here you can set the speed of the object. 
                  public var text:TextField = new TextField();
                  public var rotationPosition:Number = 0;        
                  public var rotationGoal:Number = 0;        
                  public var rotationBase:Number = 0; 
                  
                  private var fontstyle:TextFormat = new TextFormat("Arial", 44, 0x000000);
                  private var startgameFontstyle:TextFormat = new TextFormat("Georgia",64,0x000000);
                  private var endgameFontstyle:TextFormat = new TextFormat("Georgia",128,0x000000);
                  private var sheepSphere:Sphere;
                  private var penguinSphere:Sphere;
                  private var bullets:Array = new Array(10);
                  private var bulletsCounter:Number = 0;
                  private var enemyBullets:Array = new Array(10);
                  private var enemyBulletsCounter:Number = 0;
                  private var enemyBulletsInterval:Number = 25;
                  private var endgameScene:Scene3D = new Scene3D();
                  private var mat:FlatShadeMaterial = new FlatShadeMaterial(new PointLight3D(), 0xFFFFFF, 0x444444);
                  private var mat2:WireframeMaterial = new WireframeMaterial(0x00FF00);
                  private var bulletBox:DisplayObject3D;
                  private var sheepTexture:BitmapFileMaterial = new BitmapFileMaterial("Sheep.png"); // here you can set the texture image for the player-model
                  private var penguinTexture:BitmapFileMaterial = new BitmapFileMaterial("Penguin.png"); // here you can set the texture image for the penguin
                  private var groundTexture:BitmapFileMaterial = new BitmapFileMaterial("grass2.jpg");
                  private var plane:Plane = new Plane(groundTexture, 2000, 2000, 10, 10);
                  private var sheep:MD2 = new MD2(true); //if you get errors try using false - it may be a single frame file (no animation)
                  private var penguin:MD2 = new MD2(true);
                  private var bottom:Boolean = false;
                  private var endGame:Boolean = false;
                  
             
                  public function @ax-student-papervision-samples-CF07() {
                          init(600, 300);
                          
                          stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
                          stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
                  }
                  
                  override protected function init3d():void{
                          //plane.material.lineColor = 0x777777;
                          plane.material.doubleSided = true;
                          plane.pitch(90);
                          plane.y = -50;
                          default_scene.addChild(plane);
                          
                          default_camera.x = 0;
                          default_camera.z = 1000;
                          default_camera.y = 1000;
                          
                          sheep.load("sheep-walk.md2", sheepTexture); // This piece of code loads in the .md2-model
                          sheep.y = 200;
                          sheep.scale = 13;
                          sheep.rotationX = -90;
                          default_scene.addChild(sheep);
                          
                          for (var i:Number = 0; i < 10; i++) {
                                  bullets[i] = new Sphere(mat, 25, 10, 10);
                                  enemyBullets[i] = new Sphere(mat, 25, 10, 10);
                          }
                          
                          penguin.load("penguin.md2", penguinTexture); // This piece of code loads in the .md2-model
                          penguin.y = 150;
                          penguin.x = -300;
                          penguin.z = 300;
                          penguin.scale = 9;
                          penguin.rotationX = -90;
                          default_scene.addChild(penguin);
                          Tweener.addTween(sheep, { x: -500, z: -500, time:2, onComplete: randomize } );
                          
                          sheepSphere = new Sphere(mat, 75, 10, 10);
                          sheepSphere.x = sheep.x;
                          sheepSphere.y = sheep.y;
                          sheepSphere.z = sheep.z;
                          
                          penguinSphere = new Sphere(mat, 50, 10, 10);
                          penguinSphere.x = penguin.x;
                          penguinSphere.y = penguin.y;
                          penguinSphere.z = penguin.z;
                  }
                  
                  override protected function init2d():void {
                          addChild(text);        
                          text.selectable = false;
                          text.x = 0;
                          text.y = 150;
                          text.width = 800;
                          text.height = 300;
                          text.text = "Click Here To Start";
                          text.setTextFormat(startgameFontstyle);
                          text.addEventListener( MouseEvent.CLICK, onClick);
                  }
                  
                  public function randomize():void {
                  }
                  
                  public function reset():void {
                          bottom = false;
                  }
                  
                  private function onClick( e:MouseEvent ):void {
                          gameStart = true;
                          text.setTextFormat(fontstyle);
                          text.width = 800;
                          text.height = 300;
                          text.x = 0;
                          text.y = 0;
                          text.text = "Multimedia Authoring - ~vr0815";
                          text.removeEventListener(MouseEvent.CLICK, onClick);
                          
                  }
                  
                  public function onKeyDown( event:KeyboardEvent ):void {
                          switch(event.keyCode) {
                                  case 32:
                                          spacedown = true;
                                  break;
                                  case 38:
                                          updown = true;
                                  break;
                                  case 37:
                                          leftdown = true;
                                  break;
                                  case 40:
                                          downdown = true;
                                  break;
                                  case 39:
                                          rightdown = true;
                                  break;
                          }
                  }
                  
                  public function onKeyUp( event:KeyboardEvent ):void {
                          switch(event.keyCode) {
                                  case 32:
                                          spacedown = false;
                                  break;
                                  case 38:
                                          updown = false;
                                  break;
                                  case 37:
                                          leftdown = false;
                                  break;
                                  case 40:
                                          downdown = false;
                                  break;
                                  case 39:
                                          rightdown = false;
                                  break;
                          }
                  }
                  
                  override protected function processFrame():void {
                          if(gameStart) {
                                  var xp:Number = 0;
                                  var yp:Number = 0;
                                  var zp:Number = 0;
                                  
                                  if(rotationBase == rotationPosition % 360) {
                                          if (updown && leftdown) {
                                                  xp = sheep.x + speed;
                                                  yp = sheep.z + speed;
                                                  rotationGoal = 315; // RotationGoal keeps track of which direction you want to rotate to, in degrees.
                                                  Tweener.addTween(sheep, { x:xp, z:yp, time:2, onComplete: randomize } );
                                          } else if (updown && rightdown) {
                                                  xp = sheep.x + speed;
                                                  yp = sheep.z - speed;
                                                  rotationGoal = 45;
                                                  Tweener.addTween(sheep, { x:xp, z:yp, time:2, onComplete: randomize } );
                                          } else if (downdown && leftdown) {
                                                  xp = sheep.x - speed;
                                                  yp = sheep.z + speed;
                                                  rotationGoal = 225;
                                                  Tweener.addTween(sheep, { x:xp, z:yp, time:2, onComplete: randomize } );
                                          } else if (downdown && rightdown) {
                                                  xp = sheep.x - speed;
                                                  yp = sheep.z - speed;
                                                  rotationGoal = 135;
                                                  Tweener.addTween(sheep, { x:xp, z:yp, time:2, onComplete: randomize } );
                                          } else if (updown) {
                                                  xp = sheep.x + speed;
                                                  yp = sheep.z;
                                                  rotationGoal = 0;
                                                  Tweener.addTween(sheep, { x:xp, z:yp, time:2, onComplete: randomize } );
                                          } else if (downdown) {
                                                  xp = sheep.x - speed;
                                                  yp = sheep.z;
                                                  rotationGoal = 180;
                                                  Tweener.addTween(sheep, { x:xp, z:yp, time:2, onComplete: randomize } );
                                          } else if (leftdown) {
                                                  xp = sheep.x;
                                                  yp = sheep.z + speed;
                                                  rotationGoal = 270;
                                                  Tweener.addTween(sheep, { x:xp, z:yp, time:2, onComplete: randomize } );
                                          } else if (rightdown) {
                                                  xp = sheep.x;
                                                  yp = sheep.z - speed;
                                                  rotationGoal = 90;
                                                  Tweener.addTween(sheep, { x:xp, z:yp, time:2, onComplete: randomize } );
                                          }
                                  }
                  
                                  if (rotationPosition % 360 != rotationGoal) { //this piece of code rotates the player left or right, depending on which route is shorter. 
                                          if((((rotationBase % 360 - rotationGoal) < 0) && ((rotationBase % 360 - rotationGoal) > -181)) || ((rotationBase % 360 - rotationGoal) > 180)) {
                                                  sheep.rotationY = sheep.rotationY + 15;
                                                  rotationPosition = rotationPosition + 15;
                                          } else {
                                                  sheep.rotationY = sheep.rotationY - 15;
                                                  rotationPosition = rotationPosition - 15;
                                          }
                                  } else {
                                          rotationBase = rotationPosition % 360; 
                                  }
                                  
                                  if (updown || downdown || leftdown || rightdown) { // this piece of code animates the sheep if it's walking
                                          if(standing) {
                                                  sheep.play();
                                                  standing = false;
                                          }
                                  } else {
                                          sheep.stop();
                                          standing = true;
                                  }
                                  
                                  if (sheep.hitTestObject(plane) == false && bottom == false) { //this piece of code attempts some basic gravity
                                          yp = sheep.y - speed * 5;
                                          Tweener.addTween(sheep, { y:yp, time:2 } )
                                  }
                                  
                                  for (var i:Number = 0; i < 10; i++) { // If you're hit by an enemy projectile:
                                          if (enemyBullets[i].hitTestObject(sheepSphere) && endGame == false) {
                                                  text.text = "Game Over!";
                                                  current_scene = endgameScene;
                                                  text.setTextFormat(endgameFontstyle);
                                                  text.x = 0;
                                                  text.y = 150;
                                                  endGame = true;
                                          }
                                  }
                                  
                                  for (var j:Number = 0; j < 10; j++) { // If the enemy is hit by one of your projectiles:
                                          if(bullets[j].hitTestObject(penguinSphere) && endGame == false) {
                                                  current_scene = endgameScene;
                                                  text.text = "You Won!";
                                                  text.setTextFormat(endgameFontstyle);
                                                  text.x = 0;
                                                  text.y = 150;
                                                  endGame = true;
                                          }
                                  }
                                  
                                  if (spacedown) { // When space is pressed: shoot
                                          xp = Math.cos((Math.PI / 180) * (rotationPosition % 360)) * 20000;
                                          yp = Math.sin((Math.PI / 180) * (rotationPosition % 360)) * -20000;
                                          bullets[bulletsCounter].x = sheep.x;
                                          bullets[bulletsCounter].y = penguin.y;
                                          bullets[bulletsCounter].z = sheep.z;
                                          default_scene.addChild(bullets[bulletsCounter]);
                                          spacedown = false;
                                          Tweener.addTween(bullets[bulletsCounter], { x:xp, z:yp, time:100 } )
                                          bulletsCounter = (bulletsCounter + 1) % 10;
                                  } 
                                  
                                  if (enemyBulletsInterval == 0) { // This piece of code handles the enemy projectiles
                                          xp = (penguin.x - sheepSphere.x) * -20;
                                          zp = (penguin.z - sheepSphere.z) * -20;
                                          enemyBullets[enemyBulletsCounter].x = penguin.x;
                                          enemyBullets[enemyBulletsCounter].y = sheepSphere.y;
                                          enemyBullets[enemyBulletsCounter].z = penguin.z;
                                          default_scene.addChild(enemyBullets[enemyBulletsCounter]);
                                          Tweener.addTween(enemyBullets[enemyBulletsCounter], { x:xp, z:zp, time:100 } )
                                          enemyBulletsCounter = (enemyBulletsCounter + 1) % 10;
                                          enemyBulletsInterval = 50;
                                  }
                                  
                                  if (sheep.y < -10000) { //this piece of code handles simple gravity. 
                                          bottom = true;
                                          Tweener.addTween(sheep, { x:0, y:200, z:0, time:2, onComplete: reset } )
                                  }
                                  
                                  //these pieces of code update some values, for example keeping the camera looking at the sheep. 
                                  Tweener.addTween(default_camera, { x:sheep.x - 800, z:sheep.z, time:2, onComplete: randomize } )
                                  Tweener.addTween(sheepSphere, { x:sheep.x, y:sheep.y, z:sheep.z, time:2 } )
                                  rotationPosition = rotationPosition % 360;
                                  if (rotationPosition < 0) {
                                          rotationPosition = rotationPosition + 360;
                                  }
                                  enemyBulletsInterval = enemyBulletsInterval - 1;
                                  default_camera.lookAt(sheep);
                          }
                  }
          }
  }
  


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.