package { 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.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.text.TextField; import flash.text.TextFormat; public class student_papervision_samples_CF05 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 standing:Boolean = true; 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 cube2:Cube; private var mat:FlatShadeMaterial = new FlatShadeMaterial(new PointLight3D(), 0xFFFFFF, 0xFF0000); private var mat2:WireframeMaterial = new WireframeMaterial(0x00FF00); private var sheepTexture:BitmapFileMaterial = new BitmapFileMaterial("Sheep.png"); // here you can set the texture image for the player-model private var plane:Plane = new Plane(null, 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 bottom:Boolean = false; public function student_papervision_samples_CF05() { init(600, 300); stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown); stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp); } override protected function init3d():void{ cube2 = new Cube(new MaterialsList( { all: mat2 } ), 100, 100, 100); cube2.y = 0; cube2.x = 1000; cube2.z = 1000; plane.material.lineColor = 0x777777; plane.material.doubleSided = true; plane.pitch(90); plane.y = -50; default_scene.addChild(plane); default_scene.addChild(cube2); 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); Tweener.addTween(sheep, { x:500, z:500, time:2, onComplete: randomize } ); } override protected function init2d():void { addChild(text); text.selectable = false; text.setTextFormat(fontstyle); text.width = 800; text.text = "Multimedia Authoring - ~vr0815"; } public function randomize():void { cube2.x = sheep.x; cube2.z = sheep.z; } public function reset():void { bottom = false; } public function onKeyDown( event:KeyboardEvent ):void { switch(event.keyCode) { 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 38: updown = false; break; case 37: leftdown = false; break; case 40: downdown = false; break; case 39: rightdown = false; break; } } override protected function processFrame():void { var xp:Number = 0; var yp: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) { yp = sheep.y - speed * 5; Tweener.addTween(sheep, { y:yp, time:2 } ) } if (sheep.y < -10000) { bottom = true; Tweener.addTween(sheep, { x:0, y:200, z:0, time:2, onComplete: reset } ) } Tweener.addTween(default_camera, { x:sheep.x - 800, z:sheep.z, time:2, onComplete: randomize } ) default_camera.lookAt(sheep); rotationPosition = rotationPosition % 360; if (rotationPosition < 0) { rotationPosition = rotationPosition + 360; } } } }