21 Commits
Author SHA1 Message Date
azykov@mail.ru 110594483c test 9 2026-06-27 20:43:37 +03:00
azykov@mail.ru 619f54c49c test 8 2026-06-27 20:41:03 +03:00
azykov@mail.ru bc9c275776 test 7 2026-06-27 20:38:30 +03:00
azykov@mail.ru 36dcdffff2 test 6 2026-06-27 20:37:15 +03:00
azykov@mail.ru f6680161d7 test 5 2026-06-27 19:21:14 +03:00
azykov@mail.ru 2e28c9c3d5 test 4 2026-06-27 19:20:22 +03:00
azykov@mail.ru b0503669b0 test 3 2026-06-27 19:16:55 +03:00
azykov@mail.ru 224ae60cc8 test 2 2026-06-27 19:13:50 +03:00
azykov@mail.ru beaa656026 test 2026-06-27 13:37:27 +03:00
azykov@mail.ru 793a7c9da3 deploy directly to docker, not ssh 2026-06-27 12:04:58 +03:00
azykov@mail.ru bc98e23f6a Merge branch 'deploy' of ssh://10.0.64.33:49022/anton/blockly3d into deploy 2026-06-27 11:51:56 +03:00
azykov@mail.ru de292361a2 docker-cli is now baked into runner's job base image 2026-06-27 11:51:40 +03:00
azykov@mail.ru f071bbe9f7 docker-cli is now baked in runner's job base image 2026-06-27 11:48:24 +03:00
azykov@mail.ru ff5d1d52ef old buildx in alpine fix 2026-06-27 10:58:44 +03:00
azykov@mail.ru cad617000d fixe for docker cli installation 2026-06-27 10:56:15 +03:00
azykov@mail.ru e7897e8bb1 docker cli installation in container 2026-06-27 10:52:43 +03:00
azykov@mail.ru fcb8c2a78b no base actions 2026-06-26 23:12:13 +03:00
azykov@mail.ru 1ba60a5dc3 no ifs, deploy without appleboy/ssh-action 2026-06-26 22:46:12 +03:00
azykov@mail.ru e2e9a357de runs on: docker1 2026-06-26 21:57:34 +03:00
azykov@mail.ru d224c033ca any branch deploy 2026-06-26 21:29:05 +03:00
azykov@mail.ru 5f2d86368b deploy barebones 2026-06-26 20:49:23 +03:00
29 changed files with 359 additions and 155 deletions
+7
View File
@@ -0,0 +1,7 @@
# Скопируй в .env и заполни
SERVER_PORT=3000
CLIENT_PORT=5000
# Добавь свои переменные ниже
# DATABASE_URL=
# JWT_SECRET=
+102
View File
@@ -0,0 +1,102 @@
name: Build & Deploy
on:
push
env:
REGISTRY: git.aggtaa.com
IMAGE_PREFIX: ${{ gitea.repository }}
jobs:
# ── BUILD SERVER ──────────────────────────────────────────────────────────────
build-server:
runs-on: docker1
defaults:
run:
shell: sh
working-directory: /workspace
steps:
- name: Check paths
run: |
ls -la /data/tools/ || echo "no /data/tools"
mount | grep overlay || echo "no overlay"
ls /bin/
ls /usr/bin/ | grep -E "bash|sh"
- name: Check docker
run: |
find /usr -name "docker-buildx" 2>/dev/null || echo "not found in /usr"
find /root -name "docker-buildx" 2>/dev/null || echo "not found in /root"
ls /usr/libexec/ 2>/dev/null || echo "no /usr/libexec"
- name: Checkout
working-directory: /
run: |
mkdir -p /workspace
if [ -d /workspace/.git ]; then
cd /workspace && git fetch origin && git reset --hard origin/${{ gitea.ref_name }}
else
git clone ${{ gitea.server_url }}/${{ gitea.repository }}.git /workspace
fi
- name: Build & push server
run: |
echo "${{ secrets.REGISTRY_PASSWORD }}" | \
docker login ${{ env.REGISTRY }} -u ${{ secrets.REGISTRY_USER }} --password-stdin
/data/tools/docker-buildx build \
--cache-from type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_PREFIX }}/server:cache \
--cache-to type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_PREFIX }}/server:cache,mode=max \
--tag ${{ env.REGISTRY }}/${{ env.IMAGE_PREFIX }}/server:latest \
--tag ${{ env.REGISTRY }}/${{ env.IMAGE_PREFIX }}/server:${{ gitea.sha }} \
--file server.Dockerfile \
--push \
.
# ── BUILD WEBCLIENT ───────────────────────────────────────────────────────────
build-webclient:
runs-on: docker1
defaults:
run:
working-directory: /workspace
steps:
- name: Checkout
working-directory: /
run: |
mkdir -p /workspace
if [ -d /workspace/.git ]; then
cd /workspace && git fetch origin && git reset --hard origin/${{ gitea.ref_name }}
else
git clone ${{ gitea.server_url }}/${{ gitea.repository }}.git /workspace
fi
- name: Build & push webclient
run: |
echo "${{ secrets.REGISTRY_PASSWORD }}" | \
docker login ${{ env.REGISTRY }} -u ${{ secrets.REGISTRY_USER }} --password-stdin
/data/tools/docker-buildx build \
--cache-from type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_PREFIX }}/webclient:cache \
--cache-to type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_PREFIX }}/webclient:cache,mode=max \
--tag ${{ env.REGISTRY }}/${{ env.IMAGE_PREFIX }}/webclient:latest \
--tag ${{ env.REGISTRY }}/${{ env.IMAGE_PREFIX }}/webclient:${{ gitea.sha }} \
--file webclient.Dockerfile \
--push \
.
# ── DEPLOY ────────────────────────────────────────────────────────────────────
deploy:
runs-on: docker1
needs: [build-server, build-webclient]
if: always() && !failure() && !cancelled()
steps:
- name: Deploy
run: |
mkdir -p "/app-cache/${{ gitea.repository }}"
cd "/app-cache/${{ gitea.repository }}"
if [ ! -d .git ]; then
git clone ${{ gitea.server_url }}/${{ gitea.repository }}.git .
else
git pull origin main
fi
docker compose pull
docker compose up -d --remove-orphans
docker image prune -f
+34
View File
@@ -0,0 +1,34 @@
services:
server:
build:
context: .
dockerfile: server.Dockerfile
cache_from:
- ${CI_REGISTRY_IMAGE:-blockly3d}/server:cache
image: ${CI_REGISTRY_IMAGE:-blockly3d}/server:${CI_COMMIT_SHA:-latest}
restart: unless-stopped
ports:
- "${SERVER_PORT:-3000}:3000"
environment:
- NODE_ENV=production
env_file:
- .env
networks:
- blockly3d
webclient:
build:
context: .
dockerfile: webclient.Dockerfile
cache_from:
- ${CI_REGISTRY_IMAGE:-blockly3d}/webclient:cache
image: ${CI_REGISTRY_IMAGE:-blockly3d}/webclient:${CI_COMMIT_SHA:-latest}
restart: unless-stopped
ports:
- "${CLIENT_PORT:-5000}:5000"
networks:
- blockly3d
networks:
blockly3d:
driver: bridge
+37
View File
@@ -0,0 +1,37 @@
# syntax=docker/dockerfile:1
FROM node:22.22.3-alpine AS base
RUN corepack enable && corepack prepare pnpm@latest --activate
# ── deps ──────────────────────────────────────────────────────────────────────
FROM base AS deps
WORKDIR /app
# Копируем только манифесты — слой кешируется пока lock не менялся
COPY package.json pnpm-workspace.yaml pnpm-lock.yaml ./
COPY server/package.json ./server/
RUN pnpm install --frozen-lockfile --filter server...
# ── build ─────────────────────────────────────────────────────────────────────
FROM deps AS builder
WORKDIR /app
COPY server ./server
RUN pnpm --filter server build
# ── runtime ───────────────────────────────────────────────────────────────────
FROM node:22.22.3-alpine AS runner
RUN corepack enable && corepack prepare pnpm@latest --activate
WORKDIR /app
COPY package.json pnpm-workspace.yaml pnpm-lock.yaml ./
COPY server/package.json ./server/
RUN pnpm install --frozen-lockfile --filter server... --prod
COPY --from=builder /app/server/dist ./server/dist
EXPOSE 3000
CMD ["node", "server/dist/index.js"]
+32
View File
@@ -0,0 +1,32 @@
# syntax=docker/dockerfile:1
FROM node:22.22.3-alpine AS base
RUN corepack enable && corepack prepare pnpm@latest --activate
# ── deps ──────────────────────────────────────────────────────────────────────
FROM base AS deps
WORKDIR /app
COPY package.json pnpm-workspace.yaml pnpm-lock.yaml ./
COPY webclient/package.json ./webclient/
RUN pnpm install --frozen-lockfile --filter webclient...
# ── build ─────────────────────────────────────────────────────────────────────
FROM deps AS builder
WORKDIR /app
COPY webclient ./webclient
RUN pnpm --filter webclient build
# ── runtime ───────────────────────────────────────────────────────────────────
FROM node:22.22.3-alpine AS runner
WORKDIR /app
RUN npm install -g serve
COPY --from=builder /app/webclient/dist ./dist
EXPOSE 5000
CMD ["serve", "-s", "dist", "-l", "5000"]
+2 -2
View File
@@ -1,5 +1,5 @@
import { observer } from "mobx-react-lite";
import type { ObjectType, ObjectInstance } from "../types";
import type { ObjectType, RuntimeGameObjectInstance } from "../types";
import { ObjectViewInternal, type ObjectViewInternalHandle } from "./ObjectViewInternal";
import { useEffect, useLayoutEffect, useMemo, useRef } from "react";
import { Euler, Quaternion, Vector3 } from "three";
@@ -25,7 +25,7 @@ const _charPos = new Vector3();
const _lookAt = new Vector3();
type PlayerObjectViewProps = {
object: ObjectInstance;
object: RuntimeGameObjectInstance;
objectType: ObjectType;
}
@@ -2,7 +2,6 @@ import { useEffect } from "react";
import { observer } from "mobx-react-lite";
import { debugLabelsStore } from "../state/debugLabelsState";
import { DebugLabel } from "./DebugLabel";
import type { Scene } from "../types";
export const DebugLabelsView = observer(function ({ scene, editMode }: { scene: Scene, editMode?: boolean }) {
useEffect(
@@ -2,7 +2,6 @@ import { observer } from "mobx-react-lite";
import { state } from "../state";
import { ObjectViewInternal } from "./ObjectViewInternal";
import { PlayerObjectView } from "./CharacterView";
import type { ObjectInstance } from "../types";
export type GameObjectViewProps = {
object: ObjectInstance;
@@ -94,6 +94,9 @@ export const ObjectEditorView = observer(function ({ object, isPlayer }: ObjectE
if (!objectType)
return null;
// Track cache so observer re-renders when refreshObjectTypeCaches is called
void object.cache.updatedAt;
function handleClick() {
if (state.worldEditor.isEnabled) {
// Reading selection state inside an event handler: not tracked by observer.
@@ -1,12 +1,27 @@
import type { ObjectInstance, ObjectType } from "../types";
import { useEffect, useImperativeHandle, useRef, type Ref } from "react";
import type { ObjectInstance, ObjectType, RuntimeGameObjectInstance, RuntimeObjectInstance } from "../types";
import type { VoxelGroup } from "../types/runtime/voxelGroup";
import { useEffect, useImperativeHandle, useRef, type Ref, memo, useMemo } from "react";
import { Euler, Quaternion, Mesh, MeshStandardMaterial, type Group } from "three";
import type { ThreeEvent } from "@react-three/fiber";
import { RapierRigidBody } from "@react-three/rapier";
import { ConvexHullCollider, RapierRigidBody } from "@react-three/rapier";
import { SyncRigidBody } from "./SyncRigidBody";
import { reaction, runInAction } from "mobx";
import { v3toRapier } from "../utils";
import { state } from "../state";
import { buildVoxelGroupMesh } from "../utils/graphics/voxelMesh";
const VoxelGroupMesh = memo(function VoxelGroupMesh({ vg, opacity }: { vg: VoxelGroup, opacity: number }) {
const geo = useMemo(() => buildVoxelGroupMesh(vg.positions), [vg]);
useEffect(() => () => { geo.dispose(); }, [geo]);
const op = vg.opacity * opacity;
return (
<mesh geometry={geo} userData={{ baseOpacity: vg.opacity }}>
<meshStandardMaterial color={vg.color} opacity={op} transparent={op < 1} />
</mesh>
);
});
export type ObjectViewInternalHandle = {
group: Group | null;
@@ -56,6 +71,8 @@ export const ObjectViewInternal = function ({ object, objectType, ref, ...props
[object, object.id, props.isPlayer]
);
const gameObj = object as RuntimeGameObjectInstance;
useEffect(
() => {
return reaction(
@@ -81,8 +98,8 @@ export const ObjectViewInternal = function ({ object, objectType, ref, ...props
useEffect(
() => {
runInAction(() => { object.rb = () => rbRef.current; });
return () => { runInAction(() => { object.rb = () => null; }); };
runInAction(() => { gameObj.rb = () => rbRef.current; });
return () => { runInAction(() => { gameObj.rb = () => null; }); };
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[object.id],
@@ -113,11 +130,9 @@ export const ObjectViewInternal = function ({ object, objectType, ref, ...props
return;
const otherObj = Object.values(game.scene.objects)
.find((o) => o.rb()?.handle === otherHandle);
const context = { object: { ...object, typeId: objectType.id }, other: otherObj };
game.emitEvent('objectTouchStart', context);
game.emitEvent('objectTouchStart', { object: gameObj, other: otherObj });
if (otherObj?.id === game.scene.playerObjectId)
game.emitEvent('playerTouchStart', context);
game.emitEvent('playerTouchStart', { object: gameObj, other: otherObj });
}}
onSync={(data) => {
runInAction(() => {
+1 -1
View File
@@ -4,7 +4,7 @@ import { OrbitControls } from '@react-three/drei';
import { observer } from 'mobx-react-lite';
import { state } from '../state';
import { SceneView } from './SceneView';
import { MOUSE } from 'three';
import { MOUSE, type OrthographicCamera, type PerspectiveCamera } from 'three';
import { useSyncCamera } from '../utils/hooks/useSyncCamera';
import { Physics } from '@react-three/rapier';
+3 -3
View File
@@ -1,11 +1,11 @@
import { observer } from "mobx-react-lite";
import type { RuntimeScene } from "../types";
import { GameObjectView } from "./GameObjectView";
import { ObjectEditorView } from "./ObjectEditorView";
import { DebugLabelsView } from "./DebugLabelsView";
import type { Scene } from "../types";
type SceneViewProps = {
scene: Scene,
scene: RuntimeScene,
isEditor?: boolean;
debugOverlay?: boolean;
}
@@ -18,7 +18,7 @@ export const SceneView = observer(function (props: SceneViewProps) {
Object.values(props.scene.objects)
.map((obj) => (
!props.isEditor
? <GameObjectView key={obj.id} object={obj} isPlayer={props.scene.playerObjectId === obj.id} isSceneEditor={props.isEditor} />
? <GameObjectView key={obj.id} object={obj as RuntimeGameObjectInstance} isPlayer={props.scene.playerObjectId === obj.id} isSceneEditor={props.isEditor} />
: <ObjectEditorView key={obj.id} object={obj} isPlayer={props.scene.playerObjectId === obj.id} />
))
}
+18 -1
View File
@@ -8,6 +8,7 @@ import { SceneEditorView } from './SceneEditorView';
import { JoystickView } from './JoystickView';
import { useEffect } from 'react';
import { runInAction } from 'mobx';
import { IconPlayerPlayFilled, IconPlayerPauseFilled, IconPlayerStopFilled, IconPlayerSkipForwardFilled } from '@tabler/icons-react';
export const ThreeView = observer(function () {
const isGame = !!state.game;
@@ -38,7 +39,23 @@ export const ThreeView = observer(function () {
{isGame ? <GameView /> : <SceneEditorView />}
</Canvas>
{isGame && <JoystickView />}
<div style={{ position: 'absolute', top: 8, right: 8, display: 'flex', gap: 4, fontSize: 14 }}>
{
state.game
? <>
<button onClick={() => state.stopGame()}><IconPlayerStopFilled size="1em" /></button>
{
state.game!.isPaused
? <>
<button onClick={() => state.game!.resume()}><IconPlayerPlayFilled size="1em" /></button>
<button onClick={() => state.game!.step()}><IconPlayerSkipForwardFilled size="1em" /></button>
</>
: <button onClick={() => state.game!.pause()}><IconPlayerPauseFilled size="1em" /></button>
}
</>
: <button onClick={() => state.startGame()}><IconPlayerPlayFilled size="1em" /></button>
}
</div>
</div>
</KeyboardControls>
);
@@ -6,22 +6,19 @@ import { SceneEditorObjectSelectionLayer } from "./SceneEditor/SceneEditorObject
import { LightsLayer } from "./LighsLayer";
import { StatsLayer } from './StatsLayer';
import { SceneEditorObjectGizmosLayer } from "./SceneEditor/SceneEditorObjectGizmosLayer";
import { PlayButtons } from "./PlayButtons";
import { SceneEditorPlayerGizmosLayer } from "./SceneEditor/SceneEditorPlayerGizmosLayer";
export const LayeredThreeView = observer(function () {
return <>
<Canvas>
<LightsLayer />
{/* <Perf position="bottom-right" /> */}
<SceneEditorCameraLayer />
<ObjectGeometriesLayer>
<SceneEditorObjectSelectionLayer />
<SceneEditorObjectGizmosLayer />
</ObjectGeometriesLayer>
<SceneEditorPlayerGizmosLayer />
<StatsLayer />
</Canvas>
<PlayButtons />
</>;
});
@@ -1,23 +0,0 @@
import { observer } from "mobx-react-lite";
import { state } from "../../state";
import { IconPlayerPlayFilled, IconPlayerPauseFilled, IconPlayerStopFilled, IconPlayerSkipForwardFilled } from '@tabler/icons-react';
export const PlayButtons = observer(function () {
return <div style={{ position: 'absolute', top: 8, right: 8, display: 'flex', gap: 4, fontSize: 14 }}>
{
state.game
? <>
<button onClick={() => state.stopGame()}><IconPlayerStopFilled size="1em" /></button>
{
state.game!.isPaused
? <>
<button onClick={() => state.game!.resume()}><IconPlayerPlayFilled size="1em" /></button>
<button onClick={() => state.game!.step()}><IconPlayerSkipForwardFilled size="1em" /></button>
</>
: <button onClick={() => state.game!.pause()}><IconPlayerPauseFilled size="1em" /></button>
}
</>
: <button onClick={() => state.startGame()}><IconPlayerPlayFilled size="1em" /></button>
}
</div>;
});
@@ -1,72 +0,0 @@
import { useEffect, useMemo, useRef } from "react";
import * as THREE from "three";
import { useThree, useFrame } from "@react-three/fiber";
import { observer } from "mobx-react-lite";
import { state } from "../../../state";
import { computeBoundingBox } from "../../../utils/runtime";
const _offset = new THREE.Vector3();
const _lookAt = new THREE.Vector3();
const _euler = new THREE.Euler(0, 0, 0, 'YXZ');
export const SceneEditorPlayerGizmosLayer = observer(function () {
const scene = useThree(s => s.scene);
const helperRef = useRef<THREE.CameraHelper | null>(null);
const playerCam = useMemo(() => new THREE.PerspectiveCamera(75, 16 / 9, 0.1, 1000), []);
const playerObjectId = state.world.data.initialScene.playerObjectId;
const playerObject = playerObjectId
? state.world.data.initialScene.objects[playerObjectId]
: undefined;
const playerType = playerObject
? state.world.data.objectTypes[playerObject.typeId]
: undefined;
useEffect(() => {
if (!playerObject || !playerType)
return;
const helper = new THREE.CameraHelper(playerCam);
scene.add(helper);
helperRef.current = helper;
return () => {
scene.remove(helper);
helper.dispose();
helperRef.current = null;
};
}, [playerObject, playerType, playerCam, scene]);
useFrame(() => {
if (!playerObject || !playerType || !helperRef.current)
return;
const bb = computeBoundingBox(playerType);
const W = bb.max[0] - bb.min[0];
const H = bb.max[1] - bb.min[1];
const D = bb.max[2] - bb.min[2];
const radius = Math.sqrt(W * W + H * H + D * D);
const centerY = (bb.min[1] + bb.max[1]) / 2;
const yaw = playerObject.rotation[1];
_offset.set(W * 0.1, centerY + H * 0.3, bb.max[2] + radius);
_offset.applyEuler(_euler.set(0, yaw, 0, 'YXZ'));
playerCam.position.set(
playerObject.position[0] + _offset.x,
playerObject.position[1] + _offset.y,
playerObject.position[2] + _offset.z,
);
_lookAt.set(
playerObject.position[0],
playerObject.position[1] + centerY,
playerObject.position[2],
);
playerCam.lookAt(_lookAt);
playerCam.updateMatrixWorld(true);
helperRef.current.update();
});
return null;
});
@@ -49,15 +49,10 @@ export const StatsLayer = function () {
];
for (const [name, entry] of Object.entries(layerTimings)) {
const frameTime = entry.frame.frames > 0 ? (entry.frame.acc / entry.frame.frames) : 0;
const renderTime = entry.render.frames > 0 ? (entry.render.acc / entry.render.frames) : 0;
lines.push(
`${name} ${(renderTime + frameTime).toFixed(3)} ms` +
(frameTime > 0
? ` (${frameTime.toFixed(3)} frame)`
: ''
)
);
const parts: string[] = [];
parts.push(`frame ${(entry.frame.frames > 0 ? (entry.frame.acc / entry.frame.frames) : 0).toFixed(3)}`);
parts.push(`render ${(entry.render.frames > 0 ? (entry.render.acc / entry.render.frames) : 0).toFixed(3)}`);
lines.push(`${name} ${parts.join(" ")} ms`);
entry.frame.acc = 0;
entry.frame.frames = 0;
+2 -1
View File
@@ -1,6 +1,7 @@
import { toJS } from "mobx";
import type { Game, Scene, ScriptAction, ScriptCoroutineContext, World } from "../types";
import { clone } from "../utils";
import { populateRuntimeScene } from "../utils/runtime";
import { ScriptGameEventBus } from "../scripts/eventBus";
import * as ScriptActions from "../scripts/actions";
import { positionOfObject } from "../scripts/positionOfObject";
@@ -18,7 +19,7 @@ import type { ScriptCoroutines } from "../scripts/coroutines";
export class GameFactory {
public static create(world: World): Game {
const scene = clone(world.initialScene);
const scene = populateRuntimeScene(clone(world.initialScene), world);
const game = {
paused: false,
+9 -8
View File
@@ -1,10 +1,11 @@
import { type World } from "../types";
import { type RuntimeWorld, type World } from "../types";
import { depopulateRuntimeWorld, populateRuntimeWorld } from "../utils/runtime";
import { DEFAULT_VOXEL_TYPES } from "./defaultVoxelTypes";
export class WorldFactory {
public static create(): World {
return {
public static create(): RuntimeWorld {
return populateRuntimeWorld({
objectTypes: {},
voxelTypes: DEFAULT_VOXEL_TYPES,
initialScene: {
@@ -18,17 +19,17 @@ export class WorldFactory {
position: [0, 5, 10],
look: [0, 0, 0],
},
};
});
}
public static load(): World | undefined {
public static load(): RuntimeWorld | undefined {
const json = localStorage.getItem("world");
if (json) {
return JSON.parse(json) as World;
return populateRuntimeWorld(JSON.parse(json) as World);
}
}
public static save(world: World): void {
localStorage.setItem("world", JSON.stringify(world));
public static save(world: RuntimeWorld): void {
localStorage.setItem("world", JSON.stringify(depopulateRuntimeWorld(world)));
}
}
+2 -2
View File
@@ -1,6 +1,6 @@
import type { ObjectInstance, V3 } from "../types";
import type { RuntimeGameObjectInstance, V3 } from "../types";
export function positionOfObject(obj: ObjectInstance): V3 {
export function positionOfObject(obj: RuntimeGameObjectInstance): V3 {
const t = obj.rb()?.translation();
const result: V3 = t
? [t.x, t.y, t.z]
-7
View File
@@ -1,7 +0,0 @@
import { ViewItem } from "./base";
export class PlayerViewItem extends ViewItem {
constructor() {
super();
}
}
+2 -1
View File
@@ -4,6 +4,7 @@ import { type ObjectInstance, type Pos3, type R3, type V3, type ObjectType, type
import { createObjectInstance } from "../utils/object";
import { randomId } from "../utils";
import { state } from "./rootState";
import { populateRuntimeObject } from "../utils/runtime";
import { DEFAULT_VOXEL_TYPE } from "../model/defaultVoxelTypes";
export const ObjectEditModeEnum = [
@@ -129,7 +130,7 @@ export class WorldEditorState {
typeId: string,
pos: { x: number; y: number; z: number; },
): ObjectInstance {
const obj = createObjectInstance(randomId(), typeId);
const obj = populateRuntimeObject(createObjectInstance(randomId(), typeId), this.world.data);
obj.position = [pos.x, pos.y, pos.z];
obj.rotation = [0, 0, 0];
+3 -2
View File
@@ -5,6 +5,7 @@ import { state } from "./rootState";
import { DEFAULT_VOXEL_TYPES } from "../model/defaultVoxelTypes";
import { wolf } from "../model/objectPrefabs/wolf";
import { terrainXZ } from "../model/objectPrefabs/terrain";
import { populateRuntimeWorld } from "../utils/runtime";
export class WorldState {
public data: World = WorldFactory.create();
@@ -57,7 +58,7 @@ export class WorldState {
objects.map((obj) => [obj.id, obj]),
) as Record<string, ObjectInstance>;
this.data = {
this.data = populateRuntimeWorld({
objectTypes: {
wolf: {
id: 'wolf',
@@ -96,7 +97,7 @@ export class WorldState {
gameRules: {
gravity: true,
},
};
});
console.log(objects);
state.worldEditor.resetSelection();
+4 -4
View File
@@ -1,4 +1,4 @@
import type { ObjectInstance, ObjectType, Scene, World } from "../model";
import type { ObjectInstance, ObjectType, RuntimeGameObjectInstance, Scene, World } from "../model";
import type { ScriptAction } from "./actions";
export type ScriptCoroutine = Generator<ScriptAction, void, void>;
@@ -9,15 +9,15 @@ export type NamedScriptCoroutine = {
}
export type ScriptCoroutineContext = {
thisObject: ObjectInstance,
thisObject: RuntimeGameObjectInstance,
thisObjectType: ObjectType;
};
export type ScriptContext = ScriptCoroutineContext & {
world: World;
scene: Scene;
object?: ObjectInstance;
other?: ObjectInstance;
object?: RuntimeGameObjectInstance;
other?: RuntimeGameObjectInstance;
}
export type ScriptGameEventHandler = (context: ScriptContext) => ScriptCoroutine;
-3
View File
@@ -10,8 +10,5 @@ export function createObjectInstance(id: string, typeId: string): ObjectInstance
position: [0, 0, 0],
rotation: [0, 0, 0],
scale: [1, 1, 1],
linearVelocity: [0, 0, 0],
angularVelocity: [0, 0, 0],
};
}
+2
View File
@@ -1 +1,3 @@
export * from './world';
export * from './scene';
export * from './object';
+35 -1
View File
@@ -1,4 +1,6 @@
import type { ObjectType, V3 } from "../../types";
import { getObjectVoxelGroups } from "../graphics/voxelGroup";
import type { ObjectInstance, ObjectType, RuntimeObjectInstance, RuntimeObjectType, V3, World } from "../../types";
import { buildObjectTrimesh } from "../graphics/mesh";
export function computeBoundingBox(objectType: ObjectType): { min: V3; max: V3 } {
if (!objectType.voxels.length)
@@ -15,3 +17,35 @@ export function computeBoundingBox(objectType: ObjectType): { min: V3; max: V3 }
}
return { min: [minX, minY, minZ], max: [maxX, maxY, maxZ] };
}
export function populateRuntimeObjectType(value: ObjectType, world: World): RuntimeObjectType {
return {
...value,
runtime: {
voxelGroups: getObjectVoxelGroups(value, world.voxelTypes),
colliderMesh: buildObjectTrimesh(value, world.voxelTypes),
boundingBox: computeBoundingBox(value),
updatedAt: 0,
},
};
}
export function populateRuntimeObject(value: ObjectInstance, _world: World): RuntimeObjectInstance {
return {
...value,
runtime: {
rb: () => null,
},
};
}
export function depopulateRuntimeObjectType(value: RuntimeObjectType, _world: World): ObjectType {
const { runtime: _, ...result } = value;
return result;
}
export function depopulateRuntimeObject(value: RuntimeObjectInstance, _world: World): ObjectInstance {
const { runtime: _, ...result } = value;
return result;
}
+16
View File
@@ -0,0 +1,16 @@
import type { RuntimeScene, Scene, World } from "../../types";
import { depopulateRuntimeObject, populateRuntimeObject } from "./object";
export function populateRuntimeScene(scene: Scene, world: World): RuntimeScene {
return {
...scene,
objects: Object.fromEntries(Object.entries(scene.objects).map(([id, obj]) => [id, populateRuntimeObject(obj, world)])),
};
}
export function depopulateRuntimeScene(scene: RuntimeScene, world: World): Scene {
return {
...scene,
objects: Object.fromEntries(Object.entries(scene.objects).map(([id, obj]) => [id, depopulateRuntimeObject(obj, world)])),
};
}
+16
View File
@@ -0,0 +1,16 @@
import type { RuntimeWorld, World } from "../../types";
import { depopulateRuntimeScene, populateRuntimeScene } from "./scene";
export function populateRuntimeWorld(world: World): RuntimeWorld {
return {
...world,
initialScene: populateRuntimeScene(world.initialScene, world),
};
}
export function depopulateRuntimeWorld(world: RuntimeWorld): World {
return {
...world,
initialScene: depopulateRuntimeScene(world.initialScene, world),
};
}