-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add lua scripting support. (#252)
- Loading branch information
Showing
65 changed files
with
4,970 additions
and
1,277 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
local menuData = world.resources:get(schema("MenuData")) | ||
|
||
-- Increment the frame counter | ||
menuData.frame = menuData.frame + 1 | ||
|
||
if menuData.frame % 30 == 0 then | ||
info(menuData) | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "demo_scripting" | ||
edition.workspace = true | ||
version.workspace = true | ||
license.workspace = true | ||
publish = false | ||
|
||
[dependencies] | ||
bones_framework = { path = "../../framework_crates/bones_framework" } | ||
bones_bevy_renderer = { path = "../../framework_crates/bones_bevy_renderer" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
plugins: | ||
- plugin1.plugin.lua | ||
version: 1 | ||
info: ./info.yaml | ||
sprite: ./image.png |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
name: Scripting Demo Game | ||
gravity: -9.8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
root: game.yaml | ||
schemas: | ||
- schemas/DemoSprite.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
local Vec3 = s"Vec3" | ||
local Transform = s"Transform" | ||
local Sprite = s"Sprite" | ||
local Time = s"Time" | ||
local Entities = s"Entities" | ||
local DemoSprite = s"DemoSprite" | ||
|
||
local function startup() | ||
local meta = assets.root | ||
local entities = resources:get(Entities) | ||
|
||
local ent = entities:create() | ||
components:insert(ent, Transform:create()) | ||
local sprite = Sprite:create() | ||
sprite.image = meta.sprite | ||
components:insert(ent, sprite) | ||
components:insert(ent, DemoSprite:create()) | ||
end | ||
|
||
local function update() | ||
local entities = resources:get(Entities) | ||
local time = resources:get(Time) | ||
|
||
for ent, t, s in entities:iter_with(Transform, Sprite, DemoSprite) do | ||
t.translation.x = math.sin(time.elapsed_seconds * 2) * 100 | ||
t.translation.y = math.sin(time.elapsed_seconds * 1.8) * 100 | ||
end | ||
end | ||
|
||
session:add_startup_system(startup) | ||
session:add_system_to_stage(CoreStage.Update, update) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# A marker type for our demo sprite. | ||
name: DemoSprite | ||
full_name: demo_scripting::DemoSprite | ||
kind: !Struct |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
use bones_bevy_renderer::BonesBevyRenderer; | ||
use bones_framework::prelude::*; | ||
|
||
#[derive(HasSchema, Default, Clone)] | ||
#[type_data(metadata_asset("game"))] | ||
#[repr(C)] | ||
struct GameMeta { | ||
plugins: SVec<Handle<LuaPlugin>>, | ||
version: u32, | ||
sprite: Handle<Image>, | ||
info: Handle<GameInfoMeta>, | ||
} | ||
|
||
#[derive(HasSchema, Default, Clone)] | ||
#[repr(C)] | ||
#[type_data(metadata_asset("info"))] | ||
struct GameInfoMeta { | ||
name: String, | ||
gravity: f32, | ||
} | ||
|
||
#[derive(HasSchema, Default, Clone)] | ||
#[repr(C)] | ||
struct DemoData { | ||
name: String, | ||
age: f32, | ||
favorite_things: SVec<String>, | ||
attributes: SMap<String, f32>, | ||
best_friend: Maybe<String>, | ||
state: DemoState, | ||
} | ||
|
||
#[derive(HasSchema, Default, Clone)] | ||
#[repr(C, u8)] | ||
pub enum DemoState { | ||
#[default] | ||
Ready, | ||
Thinking(f32), | ||
Finished { | ||
score: u32, | ||
}, | ||
} | ||
|
||
fn main() { | ||
let mut game = Game::new(); | ||
game.install_plugin(DefaultGamePlugin); | ||
GameMeta::register_schema(); | ||
DemoData::register_schema(); | ||
|
||
game.sessions | ||
.create("launch") | ||
.add_startup_system(launch_game_session); | ||
|
||
let mut renderer = BonesBevyRenderer::new(game); | ||
renderer.app_namespace = ( | ||
"org".into(), | ||
"fishfolk".into(), | ||
"bones.demo_scripting".into(), | ||
); | ||
renderer.app().run(); | ||
} | ||
|
||
fn launch_game_session( | ||
meta: Root<GameMeta>, | ||
mut sessions: ResMut<Sessions>, | ||
mut session_ops: ResMut<SessionOptions>, | ||
) { | ||
session_ops.delete = true; | ||
let game_session = sessions.create("game"); | ||
game_session | ||
.install_plugin(DefaultSessionPlugin) | ||
// Install the plugin that will load our lua plugins and run them in the game session | ||
.install_plugin(LuaPluginLoaderSessionPlugin( | ||
// Tell it to install the lua plugins specified in our game meta | ||
meta.plugins.iter().copied().collect(), | ||
)) | ||
.add_startup_system(game_startup); | ||
|
||
game_session.world.insert_resource(DemoData { | ||
name: "default name".into(), | ||
age: 10.0, | ||
favorite_things: ["candy".into(), "rain".into()].into_iter().collect(), | ||
attributes: [("coolness".into(), 50.0), ("friendliness".into(), 10.57)] | ||
.into_iter() | ||
.collect(), | ||
best_friend: Some("Jane".into()).into(), | ||
state: DemoState::Thinking(20.), | ||
}); | ||
} | ||
|
||
fn game_startup( | ||
mut entities: ResMut<Entities>, | ||
mut transforms: CompMut<Transform>, | ||
mut cameras: CompMut<Camera>, | ||
) { | ||
spawn_default_camera(&mut entities, &mut transforms, &mut cameras); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.