64 lines
1.7 KiB
JavaScript
64 lines
1.7 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;
|
|
// 1 world unit = 16 pixels.
|
|
const PIXEL_SCALE = 1.0 / 16.0;
|
|
// Player sprite is 32x32 px (test.png dimensions).
|
|
const PLAYER_W = 32 * PIXEL_SCALE;
|
|
const PLAYER_H = 32 * PIXEL_SCALE;
|
|
|
|
var player = {};
|
|
|
|
player.getAssets = () => {
|
|
return [
|
|
{ path: 'test.png', type: Asset.TYPE_TEXTURE, format: Texture.FORMAT_RGBA }
|
|
];
|
|
}
|
|
|
|
player.init = function(scene) {
|
|
var texture = scene.assets.getAssetByPath('test.png');
|
|
Console.print('Player init: got texture ' + texture);
|
|
|
|
_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 = texture.texture;
|
|
r.type = Renderable.SPRITEBATCH;
|
|
r.color = new Color(220, 80, 80);
|
|
// Upright quad centered on X, bottom-aligned on Y.
|
|
r.sprites = [[-PLAYER_W/2, 0, 0, PLAYER_W/2, PLAYER_H, 0, 0, 1, 1, 0]];
|
|
|
|
_position.localPosition = new Vec3(0, PLAYER_H, 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;
|