52 lines
1.3 KiB
JavaScript
52 lines
1.3 KiB
JavaScript
// Copyright (c) 2026 Dominic Masters
|
|
//
|
|
// This software is released under the MIT License.
|
|
// https://opensource.org/licenses/MIT
|
|
|
|
const PLAYER_SPEED = 5.0;
|
|
|
|
var player = {};
|
|
|
|
var _entity, _position, _physics;
|
|
|
|
player.create = function(texEntry) {
|
|
_entity = Entity.create();
|
|
_position = _entity.add(Component.POSITION);
|
|
_physics = _entity.add(Component.PHYSICS);
|
|
|
|
_physics.bodyType = Physics.DYNAMIC;
|
|
_physics.shape = Physics.SHAPE_CUBE;
|
|
_physics.gravityScale = 1.0;
|
|
|
|
var r = _entity.add(Component.RENDERABLE);
|
|
r.texture = texEntry.texture;
|
|
r.type = Renderable.SPRITEBATCH;
|
|
r.color = new Color(220, 80, 80);
|
|
// upright quad: (-0.5,0,0) → (0.5,1,0) in XY plane
|
|
r.sprites = [[-0.5, 0, 0, 0.5, 1, 0, 0, 0, 1, 1]];
|
|
|
|
_position.localPosition = new Vec3(0, 1, 0);
|
|
};
|
|
|
|
player.getPosition = function() {
|
|
return _position;
|
|
};
|
|
|
|
player.update = function() {
|
|
if(!_physics) return;
|
|
var vx = Input.axis(INPUT_ACTION_LEFT, INPUT_ACTION_RIGHT) * PLAYER_SPEED;
|
|
var vz = Input.axis(INPUT_ACTION_DOWN, INPUT_ACTION_UP) * PLAYER_SPEED;
|
|
// Preserve vertical velocity so gravity and landing work correctly.
|
|
var vy = _physics.velocity.y;
|
|
_physics.velocity = new Vec3(vx, vy, vz);
|
|
};
|
|
|
|
player.dispose = function() {
|
|
Entity.dispose(_entity);
|
|
_entity = null;
|
|
_position = null;
|
|
_physics = null;
|
|
};
|
|
|
|
module.exports = player;
|