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 6ac75fc
Show file tree
Hide file tree
Showing 13 changed files with 312 additions and 122 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.

24 changes: 7 additions & 17 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,6 @@ name = "bevy_mod_js_scripting"
version = "0.1.0"
edition = "2021"

[features]
default = ["typescript"]
typescript = [
"swc_common",
"swc_ecma_codegen",
"swc_ecma_parser",
"swc_ecma_transforms_base",
"swc_ecma_transforms_typescript",
"swc_ecma_visit",
]

[dependencies]
anyhow = "1.0.57"
bevy = { version = "0.8.0", default-features = false, features = ["bevy_asset"] }
Expand All @@ -27,12 +16,13 @@ serde_json = "1.0"
bevy_ecs_dynamic = { git = "https://github.com/jakobhellermann/bevy_ecs_dynamic" }
bevy_reflect_fns = { git = "https://github.com/jakobhellermann/bevy_reflect_fns" }

swc_common = { version = "0.27.4", optional = true }
swc_ecma_codegen = { version = "0.121.2", optional = true }
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_common = { version = "0.27.4" }
swc_ecma_codegen = { version = "0.121.2" }
swc_ecma_parser = { version = "0.117.2" }
swc_ecma_transforms_base = { version = "0.103.4" }
swc_ecma_transforms_typescript = { version = "0.145.1" }
swc_ecma_visit = { version = "0.76.3" }
swc_atoms = { version = "0.4.3" }

[dev-dependencies]
bevy = { version = "0.8.0", default-features = false, features = ["render", "bevy_winit", "x11", "filesystem_watcher"] }
83 changes: 47 additions & 36 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, ""));
}


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();

function filterComponentInfos(
infos: ComponentInfo[],
prefix: string
): string[] {
return infos
.filter((info) => info.name.startsWith(prefix))
.map((info) => info.name.replace(prefix, ""));
}
84 changes: 44 additions & 40 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, ""));
}

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;

}

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());
}
export default {
update() {
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 componentId(name: string) {
let id = world.components.find((info) => info.name === name);
if (!id) throw new Error(`component id for ${name} not found`);
return id.id;
}

function init() {
return {
update: run,
}
}
function filterComponentInfos(
infos: ComponentInfo[],
prefix: string
): string[] {
return infos
.filter((info) => info.name.startsWith(prefix))
.map((info) => info.name.replace(prefix, ""));
}
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;
17 changes: 2 additions & 15 deletions src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,7 @@ impl AssetLoader for JsScriptLoader {
Box::pin(async move {
let source = String::from_utf8(bytes.to_vec())?;

#[cfg(feature = "typescript")]
let source = if load_context
.path()
.extension()
.map_or(false, |ext| ext == "ts")
{
crate::ts_to_js::ts_to_js(load_context.path(), &source)?
} else {
source
};
let source = crate::transpile::transpile(load_context.path(), &source)?;

load_context.set_default_asset(LoadedAsset::new(JsScript {
source,
Expand All @@ -40,10 +31,6 @@ impl AssetLoader for JsScriptLoader {
}

fn extensions(&self) -> &[&str] {
#[cfg(feature = "typescript")]
let exts = &["js", "ts"];
#[cfg(not(feature = "typescript"))]
let exts = &["js", "ts"];
exts
&["js", "ts"]
}
}
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,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
Loading

0 comments on commit 6ac75fc

Please sign in to comment.