forked from LeenkxTeam/Leenkx_Templates
Update
This commit is contained in:
BIN
twin_stick/Assets/checker_rough.png
Normal file
BIN
twin_stick/Assets/checker_rough.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 787 B |
BIN
twin_stick/Assets/finger.jpg
Normal file
BIN
twin_stick/Assets/finger.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 85 KiB |
BIN
twin_stick/Assets/grid.png
Normal file
BIN
twin_stick/Assets/grid.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 414 B |
BIN
twin_stick/Assets/grid_b.png
Normal file
BIN
twin_stick/Assets/grid_b.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 672 B |
0
twin_stick/Bundled/canvas/MyCanvas.files
Normal file
0
twin_stick/Bundled/canvas/MyCanvas.files
Normal file
1
twin_stick/Bundled/canvas/MyCanvas.json
Normal file
1
twin_stick/Bundled/canvas/MyCanvas.json
Normal file
@ -0,0 +1 @@
|
||||
{"name":"untitled","x":0,"y":0,"width":1280,"height":720,"elements":[{"id":15,"type":9,"name":"MenuContainer","event":"","x":10,"y":61,"width":332,"height":402,"rotation":0,"text":"My Shape","asset":"","color":-1258291200,"anchor":0,"parent":null,"children":[2,3,6,8,11,12,13,14],"visible":true},{"id":2,"type":6,"name":"SSR","event":"","x":24,"y":179,"width":150,"height":44,"rotation":0,"text":"SSR","asset":"","color":-1,"anchor":0,"parent":15,"children":[],"visible":true},{"id":3,"type":6,"name":"SSAO","event":"","x":24,"y":229,"width":150,"height":44,"rotation":0,"text":"SSAO","asset":"","color":-1,"anchor":0,"parent":15,"children":[],"visible":true},{"id":6,"type":6,"name":"Bloom","event":"","x":24,"y":129,"width":150,"height":44,"rotation":0,"text":"Bloom","asset":"","color":-1,"anchor":0,"parent":15,"children":[],"visible":true},{"id":8,"type":8,"name":"Shadows","event":"","x":24,"y":79,"width":150,"height":44,"rotation":0,"text":"High;Medium;Low","asset":"","color":-1,"anchor":0,"parent":15,"children":[],"visible":true},{"id":11,"type":0,"name":"Title","event":"","x":4,"y":9,"width":377,"height":44,"rotation":0,"text":"Graphics Settings","asset":"","color":-1,"anchor":0,"parent":15,"children":[],"visible":true},{"id":12,"type":0,"name":"ShadowsTitle","event":"","x":164,"y":79,"width":184,"height":28,"rotation":0,"text":"Shadows","asset":"","color":-6381922,"anchor":0,"parent":15,"children":[],"visible":true},{"id":13,"type":2,"name":"Apply","event":"apply_settings","x":24,"y":329,"width":150,"height":44,"rotation":0,"text":"Apply","asset":"","color":-1,"anchor":0,"parent":15,"children":[],"visible":true},{"id":14,"type":6,"name":"Voxels","event":"","x":24,"y":279,"width":150,"height":44,"rotation":0,"text":"Voxels","asset":"","color":-1,"anchor":0,"parent":15,"children":[],"visible":true},{"id":16,"type":2,"name":"Menu","event":"toggle_menu","x":5,"y":5,"width":150,"height":44,"rotation":0,"text":"Menu","asset":"","color":-1,"anchor":0,"parent":null,"children":[],"visible":true}],"assets":[]}
|
||||
BIN
twin_stick/Bundled/fire0.wav
Normal file
BIN
twin_stick/Bundled/fire0.wav
Normal file
Binary file not shown.
BIN
twin_stick/Bundled/fire1.wav
Normal file
BIN
twin_stick/Bundled/fire1.wav
Normal file
Binary file not shown.
BIN
twin_stick/Bundled/step0.wav
Normal file
BIN
twin_stick/Bundled/step0.wav
Normal file
Binary file not shown.
BIN
twin_stick/Bundled/step1.wav
Normal file
BIN
twin_stick/Bundled/step1.wav
Normal file
Binary file not shown.
73
twin_stick/Sources/lnx/GunController.hx
Normal file
73
twin_stick/Sources/lnx/GunController.hx
Normal file
@ -0,0 +1,73 @@
|
||||
package lnx;
|
||||
|
||||
import iron.Trait;
|
||||
import iron.system.Input;
|
||||
import iron.system.Audio;
|
||||
import iron.system.Time;
|
||||
import iron.object.Object;
|
||||
import iron.object.Transform;
|
||||
import leenkx.trait.physics.RigidBody;
|
||||
|
||||
class GunController extends Trait {
|
||||
|
||||
#if (!lnx_physics)
|
||||
public function new() { super(); }
|
||||
#else
|
||||
|
||||
@prop
|
||||
public var fireFreq = 0.15;
|
||||
var firePoint:Transform;
|
||||
var lastFire = 0.0;
|
||||
|
||||
var soundFire0:kha.Sound = null;
|
||||
var soundFire1:kha.Sound = null;
|
||||
var soundsLoaded = 0;
|
||||
|
||||
public function new() {
|
||||
super();
|
||||
|
||||
iron.data.Data.getSound("fire0.wav", function(sound:kha.Sound) { soundFire0 = sound; soundsLoaded++; });
|
||||
iron.data.Data.getSound("fire1.wav", function(sound:kha.Sound) { soundFire1 = sound; soundsLoaded++; });
|
||||
|
||||
var mouse = Input.getMouse();
|
||||
var gamepad = Input.getGamepad(0);
|
||||
|
||||
notifyOnInit(function() {
|
||||
firePoint = object.getChild('ProjectileSpawn').transform;
|
||||
});
|
||||
|
||||
notifyOnUpdate(function() {
|
||||
lastFire += Time.delta;
|
||||
if (lastFire > fireFreq) {
|
||||
if (mouse.down("left") || (gamepad != null && gamepad.down("r2") > 0.0)) {
|
||||
shoot();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function shoot() {
|
||||
// Spawn projectile
|
||||
iron.Scene.active.spawnObject('Projectile', null, function(o:Object) {
|
||||
o.transform.loc.x = firePoint.worldx();
|
||||
o.transform.loc.y = firePoint.worldy();
|
||||
o.transform.loc.z = firePoint.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);
|
||||
});
|
||||
|
||||
// Play sound
|
||||
if (soundsLoaded == 2) {
|
||||
Audio.play(Std.random(3) == 0 ? soundFire1 : soundFire0);
|
||||
}
|
||||
|
||||
lastFire = 0.0;
|
||||
}
|
||||
#end
|
||||
}
|
||||
185
twin_stick/Sources/lnx/PlayerController.hx
Normal file
185
twin_stick/Sources/lnx/PlayerController.hx
Normal file
@ -0,0 +1,185 @@
|
||||
package lnx;
|
||||
|
||||
import iron.math.Vec4;
|
||||
import iron.math.Vec2;
|
||||
import iron.math.RayCaster;
|
||||
import iron.Scene;
|
||||
import iron.object.Object;
|
||||
import iron.object.BoneAnimation;
|
||||
import iron.system.Time;
|
||||
import iron.system.Audio;
|
||||
import iron.system.Input;
|
||||
import leenkx.trait.physics.RigidBody;
|
||||
|
||||
class PlayerController extends iron.Trait {
|
||||
|
||||
#if (!lnx_physics)
|
||||
public function new() { super(); }
|
||||
#else
|
||||
|
||||
var soundStep0:kha.Sound = null;
|
||||
var soundStep1:kha.Sound = null;
|
||||
var soundsLoaded = 0;
|
||||
|
||||
var mouse:Mouse = null;
|
||||
var keyboard:Keyboard = null;
|
||||
var gamepad:Gamepad = null;
|
||||
|
||||
var body:RigidBody;
|
||||
var anim:BoneAnimation;
|
||||
var armature:Object;
|
||||
|
||||
var stepTime = 0.0;
|
||||
var turnTime = 0.0;
|
||||
var dir = new Vec4();
|
||||
var lastLook:Vec4;
|
||||
var state = "idle";
|
||||
|
||||
public function new() {
|
||||
super();
|
||||
|
||||
// Load sounds
|
||||
iron.data.Data.getSound("step0.wav", function(sound:kha.Sound) { soundStep0 = sound; soundsLoaded++; });
|
||||
iron.data.Data.getSound("step1.wav", function(sound:kha.Sound) { soundStep1 = sound; soundsLoaded++; });
|
||||
|
||||
notifyOnInit(init);
|
||||
notifyOnUpdate(update);
|
||||
}
|
||||
|
||||
function init() {
|
||||
// Get input devices
|
||||
mouse = Input.getMouse();
|
||||
keyboard = Input.getKeyboard();
|
||||
gamepad = Input.getGamepad(0);
|
||||
|
||||
// Store references
|
||||
body = object.getTrait(RigidBody);
|
||||
armature = object.getChild("Armature");
|
||||
anim = cast armature.children[0].animation;
|
||||
lastLook = armature.transform.look().normalize();
|
||||
}
|
||||
|
||||
function update() {
|
||||
// Movement
|
||||
dir.set(0, 0, 0);
|
||||
if (keyboard.down("w")) dir.y = 1.0;
|
||||
if (keyboard.down("s")) dir.y = -1.0;
|
||||
if (keyboard.down("a")) dir.x = -1.0;
|
||||
if (keyboard.down("d")) dir.x = 1.0;
|
||||
if (gamepad != null && Math.abs(gamepad.leftStick.x) > 0.1) dir.x = gamepad.leftStick.x;
|
||||
if (gamepad != null && Math.abs(gamepad.leftStick.y) > 0.1) dir.y = gamepad.leftStick.y;
|
||||
dir.normalize();
|
||||
|
||||
// Mouse control
|
||||
var mouse_pos = new Vec2(mouse.x,mouse.y);
|
||||
var hit_pos = project_mouse_pos(mouse_pos);
|
||||
|
||||
if (hit_pos != null)
|
||||
{
|
||||
var center = new Vec4(hit_pos.x,hit_pos.y,0);
|
||||
var eye = armature.transform.world.getLoc();
|
||||
eye.set(eye.x,eye.y,0);
|
||||
|
||||
var target = center.sub(eye);
|
||||
|
||||
armature.transform.rot.fromTo(Vec4.yAxis(), target.normalize());
|
||||
}
|
||||
|
||||
// Gamepad control
|
||||
if (gamepad != null) {
|
||||
if (Math.abs(gamepad.rightStick.x) > 0.7 || Math.abs(gamepad.rightStick.y) > 0.7) {
|
||||
armature.transform.rot.fromTo(Vec4.yAxis(), new Vec4(gamepad.rightStick.x, gamepad.rightStick.y, 0.0));
|
||||
}
|
||||
}
|
||||
|
||||
armature.transform.buildMatrix();
|
||||
updateAnim();
|
||||
updateBody();
|
||||
}
|
||||
|
||||
function project_mouse_pos(input:Vec2){
|
||||
var camera = Scene.active.camera;
|
||||
|
||||
var start = new Vec4();
|
||||
var end = new Vec4();
|
||||
|
||||
var hit_pos = RayCaster.planeIntersect(Vec4.zAxis(),new Vec4(0,0,1),input.x,input.y,camera);
|
||||
return hit_pos;
|
||||
}
|
||||
|
||||
function getAngle(va:Vec4, vb:Vec4) {
|
||||
var vn = Vec4.zAxis();
|
||||
var dot = va.dot(vb);
|
||||
var det = va.x * vb.y * vn.z +
|
||||
vb.x * vn.y * va.z +
|
||||
vn.x * va.y * vb.z -
|
||||
va.z * vb.y * vn.x -
|
||||
vb.z * vn.y * va.x -
|
||||
vn.z * va.y * vb.x;
|
||||
return Math.atan2(det, dot);
|
||||
}
|
||||
|
||||
function updateAnim() {
|
||||
var look = armature.transform.look().normalize();
|
||||
|
||||
// Move
|
||||
if (dir.length() > 0) {
|
||||
var action = "";
|
||||
// Angle from look direction to move direction
|
||||
// 0 to PI * 2
|
||||
var angle = getAngle(dir, look) + Math.PI;
|
||||
var step = Math.PI / 4;
|
||||
if (angle < step) action = "back";
|
||||
else if (angle < step * 3) action = "left";
|
||||
else if (angle < step * 5) action = "run";
|
||||
else if (angle < step * 7) action = "right";
|
||||
else action = "back";
|
||||
|
||||
setState(action, 1.0);
|
||||
|
||||
// Step sounds
|
||||
stepTime += Time.delta;
|
||||
if (stepTime > 0.3) {
|
||||
stepTime = 0;
|
||||
if (soundsLoaded == 2) {
|
||||
Audio.play(Std.random(2) == 0 ? soundStep0 : soundStep1);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Shoot
|
||||
else if (mouse.down("left") || (gamepad != null && gamepad.down("r2") > 0.0)) {
|
||||
setState("fire", 2.0);
|
||||
}
|
||||
// Idle
|
||||
else {
|
||||
var angle = getAngle(look, lastLook);
|
||||
if (Math.abs(angle) > 0.01) {
|
||||
setState("turn", angle > 0 ? 1 : -1, 0);
|
||||
turnTime = 0;
|
||||
}
|
||||
else if (turnTime > 0.25){
|
||||
setState("idle", 2.0);
|
||||
}
|
||||
else turnTime += Time.delta;
|
||||
}
|
||||
|
||||
lastLook.setFrom(look);
|
||||
}
|
||||
|
||||
function updateBody() {
|
||||
if (!body.ready) return;
|
||||
|
||||
body.syncTransform();
|
||||
body.activate();
|
||||
var linvel = body.getLinearVelocity();
|
||||
body.setLinearVelocity(dir.x * 6, dir.y * 6, linvel.z - 1.0); // Push down
|
||||
body.setAngularFactor(0, 0, 0); // Keep vertical
|
||||
}
|
||||
|
||||
function setState(s:String, speed:Float, blend = 0.2) {
|
||||
if (s == state) return;
|
||||
state = s;
|
||||
anim.play(s, null, blend, speed);
|
||||
}
|
||||
#end
|
||||
}
|
||||
68
twin_stick/Sources/lnx/SettingsTrait.hx
Normal file
68
twin_stick/Sources/lnx/SettingsTrait.hx
Normal file
@ -0,0 +1,68 @@
|
||||
package lnx;
|
||||
|
||||
import iron.Scene;
|
||||
import leenkx.system.Event;
|
||||
import leenkx.data.Config;
|
||||
import leenkx.trait.internal.CanvasScript;
|
||||
import leenkx.renderpath.RenderPathCreator;
|
||||
|
||||
class SettingsTrait extends iron.Trait {
|
||||
|
||||
var canvas:CanvasScript;
|
||||
var envStrength = 0.0;
|
||||
|
||||
public function new() {
|
||||
super();
|
||||
|
||||
notifyOnInit(function() {
|
||||
canvas = Scene.active.getTrait(CanvasScript);
|
||||
|
||||
// Init UI to values loaded from config.lnx file
|
||||
canvas.notifyOnReady(function() {
|
||||
canvas.getElement("MenuContainer").visible = false;
|
||||
canvas.getHandle("SSAO").selected = Config.raw.rp_ssgi;
|
||||
canvas.getHandle("SSR").selected = Config.raw.rp_ssr;
|
||||
canvas.getHandle("Bloom").selected = Config.raw.rp_bloom;
|
||||
canvas.getHandle("Voxels").selected = Config.raw.rp_gi;
|
||||
canvas.getHandle("Shadows").position = getShadowQuality(Config.raw.rp_shadowmap_cascade);
|
||||
});
|
||||
|
||||
// Button events
|
||||
Event.add("toggle_menu", toggleMenu);
|
||||
Event.add("apply_settings", applySettings);
|
||||
});
|
||||
}
|
||||
|
||||
function toggleMenu() {
|
||||
var e = canvas.getElement("MenuContainer");
|
||||
e.visible = !e.visible;
|
||||
}
|
||||
|
||||
function applySettings() {
|
||||
|
||||
// Apply render path settings
|
||||
Config.raw.rp_ssgi = canvas.getHandle("SSAO").selected;
|
||||
Config.raw.rp_ssr = canvas.getHandle("SSR").selected;
|
||||
Config.raw.rp_bloom = canvas.getHandle("Bloom").selected;
|
||||
Config.raw.rp_gi = canvas.getHandle("Voxels").selected;
|
||||
Config.raw.rp_shadowmap_cascade = getShadowMapSize(canvas.getHandle("Shadows").position);
|
||||
RenderPathCreator.applyConfig();
|
||||
|
||||
// Lower envmap strength when voxel ao is disabled
|
||||
var p = iron.Scene.active.world.probe.raw;
|
||||
if (envStrength == 0) envStrength = p.strength;
|
||||
p.strength = Config.raw.rp_gi ? envStrength : envStrength / 3;
|
||||
|
||||
// Save config.lnx file
|
||||
Config.save();
|
||||
}
|
||||
|
||||
inline function getShadowQuality(i:Int):Int {
|
||||
// 0 - High, 1 - Medium, 2 - Low
|
||||
return i == 4096 ? 0 : i == 2048 ? 1 : 2;
|
||||
}
|
||||
|
||||
inline function getShadowMapSize(i:Int):Int {
|
||||
return i == 0 ? 4096 : i == 1 ? 2048 : 1024;
|
||||
}
|
||||
}
|
||||
BIN
twin_stick/twin_stick.blend
Normal file
BIN
twin_stick/twin_stick.blend
Normal file
Binary file not shown.
Reference in New Issue
Block a user