Skip to content

Commit

Permalink
Game scripts and chunk stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerthecoder committed Aug 10, 2024
1 parent a963caa commit 7a0bef3
Show file tree
Hide file tree
Showing 24 changed files with 555 additions and 464 deletions.
27 changes: 17 additions & 10 deletions apps/web-client/src/game-scripts/canvas-gscript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { ChunkRenderer } from "../renders/chunkRender";
import { BlockType } from "@craft/rust-world";
import { PlayerRenderer } from "../renders/playerRender";
import { SphereRenderer } from "../renders/sphereRender";
import { GameDiff, GameWrapper } from "@craft/engine/modules";

type Config = {
renderDistance: number;
Expand All @@ -37,7 +38,7 @@ export class CanvasGameScript extends GameScript<Config> {

private renderers: Renderer[] = [];
private entityRenderers: Map<string, Renderer> = new Map();
private chunkRenderers: Map<string, ChunkRenderer> = new Map();
private chunkRenderers: Map<number, ChunkRenderer> = new Map();
shouldRenderMainPlayer = false;

isSpectating = false;
Expand All @@ -49,7 +50,7 @@ export class CanvasGameScript extends GameScript<Config> {
mainPlayer: Player;

constructor(
game: Game,
game: GameWrapper,
private webGlGScript: WebGlGScript,
private basic: BasicGScript
) {
Expand Down Expand Up @@ -81,6 +82,17 @@ export class CanvasGameScript extends GameScript<Config> {
: new EntityCamera(this.mainPlayer);
}

onDiff(diff: GameDiff) {
for (const entityId of diff.updated_entities) {
const entity = this.game.entities.get(entityId);
this.onNewEntity(entity);
}

for (const chunkId of diff.updated_chunks) {
this.onChunkUpdate(chunkId);
}
}

getFilter(camera: Camera): Vector3D | null {
const shiftedDown = camera.pos.sub(new Vector3D([0, 0.5, 0]));
const block = this.game.world.getBlockFromWorldPoint(shiftedDown);
Expand Down Expand Up @@ -273,15 +285,10 @@ export class CanvasGameScript extends GameScript<Config> {
this.entityRenderers.delete(entity.uid);
}

onChunkUpdate(chunkId: string): void {
onChunkUpdate(chunkId: number): void {
console.log("CanvasGameScript: Chunk update", chunkId);
const chunkPos = World.chunkIdToChunkPos(chunkId);
const chunkMesh = this.game.world.getChunkMesh(chunkPos);
const chunkRenderer = new ChunkRenderer(
this.webGlGScript,
chunkMesh,
chunkPos
);
const chunkMesh = this.game.getChunkMeshFromChunkId(chunkId);
const chunkRenderer = new ChunkRenderer(this.webGlGScript, chunkMesh);
chunkRenderer.getBufferData();
this.chunkRenderers.set(chunkId, chunkRenderer);
}
Expand Down
11 changes: 5 additions & 6 deletions apps/web-client/src/renders/chunkRender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,23 @@ import {
arraySub,
getBlockData,
IDim,
ChunkMesh,
Vector3D,
Vector2D,
} from "@craft/engine";
import TextureMapper from "../textureMapper";
import { BlockShape, BlockType } from "@craft/rust-world";
import ShapeBuilder from "../services/shape-builder";
import { WebGlGScript } from "../game-scripts/webgl-gscript";
import { ChunkMesh } from "@craft/engine/modules";

export class ChunkRenderer extends Renderer {
private otherRenders: Renderer[] = [];
public position: Vector2D;

constructor(
public webGlGScript: WebGlGScript,
public chunkMesh: ChunkMesh,
public position: Vector2D
) {
constructor(public webGlGScript: WebGlGScript, public chunkMesh: ChunkMesh) {
super(webGlGScript);

this.position = new Vector2D([chunkMesh.chunkPos.x, chunkMesh.chunkPos.y]);
this.setActiveTexture(webGlGScript.textureAtlas);
this.getBufferData();
}
Expand Down
12 changes: 12 additions & 0 deletions apps/web-client/src/runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { GameWrapper } from "@craft/engine/modules";
import { CanvasGameScript } from "./game-scripts/canvas-gscript";

function run() {
// Start the game

const game = GameWrapper.createGame();

const canvasGameScript = new CanvasGameScript(game);

game.makeGameScript(canvasGameScript);
}
5 changes: 0 additions & 5 deletions lib/engine/entities/cube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ export type CubeDto = {
pos: IDim;
};

export type Cube = {
type: BlockType;
pos: Vector3D;
};

export type ISerializedCube = {
block_type: BlockType;
extra_data: "None";
Expand Down
4 changes: 2 additions & 2 deletions lib/engine/entities/player/playerActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Game, IDim, Vector3D } from "../../index.js";
import { MessageDto, MessageHolder } from "../../messageHelpers.js";
import CubeHelpers from "../cube.js";
import { Player } from "./player.js";
import { PlayerAction } from "../../modules.js";
import { GameWrapper, PlayerAction } from "../../modules.js";

export enum PlayerActionType {
Jump = "jump",
Expand Down Expand Up @@ -81,7 +81,7 @@ export type PlayerActionDto = MessageDto<PlayerActionType, PlayerActionData>;
// }

export class PlayerActionService {
constructor(private game: Game) {}
constructor(private game: GameWrapper) {}

private playerActions = new Map<
string,
Expand Down
4 changes: 2 additions & 2 deletions lib/engine/game-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export abstract class GameScript<

config?: Config;

constructor(protected game: Game, ..._args: unknown[]) {}
constructor(protected game: GameWrapper, ..._args: unknown[]) {}

setConfig?(config: Config): void;

Expand All @@ -29,5 +29,5 @@ export abstract class GameScript<

onRemovedEntity?(entity: Entity): void;

onChunkUpdate?(chunkId: string): void;
onChunkUpdate?(chunkId: number): void;
}
116 changes: 92 additions & 24 deletions lib/engine/modules.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import * as WorldWasm from "@craft/rust-world";
import * as TerrainGenWasm from "@craft/terrain-gen";
import { Vector2D } from "./utils/vector.js";
import {
GameAction,
IChunkReader,
ISerializedChunk,
ISerializedWorld,
SandboxGScript,
World,
} from "./index.js";
import { Vector2D, Vector3D } from "./utils/vector.js";
import { World } from "./index.js";
export * as WorldModuleTypes from "@craft/rust-world";

async function loadWasmModule(module: any, name = "") {
Expand All @@ -17,27 +10,107 @@ async function loadWasmModule(module: any, name = "") {
console.log(`Loaded Wasm Module: ${name} 🎉`);
return loadedModule;
}
export interface ISerializedChunk {
position: {
x: number;
y: number;
};
blocks: WorldWasm.BlockType[];
block_data: ("None" | { Image: string })[];
}

type ISerializedChunkHolder = ISerializedChunk[];

export interface ISerializedWorld {
chunks: ISerializedChunkHolder;
}

export type SerializedGame = {
world: ISerializedWorld;
players: Player[];
};

export type PlayerAction = WorldWasm.PlayerJumpAction;

export type GameDiff = {
updated_entities: number[];
updated_chunks: number[];
};

export type GameScript = {
onDiff: (diff: GameDiff) => void;
};

export type Cube = {
type: WorldWasm.BlockType;
pos: Vector3D;
};

export type ChunkMesh = {
mesh: Array<{ block: Cube; faces: WorldWasm.Direction[] }>;
chunkPos: { x: number; y: number };
};

(window as any).test = async () => {
await WorldModule.load();
const game = WorldModule.createGame();
const player = WorldModule.createPlayer(Number(1));
game.addPlayer(player);
export type Player = {
speed: number;
max_speed: number;
gravity: number;
uid: number;
pos: {
x: number;
y: number;
z: number;
};
dim: {
x: number;
y: number;
z: number;
};
rot: {
theta: number;
phi: number;
};
vel: {
x: number;
y: number;
z: number;
};
is_flying: boolean;
on_ground: boolean;
moving_directions: WorldWasm.Direction[];
};

export class GameWrapper {
constructor(private game: WorldWasm.Game) {}

handleAction(action: GameAction) {
static createGame(): GameWrapper {
const game = WorldWasm.Game.new_wasm();
return new GameWrapper(game);
}

makeJumpAction(entityId: number) {
return WorldWasm.PlayerJumpAction.make_wasm(entityId);
}

handleAction(action: WorldWasm.EntityAction) {
this.game.handle_action_wasm(action);
}

addPlayer(player: WorldWasm.Player) {
this.game.add_entity_wasm(player);
makeGameScript(script: GameScript) {
return WorldWasm.WasmGameScript.make(script);
}

addGameScript(script: WorldWasm.WasmGameScript) {
this.game.add_game_script_wasm(script);
}

getChunkMeshFromChunkId(chunkId: number): ChunkMesh {
const big = BigInt(chunkId);
return this.game.get_chunk_mesh_from_chunk_id(big);
}
}

export type PlayerAction = "Jump" | { Move: WorldWasm.Direction[] };
handlePlayerAction(action: PlayerAction) {}
}

// Wrapper class for world logic
class WorldModuleClass {
Expand All @@ -62,11 +135,6 @@ class WorldModuleClass {
return world;
}

public createGame(): GameWrapper {
const game = WorldModule.module.Game.new();
return new GameWrapper(game);
}

public createPlayer(uid: number) {
const player = WorldModule.module.Player.make(uid);
return player;
Expand Down
9 changes: 0 additions & 9 deletions lib/engine/world/chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,6 @@ export interface ILookingAtData {
dist: number;
}

export interface ISerializedChunk {
position: {
x: number;
y: number;
};
blocks: BlockType[];
block_data: ("None" | { Image: string })[];
}

export const getChunkId = (serChunk: ISerializedChunk) => {
const vec = new Vector2D([serChunk.position.x, serChunk.position.y]);
return vec.toIndex();
Expand Down
9 changes: 0 additions & 9 deletions lib/engine/world/chunkMesh.ts

This file was deleted.

6 changes: 0 additions & 6 deletions lib/engine/world/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ import { WorldModule, WorldModuleTypes } from "../modules.js";
import { ChunkMesh } from "./chunkMesh.js";
import { CameraRay } from "../index.js";

type ISerializedChunkHolder = ISerializedChunk[];

export interface ISerializedWorld {
chunks: ISerializedChunkHolder;
}

export class World {
static make(data?: ISerializedWorld): World {
return WorldModule.createWorld(data);
Expand Down
6 changes: 6 additions & 0 deletions lib/world/src/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ interface ITextStyle {
}
"#;

pub type ChunkId = u64;

pub const CHUNK_WIDTH: i16 = 16;
pub const CHUNK_HEIGHT: i16 = 64;

Expand Down Expand Up @@ -72,6 +74,10 @@ impl Chunk {
}
}

pub fn get_id(&self) -> ChunkId {
self.position.to_id()
}

pub fn get_all_blocks(&self) -> Vec<ChunkBlock> {
self.blocks
.iter()
Expand Down
2 changes: 2 additions & 0 deletions lib/world/src/chunk/chunk_mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use crate::{
plane::WorldPlane,
positions::{ChunkPos, InnerChunkPos, WorldPos},
};
use js_sys::wasm_bindgen;
use wasm_bindgen::prelude::*;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

Expand Down
Loading

0 comments on commit 7a0bef3

Please sign in to comment.