UUpdate Tutorials
This commit is contained in:
4
tanks_script/README.md
Normal file
4
tanks_script/README.md
Normal file
@ -0,0 +1,4 @@
|
||||
- Player1: `Up Left Down Right`, `Space`
|
||||
- Player2: `W A S D`, `F`
|
||||
|
||||
https://github.com/lnxnkx3d/lnxnkx/wiki/tanks
|
||||
28
tanks_script/Sources/lnx/FireTrait.hx
Normal file
28
tanks_script/Sources/lnx/FireTrait.hx
Normal file
@ -0,0 +1,28 @@
|
||||
package lnx;
|
||||
|
||||
import leenkx.trait.physics.RigidBody;
|
||||
import iron.object.Object;
|
||||
|
||||
class FireTrait extends iron.Trait {
|
||||
public function new() {
|
||||
super();
|
||||
}
|
||||
|
||||
public function fire() {
|
||||
iron.Scene.active.spawnObject("Bullet", null, function(o:Object) {
|
||||
o.visible = true;
|
||||
object.transform.buildMatrix();
|
||||
o.transform.loc.setFrom(object.transform.world.getLoc());
|
||||
o.transform.buildMatrix();
|
||||
|
||||
var rigidBody = o.getTrait(RigidBody);
|
||||
if (rigidBody != null) rigidBody.syncTransform();
|
||||
|
||||
var vec = object.transform.look();
|
||||
vec.mult(7);
|
||||
rigidBody.applyImpulse(vec);
|
||||
|
||||
iron.system.Tween.timer(2, o.remove);
|
||||
});
|
||||
}
|
||||
}
|
||||
30
tanks_script/Sources/lnx/Player1Controls.hx
Normal file
30
tanks_script/Sources/lnx/Player1Controls.hx
Normal file
@ -0,0 +1,30 @@
|
||||
package lnx;
|
||||
|
||||
class Player1Controls extends iron.Trait {
|
||||
|
||||
public function new() {
|
||||
super();
|
||||
|
||||
notifyOnUpdate(function() {
|
||||
var keyboard = iron.system.Input.getKeyboard();
|
||||
var gamepad = iron.system.Input.getGamepad(0);
|
||||
var tank = object.getTrait(TankTrait);
|
||||
|
||||
if (keyboard.down("left") || gamepad.down("left") > 0.0) {
|
||||
tank.turnLeft();
|
||||
}
|
||||
|
||||
if (keyboard.down("right") || gamepad.down("right") > 0.0) {
|
||||
tank.turnRight();
|
||||
}
|
||||
|
||||
if (keyboard.down("up") || gamepad.down("up") > 0.0) {
|
||||
tank.forward();
|
||||
}
|
||||
|
||||
if (keyboard.down("down") || gamepad.down("down") > 0.0) {
|
||||
tank.backward();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
16
tanks_script/Sources/lnx/Player1Fire.hx
Normal file
16
tanks_script/Sources/lnx/Player1Fire.hx
Normal file
@ -0,0 +1,16 @@
|
||||
package lnx;
|
||||
|
||||
class Player1Fire extends iron.Trait {
|
||||
public function new() {
|
||||
super();
|
||||
|
||||
notifyOnUpdate(function() {
|
||||
var keyboard = iron.system.Input.getKeyboard();
|
||||
var gamepad = iron.system.Input.getGamepad(0);
|
||||
|
||||
if (keyboard.started("space") || gamepad.started("cross")) {
|
||||
object.getTrait(FireTrait).fire();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
29
tanks_script/Sources/lnx/Player2Controls.hx
Normal file
29
tanks_script/Sources/lnx/Player2Controls.hx
Normal file
@ -0,0 +1,29 @@
|
||||
package lnx;
|
||||
|
||||
class Player2Controls extends iron.Trait {
|
||||
public function new() {
|
||||
super();
|
||||
|
||||
notifyOnUpdate(function() {
|
||||
var keyboard = iron.system.Input.getKeyboard();
|
||||
var gamepad = iron.system.Input.getGamepad(1);
|
||||
var tank = object.getTrait(TankTrait);
|
||||
|
||||
if (keyboard.down("a") || gamepad.down("left") > 0.0) {
|
||||
tank.turnLeft();
|
||||
}
|
||||
|
||||
if (keyboard.down("d") || gamepad.down("right") > 0.0) {
|
||||
tank.turnRight();
|
||||
}
|
||||
|
||||
if (keyboard.down("w") || gamepad.down("up") > 0.0) {
|
||||
tank.forward();
|
||||
}
|
||||
|
||||
if (keyboard.down("s") || gamepad.down("down") > 0.0) {
|
||||
tank.backward();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
16
tanks_script/Sources/lnx/Player2Fire.hx
Normal file
16
tanks_script/Sources/lnx/Player2Fire.hx
Normal file
@ -0,0 +1,16 @@
|
||||
package lnx;
|
||||
|
||||
class Player2Fire extends iron.Trait {
|
||||
public function new() {
|
||||
super();
|
||||
|
||||
notifyOnUpdate(function() {
|
||||
var keyboard = iron.system.Input.getKeyboard();
|
||||
var gamepad = iron.system.Input.getGamepad(1);
|
||||
|
||||
if (keyboard.started("f") || gamepad.started("cross")) {
|
||||
object.getTrait(FireTrait).fire();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
52
tanks_script/Sources/lnx/TankTrait.hx
Normal file
52
tanks_script/Sources/lnx/TankTrait.hx
Normal file
@ -0,0 +1,52 @@
|
||||
package lnx;
|
||||
|
||||
import leenkx.trait.physics.RigidBody;
|
||||
|
||||
class TankTrait extends iron.Trait {
|
||||
|
||||
static var q = new iron.math.Quat();
|
||||
var transform:iron.object.Transform;
|
||||
|
||||
public function new() {
|
||||
super();
|
||||
|
||||
notifyOnInit(function() {
|
||||
transform = object.transform;
|
||||
});
|
||||
}
|
||||
|
||||
public function turnLeft() {
|
||||
q.fromEuler(0, 0, 0.03);
|
||||
transform.rot.mult(q);
|
||||
transform.buildMatrix();
|
||||
syncRigidBody();
|
||||
}
|
||||
|
||||
public function turnRight() {
|
||||
q.fromEuler(0, 0, -0.03);
|
||||
transform.rot.mult(q);
|
||||
transform.buildMatrix();
|
||||
syncRigidBody();
|
||||
}
|
||||
|
||||
public function forward() {
|
||||
var vec = transform.look();
|
||||
vec.mult(0.05);
|
||||
transform.loc.add(vec);
|
||||
transform.buildMatrix();
|
||||
syncRigidBody();
|
||||
}
|
||||
|
||||
public function backward() {
|
||||
var vec = transform.look();
|
||||
vec.mult(-0.05);
|
||||
transform.loc.add(vec);
|
||||
transform.buildMatrix();
|
||||
syncRigidBody();
|
||||
}
|
||||
|
||||
function syncRigidBody() {
|
||||
var rigidBody = object.getTrait(RigidBody);
|
||||
if (rigidBody != null) rigidBody.syncTransform();
|
||||
}
|
||||
}
|
||||
BIN
tanks_script/tanks_script.blend
Normal file
BIN
tanks_script/tanks_script.blend
Normal file
Binary file not shown.
Reference in New Issue
Block a user