3D game
This commit is contained in:
@@ -11,7 +11,7 @@ import { distance_point_rectangle } from "../../common/utils.ts";
|
||||
|
||||
export class DimensionLogicSystem extends System {
|
||||
update(world: ClientWorld, delta: number): void {
|
||||
const dimension_entity = world.get_entities().values().find((ent) => ent.get(Dimension));
|
||||
/*const dimension_entity = world.get_entities().values().find((ent) => ent.get(Dimension));
|
||||
const dimension = dimension_entity!.get(Dimension)!;
|
||||
|
||||
dimension.second_timer += delta;
|
||||
@@ -72,7 +72,7 @@ export class DimensionLogicSystem extends System {
|
||||
tile_info.on_click(world, tile);
|
||||
}
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}
|
||||
handle_second(world: ClientWorld, dimension: Dimension) {
|
||||
const tickables = dimension.tiles.filter((t) => t.tickable);
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
import { System } from "$/common/ecs/mod.ts";
|
||||
import { Velocity } from "$/common/components/velocity.ts";
|
||||
import { InputManager } from "../input_manager.ts";
|
||||
import { AnimatedSprite } from "../components/sprite.ts";
|
||||
import { ClientWorld } from "../client_world.ts";
|
||||
import { PlayerControls } from "../components/player_controls.ts";
|
||||
import { Camera } from "../components/camera.ts";
|
||||
import { Position } from "../../common/components/position.ts";
|
||||
import { canvas } from "../renderer/mod.ts";
|
||||
import { PlayerComponent } from "../player.ts";
|
||||
import { GuiPlayerInventory } from "../gui/gui_player_inventory.ts";
|
||||
|
||||
@@ -20,40 +18,51 @@ export class PlayerControlsSystem extends System {
|
||||
const velocity = player.get(Velocity)!;
|
||||
const controls = player.get(PlayerControls)!;
|
||||
const player_component = player.get(PlayerComponent)!;
|
||||
const position = player.get(Position)!;
|
||||
const camera = player.get(Camera)!;
|
||||
|
||||
let input_x = 0;
|
||||
let input_z = 0;
|
||||
|
||||
if (InputManager.is_key_down(controls.move_left)) {
|
||||
velocity.vx = -controls.move_speed;
|
||||
} else if (InputManager.is_key_down(controls.move_right)) {
|
||||
velocity.vx = controls.move_speed;
|
||||
} else {
|
||||
velocity.vx = 0;
|
||||
input_x -= 1;
|
||||
}
|
||||
if (InputManager.is_key_down(controls.move_right)) {
|
||||
input_x += 1;
|
||||
}
|
||||
if (InputManager.is_key_down(controls.move_forward)) {
|
||||
input_z -= 1;
|
||||
}
|
||||
if (InputManager.is_key_down(controls.move_backwards)) {
|
||||
input_z += 1;
|
||||
}
|
||||
|
||||
if (InputManager.is_key_down(controls.move_up)) {
|
||||
velocity.vy = -controls.move_speed;
|
||||
} else if (InputManager.is_key_down(controls.move_down)) {
|
||||
velocity.vy = controls.move_speed;
|
||||
const size = Math.hypot(input_x, input_z);
|
||||
if (size > 0) {
|
||||
input_x /= size;
|
||||
input_z /= size;
|
||||
}
|
||||
|
||||
const sin = Math.sin(camera.yaw);
|
||||
const cos = Math.cos(camera.yaw);
|
||||
|
||||
const forwardX = sin;
|
||||
const forwardZ = cos;
|
||||
|
||||
const rightX = cos;
|
||||
const rightZ = -sin;
|
||||
|
||||
velocity.vx = (forwardX * input_z + rightX * input_x) * controls.move_speed;
|
||||
velocity.vz = (forwardZ * input_z + rightZ * input_x) * controls.move_speed;
|
||||
|
||||
if (InputManager.is_key_down("Space")) {
|
||||
velocity.vy += controls.move_speed / 10;
|
||||
} else if (InputManager.is_key_down("ShiftLeft")) {
|
||||
velocity.vy -= controls.move_speed / 10;
|
||||
} else {
|
||||
velocity.vy = 0;
|
||||
}
|
||||
|
||||
const animated_sprite = player.get(AnimatedSprite)!;
|
||||
|
||||
if (
|
||||
InputManager.is_key_down(controls.move_up) || InputManager.is_key_down(controls.move_down) ||
|
||||
InputManager.is_key_down(controls.move_left) || InputManager.is_key_down(controls.move_right)
|
||||
) {
|
||||
animated_sprite.set_state("running");
|
||||
} else {
|
||||
animated_sprite.set_state("idle");
|
||||
}
|
||||
|
||||
if (InputManager.is_key_down(controls.move_left)) {
|
||||
animated_sprite.flip_x = false;
|
||||
} else if (InputManager.is_key_down(controls.move_right)) {
|
||||
animated_sprite.flip_x = true;
|
||||
}
|
||||
|
||||
if (InputManager.is_key_pressed(controls.open_inventory)) {
|
||||
if (player_component.screens.length === 0) {
|
||||
player_component.screens.push(new GuiPlayerInventory(player_component.player_inventory));
|
||||
@@ -66,13 +75,24 @@ export class PlayerControlsSystem extends System {
|
||||
world.debugging = !world.debugging;
|
||||
}
|
||||
|
||||
const position = player.get(Position)!;
|
||||
const camera = player.get(Camera)!;
|
||||
if (InputManager.is_key_pressed("F11")) {
|
||||
InputManager.toggle_fullscreen();
|
||||
}
|
||||
|
||||
camera.x = Math.round(position.x);
|
||||
camera.y = Math.round(position.y);
|
||||
camera.offset_x = Math.floor(canvas.width / 2);
|
||||
camera.offset_y = Math.floor(canvas.height / 2);
|
||||
camera.x = position.x;
|
||||
camera.y = position.y + 1.79;
|
||||
camera.z = position.z;
|
||||
|
||||
if (InputManager.is_mouse_grabbed()) {
|
||||
const mouse_delta = InputManager.get_mouse_delta();
|
||||
camera.yaw += -mouse_delta.x * 0.001;
|
||||
camera.pitch += -mouse_delta.y * 0.001;
|
||||
|
||||
const limit = Math.PI / 2 - 0.01;
|
||||
camera.pitch = Math.max(-limit, Math.min(limit, camera.pitch));
|
||||
}
|
||||
|
||||
InputManager.set_mouse_grabbed(player_component.screens.length === 0);
|
||||
|
||||
if (player_component.screens.length === 0) {
|
||||
const player_inventory = player_component.player_inventory;
|
||||
|
||||
@@ -8,9 +8,8 @@ import { Camera } from "$/client/components/camera.ts";
|
||||
import { render_animated_sprite, render_sprite } from "./rendering/sprites.ts";
|
||||
import { render_dimension } from "./rendering/dimension.ts";
|
||||
import { render_player_hotbar } from "./rendering/player.ts";
|
||||
import { begin_mode_2d, end_mode_2d } from "../renderer/mod.ts";
|
||||
import { PlayerComponent } from "../player.ts";
|
||||
import { begin_mode_3d, end_mode_3d, push_cube } from "../renderer/core.ts";
|
||||
import { begin_mode_3d, end_mode_3d } from "../renderer/core.ts";
|
||||
|
||||
export class RenderSystem extends System {
|
||||
constructor() {
|
||||
@@ -18,83 +17,42 @@ export class RenderSystem extends System {
|
||||
}
|
||||
|
||||
update(world: World, _delta: number): void {
|
||||
/*const camera_entity = world.get_entities().values().find((e) => e.get(Camera)?.active);
|
||||
const camera_entity = world.get_entities().values().find((e) => e.get(Camera));
|
||||
const camera = camera_entity?.get(Camera);
|
||||
|
||||
if (!camera) {
|
||||
return;
|
||||
}
|
||||
|
||||
begin_mode_2d(camera);
|
||||
begin_mode_3d(camera);
|
||||
|
||||
for (const entity of world.get_entities()) {
|
||||
const position = entity.get(Position);
|
||||
const sprite = entity.get(Sprite);
|
||||
|
||||
if (sprite && position) {
|
||||
render_sprite(sprite, position, camera);
|
||||
}
|
||||
|
||||
const animated_sprite = entity.get(AnimatedSprite);
|
||||
|
||||
if (animated_sprite && position) {
|
||||
render_animated_sprite(animated_sprite, position, camera);
|
||||
}
|
||||
|
||||
const dimension = entity.get(Dimension);
|
||||
|
||||
if (dimension) {
|
||||
render_dimension(dimension, camera);
|
||||
render_dimension(dimension /*, camera*/);
|
||||
}
|
||||
}
|
||||
|
||||
end_mode_2d();
|
||||
end_mode_3d();
|
||||
|
||||
for (const entity of world.get_entities()) {
|
||||
const player_component = entity.get(PlayerComponent);
|
||||
const position = entity.get(Position);
|
||||
|
||||
const sprite = entity.get(Sprite);
|
||||
if (position && sprite) {
|
||||
render_sprite(sprite, position);
|
||||
}
|
||||
|
||||
const animated_sprite = entity.get(AnimatedSprite);
|
||||
if (position && animated_sprite) {
|
||||
render_animated_sprite(animated_sprite, position);
|
||||
}
|
||||
|
||||
const player_component = entity.get(PlayerComponent);
|
||||
if (player_component) {
|
||||
render_player_hotbar(player_component.player_inventory);
|
||||
}
|
||||
}*/
|
||||
|
||||
begin_mode_3d();
|
||||
push_cube(
|
||||
-0.5,
|
||||
-0.5,
|
||||
-0.5,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
0,
|
||||
0,
|
||||
1,
|
||||
);
|
||||
push_cube(
|
||||
0.5,
|
||||
0.5,
|
||||
0.5,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
0,
|
||||
0,
|
||||
1,
|
||||
);
|
||||
push_cube(
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
0,
|
||||
0,
|
||||
1,
|
||||
);
|
||||
end_mode_3d();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,23 +1,11 @@
|
||||
import { get_sprite_region } from "$/common/utils.ts";
|
||||
import { Dimension } from "$/client/components/dimension.ts";
|
||||
import { TEXTURE_SIZE, TILE_SIZE } from "$/common/constants.ts";
|
||||
import { Camera, world_to_screen } from "$/client/components/camera.ts";
|
||||
import { canvas, draw_texture_region } from "$/client/renderer/mod.ts";
|
||||
import { TEXTURE_SIZE } from "$/common/constants.ts";
|
||||
import { EverythingRegistry, TileRegistry } from "$/common/everything_registry.ts";
|
||||
import { push_cube } from "$/client/renderer/mod.ts";
|
||||
|
||||
export function render_dimension(dimension: Dimension, camera: Camera) {
|
||||
export function render_dimension(dimension: Dimension /*, camera: Camera*/) {
|
||||
for (const tile of dimension.tiles) {
|
||||
const x = tile.x * TILE_SIZE;
|
||||
const y = tile.y * TILE_SIZE;
|
||||
|
||||
const screen_position = world_to_screen(x, y, camera, canvas.width, canvas.height);
|
||||
if (
|
||||
screen_position.x + TILE_SIZE < 0 || screen_position.x > canvas.width ||
|
||||
screen_position.y + TILE_SIZE < 0 || screen_position.y > canvas.height
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let texture_id = tile.id;
|
||||
const tile_info = EverythingRegistry.get<TileRegistry>("tiles", tile.id);
|
||||
if (tile_info?.texture_id) {
|
||||
@@ -30,16 +18,18 @@ export function render_dimension(dimension: Dimension, camera: Camera) {
|
||||
|
||||
const region = get_sprite_region(texture_id);
|
||||
|
||||
draw_texture_region(
|
||||
push_cube(
|
||||
dimension.image,
|
||||
tile.x,
|
||||
tile.y,
|
||||
tile.z,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
region.x * TEXTURE_SIZE,
|
||||
region.y * TEXTURE_SIZE,
|
||||
TEXTURE_SIZE,
|
||||
TEXTURE_SIZE,
|
||||
x,
|
||||
y,
|
||||
TILE_SIZE,
|
||||
TILE_SIZE,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,8 @@
|
||||
import { Position } from "$/common/components/position.ts";
|
||||
import { AnimatedSprite, Sprite } from "$/client/components/sprite.ts";
|
||||
import { draw_texture_region } from "$/client/renderer/mod.ts";
|
||||
import { Camera } from "$/client/components/camera.ts";
|
||||
|
||||
// TODO: camera scale
|
||||
// TODO: only render if inside screen
|
||||
|
||||
export function render_sprite(sprite: Sprite, position: Position, _camera: Camera) {
|
||||
export function render_sprite(sprite: Sprite, position: Position) {
|
||||
draw_texture_region(
|
||||
sprite.image,
|
||||
sprite.source_x,
|
||||
@@ -25,7 +21,6 @@ export function render_sprite(sprite: Sprite, position: Position, _camera: Camer
|
||||
export function render_animated_sprite(
|
||||
animated_sprite: AnimatedSprite,
|
||||
position: Position,
|
||||
_camera: Camera,
|
||||
) {
|
||||
const current_animation = animated_sprite.states[animated_sprite.current_state];
|
||||
if (!current_animation) {
|
||||
|
||||
Reference in New Issue
Block a user