Skip to content

Commit

Permalink
Use Module Syntax For Scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
zicklag committed Aug 13, 2022
1 parent ae9fc55 commit 72f2f1b
Show file tree
Hide file tree
Showing 13 changed files with 304 additions and 88 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = false

[*.ts]
indent_size = 2
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ typescript = [
"swc_ecma_transforms_base",
"swc_ecma_transforms_typescript",
"swc_ecma_visit",
"swc_atoms"
]

[dependencies]
Expand All @@ -33,6 +34,7 @@ swc_ecma_parser = { version = "0.117.2", optional = true }
swc_ecma_transforms_base = { version = "0.103.4", optional = true }
swc_ecma_transforms_typescript = { version = "0.145.1", optional = true }
swc_ecma_visit = { version = "0.76.3", optional = true }
swc_atoms = { version = "0.4.3", optional = true }

[dev-dependencies]
bevy = { version = "0.8.0", default-features = false, features = ["render", "bevy_winit", "x11", "filesystem_watcher"] }
81 changes: 46 additions & 35 deletions assets/scripts/debug.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,53 @@

function filterComponentInfos(infos: ComponentInfo[], prefix: string): string[] {
return infos
.filter(info => info.name.startsWith(prefix))
.map(info => info.name.replace(prefix, ""));
function filterComponentInfos(
infos: ComponentInfo[],
prefix: string
): string[] {
return infos
.filter((info) => info.name.startsWith(prefix))
.map((info) => info.name.replace(prefix, ""));
}


let firstIteration = true;
let i = 0;
function run() {
if (firstIteration) {
firstIteration = false;

info("Components: " + filterComponentInfos(world.components, "bevy_transform::"));
info("Resources: " + filterComponentInfos(world.resources, "breakout::").join(", "));
class Debug implements BevyScript {
firstIteration = true;
i = 0;
update() {
if (this.firstIteration) {
this.firstIteration = false;

info(
"Components: " +
filterComponentInfos(world.components, "bevy_transform::")
);
info(
"Resources: " +
filterComponentInfos(world.resources, "breakout::").join(", ")
);
}


i++;
if (i % 60 == 0) {
let ballId = world.components.find(info => info.name == "breakout::Ball").id;
let velocityId = world.components.find(info => info.name == "breakout::Velocity").id;
let transformId = world.components.find(info => info.name == "bevy_transform::components::transform::Transform").id;

const ballQuery = world.query({
components: [ballId, transformId, velocityId],
});
for (const item of ballQuery) {
let [ball, transform, velocity] = item.components;
velocity = velocity[0];

info(velocity.toString());
}
this.i++;
if (this.i % 60 == 0) {
let ballId = world.components.find(
(info) => info.name == "breakout::Ball"
).id;
let velocityId = world.components.find(
(info) => info.name == "breakout::Velocity"
).id;
let transformId = world.components.find(
(info) =>
info.name == "bevy_transform::components::transform::Transform"
).id;

const ballQuery = world.query({
components: [ballId, transformId, velocityId],
});
for (const item of ballQuery) {
let [ball, transform, velocity] = item.components;
velocity = velocity[0];

info(velocity.toString());
}
}
}
}

function init() {
return {
update: run,
};
}
export default new Debug();
82 changes: 43 additions & 39 deletions assets/scripts/headless.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,49 @@
function filterComponentInfos(infos: ComponentInfo[], prefix: string): string[] {
return infos
.filter(info => info.name.startsWith(prefix))
.map(info => info.name.replace(prefix, ""));
class Headless implements BevyScript {
firstIteration = true;
update() {
if (this.firstIteration) {
this.firstIteration = false;

// info("Components: " + world.components.map(info => info.name).join(", "));
// info("Resources:");
// info(world.resources.map(info => info.name));
// info("Resources (headless): " + filterComponentInfos(world.resources, "headless::").join(", "));
// info("Entitites: " + (world.entities.map(entity => `Entity(${entity.id}v${entity.generation})`).join(", ")));
info("----------");

let transformId = componentId(
"bevy_transform::components::transform::Transform"
);

let query = world.query({
components: [transformId],
});
let [transform1, transform2] = query.map((item) => item.components[0]);
let [translation1, translation2] = [
transform1.translation,
transform2.translation,
];

for (const s of [0.0, 0.25, 0.5, 0.75, 1.0]) {
info(translation1.lerp(translation2, s).toString());
}
}
}
}

function componentId(name) {
let id = world.components.find(info => info.name === name);
if (!id) throw new Error(`component id for ${name} not found`);
return id.id;

function componentId(name: string): ComponentId {
let id = world.components.find((info) => info.name === name);
if (!id) throw new Error(`component id for ${name} not found`);
return id.id;
}

let firstIteration = true;
function run() {
if (firstIteration) {
firstIteration = false;

// info("Components: " + world.components.map(info => info.name).join(", "));
// info("Resources:");
// info(world.resources.map(info => info.name));
// info("Resources (headless): " + filterComponentInfos(world.resources, "headless::").join(", "));
// info("Entitites: " + (world.entities.map(entity => `Entity(${entity.id}v${entity.generation})`).join(", ")));
info("----------");

let transformId = componentId("bevy_transform::components::transform::Transform");


let query = world.query({
components: [transformId]
});
let [transform1, transform2] = query.map(item => item.components[0]);
let [translation1, translation2] = [transform1.translation, transform2.translation];

for (const s of [0.0, 0.25, 0.5, 0.75, 1.0]) {
info(translation1.lerp(translation2, s).toString());
}
}
function filterComponentInfos(
infos: ComponentInfo[],
prefix: string
): string[] {
return infos
.filter((info) => info.name.startsWith(prefix))
.map((info) => info.name.replace(prefix, ""));
}

function init() {
return {
update: run,
}
}
export default new Headless();
53 changes: 53 additions & 0 deletions lib.bevy.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// log.s
declare function trace(val: any): void;
declare function debug(val: any): void;
declare function info(val: any): void;
declare function warn(val: any): void;
declare function error(val: any): void;

// ecs.js
declare interface BevyScript {
first?: () => void;
pre_update?: () => void;
update?: () => void;
post_update?: () => void;
last?: () => void;
}

declare class ComponentId {
index: number;
}
declare class Entity {
id: number;
generation: number;
}

declare interface ComponentInfo {
id: ComponentId;
name: string;
size: number;
}

declare interface QueryDescriptor {
components: ComponentId[];
}

declare interface QueryItem {
entity: Entity;
components: any[];
}

declare type Primitive = number | string | boolean;
declare interface Value {
[path: string | number]: Value | Primitive | undefined;
}

declare class World {
get components(): ComponentInfo[];
get resources(): ComponentInfo[];
get entities(): Entity[];

query(descriptor: QueryDescriptor): QueryItem[];
}

declare const world: World;
2 changes: 1 addition & 1 deletion src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl AssetLoader for JsScriptLoader {
.extension()
.map_or(false, |ext| ext == "ts")
{
crate::ts_to_js::ts_to_js(load_context.path(), &source)?
crate::transpile::transpile(load_context.path(), &source)?
} else {
source
};
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
mod asset;
mod runtime;
#[cfg(feature = "typescript")]
mod ts_to_js;
mod transpile;

use asset::{JsScript, JsScriptLoader};
use bevy::{asset::AssetStage, prelude::*};
Expand Down
72 changes: 72 additions & 0 deletions src/runtime/js/ecs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"use strict";

((window) => {

class ComponentId {
index;
}

class Entity {
#bits;
id;
generation;
}

class World {
get #rid() { return 0 }

toString() {
return Deno.core.opSync("op_world_tostring", this.rid);
}

get components() {
return Deno.core.opSync("op_world_components", this.rid);
}

get resources() {
return Deno.core.opSync("op_world_resources", this.rid);
}

get entities() {
return Deno.core.opSync("op_world_entities", this.rid);
}

query(descriptor) {
return Deno.core.opSync("op_world_query", this.rid, descriptor)
.map(({ entity, components }) => ({
entity,
components: components.map(wrapValueRef),
}));
}
}


const VALUE_REF_GET_INNER = Symbol("value_ref_get_inner");
function wrapValueRef(valueRef) {
// leaf primitives
if (typeof valueRef !== "object") { return valueRef };
const proxy = new Proxy(valueRef, {
ownKeys: (target) => {
return Deno.core.opSync("op_value_ref_keys", world.rid, target);
},
get: (target, p, receiver) => {
switch (p) {
case VALUE_REF_GET_INNER:
return target;
case "toString":
return () => Deno.core.opSync("op_value_ref_to_string", world.rid, target);
default:
let valueRef = Deno.core.opSync("op_value_ref_get", world.rid, target, "." + p);
return wrapValueRef(valueRef);
}
},
set: (target, p, value) => {
Deno.core.opSync("op_value_ref_set", world.rid, target, "." + p, value);
}
});
return proxy;
}

const world = new World();
window.world = world;
})(globalThis);
19 changes: 19 additions & 0 deletions src/runtime/js/log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"use strict";

((window) => {
window.trace = function (val) {
Deno.core.opSync("op_log", "trace", val);
}
window.debug = function (val) {
Deno.core.opSync("op_log", "debug", val)
}
window.info = function (val) {
Deno.core.opSync("op_log", "info", val)
}
window.warn = function (val) {
Deno.core.opSync("op_log", "warn", val)
}
window.error = function (val) {
Deno.core.opSync("op_log", "error", val)
}
})(globalThis);
Loading

0 comments on commit 72f2f1b

Please sign in to comment.