package arm; import iron.math.Vec4; import iron.system.Input; import iron.object.Object; import armory.trait.physics.PhysicsWorld; import armory.trait.internal.CameraController; import armory.trait.physics.RigidBody; import iron.system.Time; class Galinha extends CameraController { #if (!arm_bullet) public function new() { super(); } #else static inline var rotationSpeed = 1.0; var currentAction:String; var marioneta:Object; var fireFreq = 0.15; var firePoint:Object; var lastFire = 0.0; var turnToRight = true; var pivotCamara:Object; public function new() { super(); currentAction = 'galinha_Idle'; iron.Scene.active.notifyOnInit(init); } function findAnimation(o:Object):Object { if (o.animation != null) return o; for (c in o.children) { var co = findAnimation(c); if (co != null) return co; } return null; } function init() { /*if (animObject == "") arm = findAnimation(object); else arm = object.getChild(animObject);*/ //marioneta = object.getChild('galinha_esqueleto'); marioneta = findAnimation(object); PhysicsWorld.active.notifyOnPreUpdate(preUpdate); notifyOnUpdate(update); notifyOnRemove(removed); firePoint = object.getChild('ProjectileSpawn'); pivotCamara = object.getChild('PivotCamara'); } var xVec = Vec4.xAxis(); var zVec = Vec4.zAxis(); function preUpdate() { if (Input.occupied || !body.ready) return; var mouse = Input.getMouse(); var gamepad = Input.getGamepad(0); /*if (mouse.down()) { camera.transform.rotate(xVec, mouse.movementY / 250 * rotationSpeed); transform.rotate(zVec, -mouse.movementX / 250 * rotationSpeed); camera.buildMatrix(); body.syncTransform(); }*/ } function removed() { PhysicsWorld.active.removePreUpdate(preUpdate); } var dir = new Vec4(); function update() { lastFire += Time.delta; if (lastFire > fireFreq) { var mouse = Input.getMouse(); var gamepad = Input.getGamepad(0); if (mouse.down("left") || (gamepad != null && gamepad.down("r2") > 0.0)) { shoot(); } } if (!body.ready) return; if (jump) body.applyImpulse(new Vec4(0, 0, 25)); // Move dir.set(0, 0, 0); /* if (moveForward) dir.add(transform.look()); W if (moveBackward) dir.add(transform.look().mult(-1)); S if (moveLeft) dir.add(transform.right().mult(-1)); A if (moveRight) dir.add(transform.right()); D */ //if (moveForward) dir.add(transform.right()); //if (moveBackward) dir.add(transform.right().mult(-1)); //if (moveLeft) dir.add(transform.look()); //if (moveRight) dir.add(transform.look().mult(-1)); if (moveLeft){ if(turnToRight){ var Vec4 = iron.math.Vec4; transform.rotate(Vec4.zAxis(), 3.1415926536); body.syncTransform(); pivotCamara.transform.rotate(Vec4.zAxis(), -3.1415926536); turnToRight = false; } dir.add(transform.look().mult(-1)); } if (moveRight){ if(!turnToRight){ var Vec4 = iron.math.Vec4; transform.rotate(Vec4.zAxis(), -3.1415926536); body.syncTransform(); pivotCamara.transform.rotate(Vec4.zAxis(), 3.1415926536); turnToRight = true; } dir.add(transform.look().mult(-1)); } // Push down var btvec = body.getLinearVelocity(); body.setLinearVelocity(0.0, 0.0, btvec.z - 1.0); if (moveLeft || moveRight) { if (currentAction != 'galinha_Walk') { marioneta.animation.play( 'galinha_Walk', null, 0.2); currentAction = 'galinha_Walk'; } dir.mult(-4 * 2.5); body.activate(); body.setLinearVelocity(dir.x, dir.y, btvec.z - 1.0); } else { if (currentAction != 'galinha_Idle') { marioneta.animation.play( 'galinha_Idle', null, 0.2); currentAction = 'galinha_Idle'; } } // Keep vertical body.setAngularFactor(0, 0, 0); camera.buildMatrix(); } function shoot() { // Spawn projectile iron.Scene.active.spawnObject('Projectile', null, function(o:Object) { o.transform.loc.x = firePoint.transform.worldx(); o.transform.loc.y = firePoint.transform.worldy(); o.transform.loc.z = firePoint.transform.worldz(); o.transform.buildMatrix(); // Apply force var rb:RigidBody = o.getTrait(RigidBody); rb.syncTransform(); var look = object.transform.look().normalize(); rb.setLinearVelocity(look.x * 15, look.y * 15, look.z * 15); // Remove projectile after a period of time kha.Scheduler.addTimeTask(o.remove, 10); }); lastFire = 0.0; } #end }