UUpdate Tutorials
This commit is contained in:
30
_tanks_networking/Sources/lnx/FireTrait.hx
Normal file
30
_tanks_networking/Sources/lnx/FireTrait.hx
Normal file
@ -0,0 +1,30 @@
|
||||
package lnx;
|
||||
|
||||
import kha.netsync.Entity;
|
||||
import kha.netsync.Sync;
|
||||
import iron.object.Object;
|
||||
|
||||
class FireTrait extends iron.Trait implements Entity implements Sync {
|
||||
public function new() {
|
||||
super();
|
||||
}
|
||||
|
||||
@synced("all")
|
||||
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(leenkx.trait.physics.RigidBody);
|
||||
if (rigidBody != null) rigidBody.syncTransform();
|
||||
|
||||
var vec = object.transform.look();
|
||||
vec.mult(7);
|
||||
rigidBody.applyImpulse(vec);
|
||||
|
||||
iron.system.Tween.timer(2, o.remove);
|
||||
});
|
||||
}
|
||||
}
|
||||
29
_tanks_networking/Sources/lnx/Player1Controls.hx
Normal file
29
_tanks_networking/Sources/lnx/Player1Controls.hx
Normal file
@ -0,0 +1,29 @@
|
||||
package lnx;
|
||||
|
||||
class Player1Controls extends iron.Trait {
|
||||
|
||||
public function new() {
|
||||
super();
|
||||
|
||||
notifyOnUpdate(function() {
|
||||
var keyboard = iron.system.Input.getKeyboard();
|
||||
var tank = object.getTrait(TankTrait);
|
||||
|
||||
if (keyboard.down("left")) {
|
||||
tank.turnLeft();
|
||||
}
|
||||
|
||||
if (keyboard.down("right")) {
|
||||
tank.turnRight();
|
||||
}
|
||||
|
||||
if (keyboard.down("up")) {
|
||||
tank.forward();
|
||||
}
|
||||
|
||||
if (keyboard.down("down")) {
|
||||
tank.backward();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
15
_tanks_networking/Sources/lnx/Player1Fire.hx
Normal file
15
_tanks_networking/Sources/lnx/Player1Fire.hx
Normal file
@ -0,0 +1,15 @@
|
||||
package lnx;
|
||||
|
||||
class Player1Fire extends iron.Trait {
|
||||
public function new() {
|
||||
super();
|
||||
|
||||
notifyOnUpdate(function() {
|
||||
var keyboard = iron.system.Input.getKeyboard();
|
||||
|
||||
if (keyboard.started("m")) {
|
||||
object.getTrait(FireTrait).fire();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
28
_tanks_networking/Sources/lnx/Player2Controls.hx
Normal file
28
_tanks_networking/Sources/lnx/Player2Controls.hx
Normal file
@ -0,0 +1,28 @@
|
||||
package lnx;
|
||||
|
||||
class Player2Controls extends iron.Trait {
|
||||
public function new() {
|
||||
super();
|
||||
|
||||
notifyOnUpdate(function() {
|
||||
var keyboard = iron.system.Input.getKeyboard();
|
||||
var tank = object.getTrait(TankTrait);
|
||||
|
||||
if (keyboard.down("a")) {
|
||||
tank.turnLeft();
|
||||
}
|
||||
|
||||
if (keyboard.down("d")) {
|
||||
tank.turnRight();
|
||||
}
|
||||
|
||||
if (keyboard.down("w")) {
|
||||
tank.forward();
|
||||
}
|
||||
|
||||
if (keyboard.down("s")) {
|
||||
tank.backward();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
15
_tanks_networking/Sources/lnx/Player2Fire.hx
Normal file
15
_tanks_networking/Sources/lnx/Player2Fire.hx
Normal file
@ -0,0 +1,15 @@
|
||||
package lnx;
|
||||
|
||||
class Player2Fire extends iron.Trait {
|
||||
public function new() {
|
||||
super();
|
||||
|
||||
notifyOnUpdate(function() {
|
||||
var keyboard = iron.system.Input.getKeyboard();
|
||||
|
||||
if (keyboard.started("f")) {
|
||||
object.getTrait(FireTrait).fire();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
33
_tanks_networking/Sources/lnx/SessionTrait.hx
Normal file
33
_tanks_networking/Sources/lnx/SessionTrait.hx
Normal file
@ -0,0 +1,33 @@
|
||||
package lnx;
|
||||
|
||||
class SessionTrait extends iron.Trait {
|
||||
|
||||
public static var session:kha.netsync.Session = null;
|
||||
|
||||
public function new() {
|
||||
super();
|
||||
|
||||
session = new kha.netsync.Session(2, "localhost", 6789);
|
||||
session.waitForStart(startSession, onSessionRefused, onSessionError, onSessionClosed, resetSession);
|
||||
}
|
||||
|
||||
function startSession() {
|
||||
|
||||
}
|
||||
|
||||
function onSessionRefused() {
|
||||
|
||||
}
|
||||
|
||||
function onSessionError() {
|
||||
|
||||
}
|
||||
|
||||
function onSessionClosed() {
|
||||
|
||||
}
|
||||
|
||||
function resetSession() {
|
||||
|
||||
}
|
||||
}
|
||||
66
_tanks_networking/Sources/lnx/TankTrait.hx
Normal file
66
_tanks_networking/Sources/lnx/TankTrait.hx
Normal file
@ -0,0 +1,66 @@
|
||||
package lnx;
|
||||
|
||||
import kha.netsync.Entity;
|
||||
import kha.netsync.Sync;
|
||||
|
||||
class TankTrait extends iron.Trait implements Entity implements Sync {
|
||||
|
||||
static var q = new iron.math.Quat();
|
||||
var transform:iron.object.Transform;
|
||||
|
||||
public function new() {
|
||||
super();
|
||||
|
||||
notifyOnInit(function() {
|
||||
transform = object.transform;
|
||||
});
|
||||
|
||||
notifyOnUpdate(register);
|
||||
}
|
||||
|
||||
function register() {
|
||||
if (SessionTrait.session != null) {
|
||||
SessionTrait.session.addEntity(this);
|
||||
removeUpdate(register);
|
||||
}
|
||||
}
|
||||
|
||||
@synced("all")
|
||||
public function turnLeft() {
|
||||
q.fromEuler(0, 0, 0.03);
|
||||
transform.rot.mult(q);
|
||||
transform.buildMatrix();
|
||||
syncRigidBody();
|
||||
}
|
||||
|
||||
@synced("all")
|
||||
public function turnRight() {
|
||||
q.fromEuler(0, 0, -0.03);
|
||||
transform.rot.mult(q);
|
||||
transform.buildMatrix();
|
||||
syncRigidBody();
|
||||
}
|
||||
|
||||
@synced("all")
|
||||
public function forward() {
|
||||
var vec = transform.look();
|
||||
vec.mult(0.05);
|
||||
transform.loc.add(vec);
|
||||
transform.buildMatrix();
|
||||
syncRigidBody();
|
||||
}
|
||||
|
||||
@synced("all")
|
||||
public function backward() {
|
||||
var vec = transform.look();
|
||||
vec.mult(-0.05);
|
||||
transform.loc.add(vec);
|
||||
transform.buildMatrix();
|
||||
syncRigidBody();
|
||||
}
|
||||
|
||||
function syncRigidBody() {
|
||||
var rigidBody = object.getTrait(leenkx.trait.physics.RigidBody);
|
||||
if (rigidBody != null) rigidBody.syncTransform();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user