we ball I guess
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
// 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;
|
||||
+50
-25
@@ -1,47 +1,72 @@
|
||||
var scene = {};
|
||||
// Copyright (c) 2026 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
var scene = {};
|
||||
var player = require('player.js');
|
||||
|
||||
var assets = AssetBatch([
|
||||
{ path: 'test.png', type: Asset.TYPE_TEXTURE, format: Texture.FORMAT_RGBA }
|
||||
]);
|
||||
|
||||
var cam;
|
||||
var camPos;
|
||||
var testEntity;
|
||||
var testPos;
|
||||
var testRenderable;
|
||||
var texEntry;
|
||||
// Pokemon DS-style camera: ~34 degrees elevation (atan(6/9)).
|
||||
// CAM_HEIGHT / CAM_DIST ratio controls the tilt — keep it under 0.7 for
|
||||
// the characteristically shallow DS angle.
|
||||
const CAM_HEIGHT = 6;
|
||||
const CAM_DIST = 9;
|
||||
|
||||
var cam, camPos;
|
||||
var floorEntity;
|
||||
|
||||
function updateCamera() {
|
||||
var pp = player.getPosition().worldPosition;
|
||||
// Position is offset above and behind the player; lookAt the exact player
|
||||
// world position so the player projects to the center pixel every frame.
|
||||
camPos.localPosition = new Vec3(pp.x, pp.y + CAM_HEIGHT, pp.z + CAM_DIST);
|
||||
camPos.lookAt(new Vec3(pp.x, pp.y, pp.z));
|
||||
}
|
||||
|
||||
scene.init = async function() {
|
||||
assets.lock();
|
||||
await assets.loaded();
|
||||
|
||||
Console.print('Scene Init');
|
||||
texEntry = assets.entry(0);
|
||||
var texEntry = assets.entry(0);
|
||||
|
||||
// Camera at (3, 3, 3) looking at origin
|
||||
// Camera
|
||||
cam = Entity.create();
|
||||
camPos = cam.add(Component.POSITION);
|
||||
cam.add(Component.CAMERA);
|
||||
camPos.localPosition = new Vec3(3, 3, 3);
|
||||
camPos.lookAt(new Vec3(0, 0, 0));
|
||||
|
||||
// Test entity with textured quad at origin
|
||||
testEntity = Entity.create();
|
||||
testPos = testEntity.add(Component.POSITION);
|
||||
testRenderable = testEntity.add(Component.RENDERABLE);
|
||||
// Floor — infinite static plane at Y=0. Rendered as a large flat blue
|
||||
// slab using the default SHADER_MATERIAL (no texture needed).
|
||||
floorEntity = Entity.create();
|
||||
var floorPos = floorEntity.add(Component.POSITION);
|
||||
var floorPhysics = floorEntity.add(Component.PHYSICS);
|
||||
floorPhysics.bodyType = Physics.STATIC;
|
||||
floorPhysics.shape = Physics.SHAPE_PLANE;
|
||||
|
||||
testRenderable.texture = texEntry.texture;
|
||||
testRenderable.type = Renderable.SPRITEBATCH;
|
||||
testRenderable.sprites = [
|
||||
[0, 0, 1, 1, 0, 1, 1, 0]
|
||||
];
|
||||
// testPos.localPosition = new Vec3(0, 0, 0);
|
||||
}
|
||||
var floorR = floorEntity.add(Component.RENDERABLE);
|
||||
floorR.color = Color.BLUE;
|
||||
floorPos.localScale = new Vec3(16, 0.2, 16);
|
||||
floorPos.localPosition = new Vec3(0, -0.1, 0);
|
||||
|
||||
// Player — spawns 1 unit above the floor so physics drops it cleanly.
|
||||
player.create(texEntry);
|
||||
|
||||
// Initialise camera at the correct angle from the player's spawn position.
|
||||
updateCamera();
|
||||
};
|
||||
|
||||
scene.update = function() {
|
||||
player.update();
|
||||
updateCamera();
|
||||
};
|
||||
|
||||
scene.dispose = function() {
|
||||
Console.print('Scene Dispose');
|
||||
player.dispose();
|
||||
Entity.dispose(floorEntity);
|
||||
Entity.dispose(cam);
|
||||
Entity.dispose(testEntity);
|
||||
assets.unlock();
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user