Compare commits
No commits in common. "23451eeb4b35c405ce31358212d4d082b112fc4f" and "b64d9c12f69b8e99db9853223fdadcdb6864da34" have entirely different histories.
23451eeb4b
...
b64d9c12f6
|
@ -816,7 +816,6 @@ dependencies = [
|
||||||
"rapier2d",
|
"rapier2d",
|
||||||
"rhai",
|
"rhai",
|
||||||
"serde",
|
"serde",
|
||||||
"smartstring",
|
|
||||||
"toml",
|
"toml",
|
||||||
"walkdir",
|
"walkdir",
|
||||||
]
|
]
|
||||||
|
@ -877,7 +876,6 @@ dependencies = [
|
||||||
"galactica-content",
|
"galactica-content",
|
||||||
"galactica-playeragent",
|
"galactica-playeragent",
|
||||||
"galactica-util",
|
"galactica-util",
|
||||||
"log",
|
|
||||||
"nalgebra",
|
"nalgebra",
|
||||||
"rand",
|
"rand",
|
||||||
"rapier2d",
|
"rapier2d",
|
||||||
|
|
|
@ -75,4 +75,3 @@ clap = { version = "4.4.18", features = ["derive"] }
|
||||||
log = "0.4.20"
|
log = "0.4.20"
|
||||||
log4rs = { version = "1.2.0", features = ["console_appender"] }
|
log4rs = { version = "1.2.0", features = ["console_appender"] }
|
||||||
rhai = { version = "1.17.1", features = ["f32_float", "no_custom_syntax"] }
|
rhai = { version = "1.17.1", features = ["f32_float", "no_custom_syntax"] }
|
||||||
smartstring = { version = "1.0.1" }
|
|
||||||
|
|
15
TODO.md
15
TODO.md
|
@ -1,15 +1,15 @@
|
||||||
# Specific projects
|
# Specific projects
|
||||||
|
|
||||||
## Now:
|
## Now:
|
||||||
|
- Replace handles with Arcs in content
|
||||||
- clean up content
|
- clean up content
|
||||||
- Clean up state api
|
- Clean up state api
|
||||||
- Clean up & document UI api
|
- Persistent variables
|
||||||
- Persistent variables in ui scripts
|
|
||||||
- Better planet icons
|
- Better planet icons
|
||||||
- Clean up scripting errors
|
- Clean up scripting errors
|
||||||
- Mouse colliders
|
- Mouse colliders
|
||||||
- Fade sprites and text in scrollbox
|
- UI captures input?
|
||||||
- Selection while flying
|
- No UI zoom scroll
|
||||||
- outfitter
|
- outfitter
|
||||||
- fps textbox positioning
|
- fps textbox positioning
|
||||||
|
|
||||||
|
@ -24,10 +24,12 @@
|
||||||
- No wobble for ai ships & autopilot
|
- No wobble for ai ships & autopilot
|
||||||
- 🌟 User-configurable outfit space types
|
- 🌟 User-configurable outfit space types
|
||||||
- 🌟 Sticky radar
|
- 🌟 Sticky radar
|
||||||
|
- Configurable radar
|
||||||
- 🌟 Ship damage events
|
- 🌟 Ship damage events
|
||||||
- Better landing animation (slow down)
|
- Better landing animation (slow down)
|
||||||
- Land from farther away
|
- Land from farther away
|
||||||
- Ship collapse: damage + force events
|
- Ship collapse: damage + force events
|
||||||
|
- Redesign UI elements
|
||||||
- Background haze: 3d perlin?
|
- Background haze: 3d perlin?
|
||||||
- nova dust parallax
|
- nova dust parallax
|
||||||
- Motion blur
|
- Motion blur
|
||||||
|
@ -36,12 +38,13 @@
|
||||||
- Reverse engines + flares
|
- Reverse engines + flares
|
||||||
- Turn flares (physics by location?)
|
- Turn flares (physics by location?)
|
||||||
- Angled engines & guns
|
- Angled engines & guns
|
||||||
- Unified content dir
|
|
||||||
|
|
||||||
## Misc fixes & Optimizations
|
## Misc fixes & Optimizations
|
||||||
- 🌟 Better errors when content/asset dirs don't exist
|
- 🌟 Better errors when content/asset dirs don't exist
|
||||||
- Clear `// TODO:` comments
|
- Clear `// TODO:` comments
|
||||||
- Correct drawing order (player on top, landing ships)
|
- Correct drawing order (player on top, landing ships)
|
||||||
|
- Faster handles (better than a hashmap?)
|
||||||
|
- Check for handle leaks
|
||||||
- Better physshiphandle
|
- Better physshiphandle
|
||||||
- Clean up & faster frame timings (average)
|
- Clean up & faster frame timings (average)
|
||||||
- 🌟 Handle lost focus
|
- 🌟 Handle lost focus
|
||||||
|
@ -147,7 +150,6 @@
|
||||||
- Muzzle effect
|
- Muzzle effect
|
||||||
- Effect / sprite color variation
|
- Effect / sprite color variation
|
||||||
- UI Animations
|
- UI Animations
|
||||||
- in-game console?
|
|
||||||
|
|
||||||
|
|
||||||
## Game & Story
|
## Game & Story
|
||||||
|
@ -181,6 +183,7 @@
|
||||||
- Outfit pipeline
|
- Outfit pipeline
|
||||||
- Collision detection
|
- Collision detection
|
||||||
- Ship AI
|
- Ship AI
|
||||||
|
- Handles
|
||||||
- Content specification and pipeline
|
- Content specification and pipeline
|
||||||
- How packer and optimizations work, and why
|
- How packer and optimizations work, and why
|
||||||
- How big should sprite resolutions be? How about frame rate?
|
- How big should sprite resolutions be? How about frame rate?
|
||||||
|
|
2
assets
2
assets
|
@ -1 +1 @@
|
||||||
Subproject commit 1400f7bb89f1190a11a7371bb23778881073a49f
|
Subproject commit fba4f1083b5a07a10445cf28bcae4bb05c2cede6
|
|
@ -1,5 +1,4 @@
|
||||||
[outfit."plasma engines"]
|
[outfit."plasma engines"]
|
||||||
name = "Plasma Engines"
|
|
||||||
thumbnail = "icon::engine"
|
thumbnail = "icon::engine"
|
||||||
|
|
||||||
space.engine = 20
|
space.engine = 20
|
||||||
|
@ -12,7 +11,6 @@ steering.power = 20
|
||||||
|
|
||||||
[outfit."shield generator"]
|
[outfit."shield generator"]
|
||||||
thumbnail = "icon::shield"
|
thumbnail = "icon::shield"
|
||||||
name = "Shield Generator"
|
|
||||||
|
|
||||||
space.outfit = 5
|
space.outfit = 5
|
||||||
shield.generation = 10
|
shield.generation = 10
|
||||||
|
@ -21,8 +19,7 @@ shield.delay = 2.0
|
||||||
|
|
||||||
|
|
||||||
[outfit."blaster"]
|
[outfit."blaster"]
|
||||||
thumbnail = "icon::blaster"
|
thumbnail = "icon::shield"
|
||||||
name = "Blaster"
|
|
||||||
|
|
||||||
space.weapon = 10
|
space.weapon = 10
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
[ship."gypsum"]
|
[ship."Gypsum"]
|
||||||
name = "Gypsum"
|
|
||||||
sprite = "ship::gypsum"
|
sprite = "ship::gypsum"
|
||||||
thumbnail = "icon::gypsum"
|
thumbnail = "icon::gypsum"
|
||||||
size = 100
|
size = 100
|
||||||
|
@ -66,7 +65,7 @@ collision = [
|
||||||
|
|
||||||
|
|
||||||
# Scripted explosion
|
# Scripted explosion
|
||||||
[[ship."gypsum".collapse.event]]
|
[[ship."Gypsum".collapse.event]]
|
||||||
time = 4.9
|
time = 4.9
|
||||||
effects = [
|
effects = [
|
||||||
#[rustfmt:skip],
|
#[rustfmt:skip],
|
||||||
|
@ -77,7 +76,7 @@ effects = [
|
||||||
]
|
]
|
||||||
|
|
||||||
# Scripted explosion
|
# Scripted explosion
|
||||||
[[ship."gypsum".collapse.event]]
|
[[ship."Gypsum".collapse.event]]
|
||||||
time = 0.0
|
time = 0.0
|
||||||
effects = [
|
effects = [
|
||||||
#[rustfmt:skip],
|
#[rustfmt:skip],
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
# TODO: big objects in one config
|
# TODO: big objects in one config
|
||||||
# TODO: satisfy conditions to land
|
|
||||||
|
|
||||||
[system."12 Autumn Above"]
|
[system."12 Autumn Above"]
|
||||||
name = "12 Autumn Above"
|
|
||||||
object.star.sprite = "star::star"
|
object.star.sprite = "star::star"
|
||||||
object.star.position = [0.0, 0.0, 30.0]
|
object.star.position = [0.0, 0.0, 30.0]
|
||||||
object.star.size = 2000
|
object.star.size = 2000
|
||||||
|
@ -14,9 +13,10 @@ object.earth.position.angle = 0
|
||||||
object.earth.position.z = 10.0
|
object.earth.position.z = 10.0
|
||||||
object.earth.size = 1000
|
object.earth.size = 1000
|
||||||
|
|
||||||
|
# TODO: satisfy conditions to land
|
||||||
object.earth.landable.name = "Earth"
|
object.earth.landable = true
|
||||||
object.earth.landable.desc = """
|
object.earth.name = "Earth"
|
||||||
|
object.earth.desc = """
|
||||||
The ancestral home world of humanity, Earth has a population twice that of any other inhabited planet.
|
The ancestral home world of humanity, Earth has a population twice that of any other inhabited planet.
|
||||||
Sprawling cities cover large portions of its surface, many of them overcrowded and dangerous.
|
Sprawling cities cover large portions of its surface, many of them overcrowded and dangerous.
|
||||||
Some people work to scrape together enough money to leave, while at the same time others, born
|
Some people work to scrape together enough money to leave, while at the same time others, born
|
||||||
|
@ -28,13 +28,8 @@ one planet has a greater population than a hundred planets elsewhere. As a resul
|
||||||
settlements of less than a million are grouped together into planetary districts that
|
settlements of less than a million are grouped together into planetary districts that
|
||||||
elect a single representative between them - a source of much frustration in the frontier worlds.
|
elect a single representative between them - a source of much frustration in the frontier worlds.
|
||||||
"""
|
"""
|
||||||
object.earth.landable.image = "ui::landscape::test"
|
object.earth.image = "ui::landscape::test"
|
||||||
|
|
||||||
object.earth.landable.outfitter = [
|
|
||||||
"plasma engines",
|
|
||||||
"shield generator",
|
|
||||||
"blaster",
|
|
||||||
]
|
|
||||||
|
|
||||||
object.luna.sprite = "planet::luna"
|
object.luna.sprite = "planet::luna"
|
||||||
object.luna.position.center = "earth"
|
object.luna.position.center = "earth"
|
||||||
|
|
|
@ -82,31 +82,6 @@ fn event(state, event) {
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if type_of(event) == "ScrollEvent" {
|
|
||||||
return ui::set_camera_zoom(
|
|
||||||
ui::get_camera_zoom()
|
|
||||||
- (5.0 * event.val())
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if type_of(event) == "KeyboardEvent" {
|
|
||||||
if event.key() == "up" {
|
|
||||||
return PlayerDirective::Engine(event.is_down());
|
|
||||||
}
|
|
||||||
if event.key() == "left" {
|
|
||||||
return PlayerDirective::TurnLeft(event.is_down());
|
|
||||||
}
|
|
||||||
if event.key() == "right" {
|
|
||||||
return PlayerDirective::TurnRight(event.is_down());
|
|
||||||
}
|
|
||||||
if event.key() == "space" {
|
|
||||||
return PlayerDirective::Guns(event.is_down());
|
|
||||||
}
|
|
||||||
if event.key() == "L" && event.is_down() {
|
|
||||||
return PlayerDirective::Land;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn step(state) {
|
fn step(state) {
|
||||||
|
@ -156,7 +131,7 @@ fn step(state) {
|
||||||
// Ships
|
// Ships
|
||||||
{
|
{
|
||||||
for s in state.ships() {
|
for s in state.ships() {
|
||||||
let uid = s.phys_uid();
|
let uid = s.get_uid();
|
||||||
let sprite_name = `radar.ship.${uid}`;
|
let sprite_name = `radar.ship.${uid}`;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
|
@ -225,8 +200,7 @@ fn step(state) {
|
||||||
// System objects
|
// System objects
|
||||||
{
|
{
|
||||||
for o in state.objects() {
|
for o in state.objects() {
|
||||||
let l = o.get_index();
|
let sprite_name = `radar.object.${o.get_label()}`;
|
||||||
let sprite_name = `radar.object.${l}`;
|
|
||||||
|
|
||||||
if !o.is_some() {
|
if !o.is_some() {
|
||||||
if sprite::exists(sprite_name) {
|
if sprite::exists(sprite_name) {
|
||||||
|
@ -323,5 +297,5 @@ fn step(state) {
|
||||||
Anchor::NorthWest
|
Anchor::NorthWest
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -55,7 +55,7 @@ fn init(state) {
|
||||||
textbox::font_serif("title");
|
textbox::font_serif("title");
|
||||||
textbox::weight_bold("title");
|
textbox::weight_bold("title");
|
||||||
if player.is_landed() {
|
if player.is_landed() {
|
||||||
textbox::set_text("title", player.landed_on().display_name());
|
textbox::set_text("title", player.landed_on().name());
|
||||||
}
|
}
|
||||||
|
|
||||||
textbox::add(
|
textbox::add(
|
||||||
|
@ -78,9 +78,9 @@ fn event(state, event) {
|
||||||
let element = event.element();
|
let element = event.element();
|
||||||
if element == "button" {
|
if element == "button" {
|
||||||
if event.is_enter() {
|
if event.is_enter() {
|
||||||
sprite::jump_to("button", "on:top", 0.1);
|
sprite::take_edge("button", "on:top", 0.1);
|
||||||
} else {
|
} else {
|
||||||
sprite::jump_to("button", "off:top", 0.1);
|
sprite::take_edge("button", "off:top", 0.1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
@ -100,22 +100,6 @@ fn event(state, event) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if type_of(event) == "KeyboardEvent" {
|
|
||||||
if !event.is_down() {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if event.key() == "L" {
|
|
||||||
return PlayerDirective::UnLand;
|
|
||||||
}
|
|
||||||
|
|
||||||
if event.key() == "O" {
|
|
||||||
ui::go_to_scene("outfitter");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if type_of(event) == "PlayerShipStateEvent" {
|
if type_of(event) == "PlayerShipStateEvent" {
|
||||||
if !state.player_ship().is_landed() {
|
if !state.player_ship().is_landed() {
|
||||||
ui::go_to_scene("flying");
|
ui::go_to_scene("flying");
|
||||||
|
|
|
@ -84,7 +84,7 @@ fn init(state) {
|
||||||
textbox::font_sans("ship_type");
|
textbox::font_sans("ship_type");
|
||||||
textbox::align_center("ship_type");
|
textbox::align_center("ship_type");
|
||||||
if state.player_ship().is_some() {
|
if state.player_ship().is_some() {
|
||||||
textbox::set_text("ship_type", state.player_ship().display_name());
|
textbox::set_text("ship_type", state.player_ship().name());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -161,80 +161,6 @@ fn init(state) {
|
||||||
textbox::font_mono("outfit_stats");
|
textbox::font_mono("outfit_stats");
|
||||||
textbox::set_text("outfit_stats", "Earth");
|
textbox::set_text("outfit_stats", "Earth");
|
||||||
|
|
||||||
|
|
||||||
// width should be calculated as a fraction of screen width
|
|
||||||
|
|
||||||
let scrollbox_rect = Rect(
|
|
||||||
222.0, -16.0, 470.0, 480.0,
|
|
||||||
Anchor::NorthWest,
|
|
||||||
Anchor::NorthWest,
|
|
||||||
);
|
|
||||||
|
|
||||||
scrollbox::add("outfit_list", scrollbox_rect);
|
|
||||||
|
|
||||||
let p = state.player_ship();
|
|
||||||
if p.is_landed() {
|
|
||||||
let s = "";
|
|
||||||
let x = scrollbox_rect.pos().x() + 45.0;
|
|
||||||
let y = scrollbox_rect.pos().y() - 45.0;
|
|
||||||
for xxx in ["1","2","3"] {
|
|
||||||
for i in p.landed_on().outfitter() {
|
|
||||||
s = s + i.display_name() + "\n";
|
|
||||||
|
|
||||||
let thumb_name = "outfit.thumb." + i.index() + xxx;
|
|
||||||
let backg_name = "outfit.backg." + i.index() + xxx;
|
|
||||||
let title_name = "outfit.title." + i.index() + xxx;
|
|
||||||
|
|
||||||
sprite::add(
|
|
||||||
backg_name,
|
|
||||||
"ui::outfitbg",
|
|
||||||
Rect(
|
|
||||||
x, y, 90.0, 90.0,
|
|
||||||
Anchor::Center,
|
|
||||||
Anchor::NorthWest
|
|
||||||
)
|
|
||||||
);
|
|
||||||
sprite::preserve_aspect(backg_name, true);
|
|
||||||
scrollbox::add_element("outfit_list", backg_name);
|
|
||||||
|
|
||||||
sprite::add(
|
|
||||||
thumb_name,
|
|
||||||
i.thumbnail(),
|
|
||||||
Rect(
|
|
||||||
x, y, 75.0, 75.0,
|
|
||||||
Anchor::Center,
|
|
||||||
Anchor::NorthWest
|
|
||||||
)
|
|
||||||
);
|
|
||||||
sprite::preserve_aspect(thumb_name, true);
|
|
||||||
scrollbox::add_element("outfit_list", thumb_name);
|
|
||||||
|
|
||||||
textbox::add(
|
|
||||||
title_name,
|
|
||||||
10.0, 10.0,
|
|
||||||
Rect(
|
|
||||||
x, y - 50.0, 90.0, 10.0,
|
|
||||||
Anchor::Center,
|
|
||||||
Anchor::NorthWest,
|
|
||||||
),
|
|
||||||
Color(1.0, 1.0, 1.0, 1.0)
|
|
||||||
);
|
|
||||||
textbox::font_sans(title_name);
|
|
||||||
textbox::align_center(title_name);
|
|
||||||
textbox::set_text(title_name, i.display_name());
|
|
||||||
scrollbox::add_element("outfit_list", title_name);
|
|
||||||
|
|
||||||
x = x + 120.0;
|
|
||||||
if x > (
|
|
||||||
scrollbox_rect.pos().x() + scrollbox_rect.dim().x() - 45.0
|
|
||||||
) {
|
|
||||||
x = scrollbox_rect.pos().x() + 45.0;
|
|
||||||
y = y - 120.0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
textbox::set_text("outfit_stats", s);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -243,9 +169,9 @@ fn event(state, event) {
|
||||||
let element = event.element();
|
let element = event.element();
|
||||||
if element == "exit_button" {
|
if element == "exit_button" {
|
||||||
if event.is_enter() {
|
if event.is_enter() {
|
||||||
sprite::jump_to("exit_button", "on:top", 0.1);
|
sprite::take_edge("exit_button", "on:top", 0.1);
|
||||||
} else {
|
} else {
|
||||||
sprite::jump_to("exit_button", "off:top", 0.1);
|
sprite::take_edge("exit_button", "off:top", 0.1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -30,4 +30,3 @@ rapier2d = { workspace = true }
|
||||||
lazy_static = { workspace = true }
|
lazy_static = { workspace = true }
|
||||||
log = { workspace = true }
|
log = { workspace = true }
|
||||||
rhai = { workspace = true }
|
rhai = { workspace = true }
|
||||||
smartstring = { workspace = true }
|
|
||||||
|
|
|
@ -0,0 +1,64 @@
|
||||||
|
//! This module defines lightweight handles for every content type.
|
||||||
|
//! This isn't strictly necessary (we could refer to them using their string keys),
|
||||||
|
//! but this approach doesn't require frequent string cloning, and
|
||||||
|
//! gives each content type a distinct Rust type.
|
||||||
|
//!
|
||||||
|
//! We could also use raw references to content types, but that creates a mess of lifetimes
|
||||||
|
//! in our code. It's managable, but the approach here is simpler and easier to understand.
|
||||||
|
use std::{cmp::Eq, hash::Hash};
|
||||||
|
|
||||||
|
/// A lightweight representation of a sprite
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub struct SpriteHandle {
|
||||||
|
pub(crate) index: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A lightweight representation of system body
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub struct SystemObjectHandle {
|
||||||
|
/// TODO: pub in crate
|
||||||
|
pub system_handle: SystemHandle,
|
||||||
|
/// The index of this object in system.objects
|
||||||
|
pub body_index: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A lightweight representation of an outfit
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub struct OutfitHandle {
|
||||||
|
/// TODO: pub in crate, currently for debug (same with all other handles)
|
||||||
|
pub index: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A lightweight representation of a gun
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub struct GunHandle {
|
||||||
|
/// TODO
|
||||||
|
pub index: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A lightweight representation of a ship
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub struct ShipHandle {
|
||||||
|
/// TODO
|
||||||
|
pub index: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A lightweight representation of a star system
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub struct SystemHandle {
|
||||||
|
/// TODO
|
||||||
|
pub index: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A lightweight representation of a faction
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub struct FactionHandle {
|
||||||
|
/// TODO
|
||||||
|
pub index: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A lightweight representation of an effect
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub struct EffectHandle {
|
||||||
|
pub(crate) index: usize,
|
||||||
|
}
|
|
@ -1,6 +1,9 @@
|
||||||
#![warn(missing_docs)]
|
#![warn(missing_docs)]
|
||||||
|
|
||||||
//! This subcrate is responsible for loading, parsing, validating game content.
|
//! This subcrate is responsible for loading, parsing, validating game content,
|
||||||
|
//! which is usually stored in `./content`.
|
||||||
|
|
||||||
|
mod handle;
|
||||||
mod part;
|
mod part;
|
||||||
mod spriteautomaton;
|
mod spriteautomaton;
|
||||||
mod util;
|
mod util;
|
||||||
|
@ -8,21 +11,18 @@ mod util;
|
||||||
use anyhow::{bail, Context, Result};
|
use anyhow::{bail, Context, Result};
|
||||||
use galactica_packer::{SpriteAtlas, SpriteAtlasImage};
|
use galactica_packer::{SpriteAtlas, SpriteAtlasImage};
|
||||||
use log::warn;
|
use log::warn;
|
||||||
use rhai::ImmutableString;
|
|
||||||
use serde::{Deserialize, Deserializer};
|
|
||||||
use smartstring::{LazyCompact, SmartString};
|
|
||||||
use std::{
|
use std::{
|
||||||
|
//cell::OnceCell,
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
fmt::Display,
|
|
||||||
fs::File,
|
fs::File,
|
||||||
io::Read,
|
io::Read,
|
||||||
num::NonZeroU32,
|
num::NonZeroU32,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
sync::Arc,
|
|
||||||
};
|
};
|
||||||
use toml;
|
use toml;
|
||||||
use walkdir::WalkDir;
|
use walkdir::WalkDir;
|
||||||
|
|
||||||
|
pub use handle::*;
|
||||||
pub use part::*;
|
pub use part::*;
|
||||||
pub use spriteautomaton::*;
|
pub use spriteautomaton::*;
|
||||||
|
|
||||||
|
@ -123,49 +123,11 @@ trait Build {
|
||||||
Self: Sized;
|
Self: Sized;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The type we use to index content objects
|
|
||||||
/// These are only unique WITHIN each "type" of object!
|
|
||||||
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
|
|
||||||
pub struct ContentIndex(Arc<SmartString<LazyCompact>>);
|
|
||||||
|
|
||||||
impl From<ImmutableString> for ContentIndex {
|
|
||||||
fn from(value: ImmutableString) -> Self {
|
|
||||||
Self::new(&value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ContentIndex {
|
|
||||||
/// Make a new ContentIndex from a strign
|
|
||||||
pub fn new(s: &str) -> Self {
|
|
||||||
Self(SmartString::from(s).into())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get a &str to this index's content
|
|
||||||
pub fn as_str(&self) -> &str {
|
|
||||||
self.0.as_str()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'d> Deserialize<'d> for ContentIndex {
|
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
||||||
where
|
|
||||||
D: Deserializer<'d>,
|
|
||||||
{
|
|
||||||
let s = String::deserialize(deserializer)?;
|
|
||||||
Ok(Self::new(&s))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for ContentIndex {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
std::fmt::Display::fmt(&self.0, f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Stores temporary data while building context objects
|
/// Stores temporary data while building context objects
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) struct ContentBuildContext {
|
pub(crate) struct ContentBuildContext {
|
||||||
pub effect_index: HashMap<ContentIndex, Arc<Effect>>,
|
/// Map effect names to handles
|
||||||
|
pub effect_index: HashMap<String, EffectHandle>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ContentBuildContext {
|
impl ContentBuildContext {
|
||||||
|
@ -179,29 +141,22 @@ impl ContentBuildContext {
|
||||||
/// Represents static game content
|
/// Represents static game content
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Content {
|
pub struct Content {
|
||||||
|
/// Sprites
|
||||||
|
pub sprites: Vec<Sprite>,
|
||||||
|
/// Map strings to texture names.
|
||||||
|
/// This is only necessary because we need to hard-code a few texture names for UI elements.
|
||||||
|
sprite_index: HashMap<String, SpriteHandle>,
|
||||||
|
|
||||||
/// Keeps track of which images are in which texture
|
/// Keeps track of which images are in which texture
|
||||||
sprite_atlas: SpriteAtlas,
|
sprite_atlas: SpriteAtlas,
|
||||||
|
|
||||||
/// Sprites defined in this game
|
outfits: Vec<Outfit>,
|
||||||
pub sprites: HashMap<ContentIndex, Arc<Sprite>>,
|
guns: Vec<Gun>, // TODO: merge with outfit
|
||||||
|
ships: Vec<Ship>,
|
||||||
/// Outfits defined in this game
|
systems: Vec<System>,
|
||||||
pub outfits: HashMap<ContentIndex, Arc<Outfit>>,
|
factions: Vec<Faction>,
|
||||||
|
effects: Vec<Effect>,
|
||||||
/// Ships defined in this game
|
config: Config,
|
||||||
pub ships: HashMap<ContentIndex, Arc<Ship>>,
|
|
||||||
|
|
||||||
/// Systems defined in this game
|
|
||||||
pub systems: HashMap<ContentIndex, Arc<System>>,
|
|
||||||
|
|
||||||
/// Factions defined in this game
|
|
||||||
pub factions: HashMap<ContentIndex, Arc<Faction>>,
|
|
||||||
|
|
||||||
/// Effects defined in this game
|
|
||||||
pub effects: HashMap<ContentIndex, Arc<Effect>>,
|
|
||||||
|
|
||||||
/// Game configuration
|
|
||||||
pub config: Config,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Loading methods
|
// Loading methods
|
||||||
|
@ -269,12 +224,14 @@ impl Content {
|
||||||
},
|
},
|
||||||
|
|
||||||
sprite_atlas: atlas,
|
sprite_atlas: atlas,
|
||||||
systems: HashMap::new(),
|
systems: Vec::new(),
|
||||||
ships: HashMap::new(),
|
ships: Vec::new(),
|
||||||
outfits: HashMap::new(),
|
guns: Vec::new(),
|
||||||
sprites: HashMap::new(),
|
outfits: Vec::new(),
|
||||||
factions: HashMap::new(),
|
sprites: Vec::new(),
|
||||||
effects: HashMap::new(),
|
factions: Vec::new(),
|
||||||
|
effects: Vec::new(),
|
||||||
|
sprite_index: HashMap::new(),
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: enforce sprite and image limits
|
// TODO: enforce sprite and image limits
|
||||||
|
@ -328,6 +285,23 @@ impl Content {
|
||||||
|
|
||||||
// Access methods
|
// Access methods
|
||||||
impl Content {
|
impl Content {
|
||||||
|
/// Iterate over all valid system handles
|
||||||
|
pub fn iter_systems(&self) -> impl Iterator<Item = SystemHandle> {
|
||||||
|
(0..self.systems.len()).map(|x| SystemHandle { index: x })
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get a handle from a sprite name
|
||||||
|
pub fn get_sprite_handle(&self, name: &str) -> Option<SpriteHandle> {
|
||||||
|
self.sprite_index.get(name).map(|x| *x)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get a sprite from a handle
|
||||||
|
pub fn get_sprite(&self, h: SpriteHandle) -> &Sprite {
|
||||||
|
// In theory, this could fail if h has a bad index, but that shouldn't ever happen.
|
||||||
|
// The only handles that exist should be created by this crate.
|
||||||
|
return &self.sprites[h.index as usize];
|
||||||
|
}
|
||||||
|
|
||||||
/// Get the list of atlas files we may use
|
/// Get the list of atlas files we may use
|
||||||
pub fn atlas_files(&self) -> &Vec<String> {
|
pub fn atlas_files(&self) -> &Vec<String> {
|
||||||
return &self.sprite_atlas.atlas_list;
|
return &self.sprite_atlas.atlas_list;
|
||||||
|
@ -342,4 +316,63 @@ impl Content {
|
||||||
pub fn get_image(&self, idx: NonZeroU32) -> &SpriteAtlasImage {
|
pub fn get_image(&self, idx: NonZeroU32) -> &SpriteAtlasImage {
|
||||||
&self.sprite_atlas.get_by_idx(idx)
|
&self.sprite_atlas.get_by_idx(idx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get an outfit from a handle
|
||||||
|
pub fn get_outfit(&self, h: OutfitHandle) -> &Outfit {
|
||||||
|
return &self.outfits[h.index];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get a gun from a handle
|
||||||
|
pub fn get_gun(&self, h: GunHandle) -> &Gun {
|
||||||
|
return &self.guns[h.index];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get a ship from a handle
|
||||||
|
pub fn get_ship(&self, h: ShipHandle) -> &Ship {
|
||||||
|
return &self.ships[h.index];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get a system from a handle
|
||||||
|
pub fn get_system(&self, h: SystemHandle) -> &System {
|
||||||
|
return &self.systems[h.index];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get a system object from a handle
|
||||||
|
pub fn get_system_object(&self, h: SystemObjectHandle) -> &SystemObject {
|
||||||
|
return &self.get_system(h.system_handle).objects[h.body_index];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get a faction from a handle
|
||||||
|
pub fn get_faction(&self, h: FactionHandle) -> &Faction {
|
||||||
|
return &self.factions[h.index];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get an effect from a handle
|
||||||
|
pub fn get_effect(&self, h: EffectHandle) -> &Effect {
|
||||||
|
return &self.effects[h.index];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get content configuration
|
||||||
|
pub fn get_config(&self) -> &Config {
|
||||||
|
return &self.config;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
TODO: don't pass content around?
|
||||||
|
static mut CONTENT: OnceCell<Content> = OnceCell::new();
|
||||||
|
|
||||||
|
/// Initialize content::CONTENT with the given paths
|
||||||
|
pub fn init(content_dir: PathBuf, asset_dir: PathBuf, atlas_index: PathBuf) -> Result<()> {
|
||||||
|
let content = Content::load_dir(content_dir, asset_dir, atlas_index)?;
|
||||||
|
unsafe {
|
||||||
|
match CONTENT.set(content) {
|
||||||
|
Ok(()) => {}
|
||||||
|
Err(_) => {
|
||||||
|
bail!("cannot initialize content, already set.")
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
|
@ -1,22 +1,20 @@
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use std::{collections::HashMap, sync::Arc};
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::{Content, ContentBuildContext, ContentIndex, Sprite};
|
use crate::{handle::SpriteHandle, Content, ContentBuildContext, EffectHandle};
|
||||||
|
|
||||||
pub(crate) mod syntax {
|
pub(crate) mod syntax {
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use anyhow::{bail, Result};
|
use anyhow::{bail, Result};
|
||||||
use galactica_util::to_radians;
|
use galactica_util::to_radians;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use crate::{Content, ContentBuildContext, ContentIndex, StartEdge};
|
use crate::{Content, ContentBuildContext, EffectHandle, StartEdge};
|
||||||
// Raw serde syntax structs.
|
// Raw serde syntax structs.
|
||||||
// These are never seen by code outside this crate.
|
// These are never seen by code outside this crate.
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct Effect {
|
pub struct Effect {
|
||||||
pub sprite: ContentIndex,
|
pub sprite: String,
|
||||||
pub size: f32,
|
pub size: f32,
|
||||||
pub size_rng: Option<f32>,
|
pub size_rng: Option<f32>,
|
||||||
pub lifetime: TextOrFloat,
|
pub lifetime: TextOrFloat,
|
||||||
|
@ -61,11 +59,10 @@ pub(crate) mod syntax {
|
||||||
self,
|
self,
|
||||||
_build_context: &mut ContentBuildContext,
|
_build_context: &mut ContentBuildContext,
|
||||||
content: &mut Content,
|
content: &mut Content,
|
||||||
name: &str,
|
) -> Result<EffectHandle> {
|
||||||
) -> Result<Arc<super::Effect>> {
|
let sprite = match content.sprite_index.get(&self.sprite) {
|
||||||
let sprite = match content.sprites.get(&self.sprite) {
|
|
||||||
None => bail!("sprite `{}` doesn't exist", self.sprite),
|
None => bail!("sprite `{}` doesn't exist", self.sprite),
|
||||||
Some(t) => t.clone(),
|
Some(t) => *t,
|
||||||
};
|
};
|
||||||
|
|
||||||
let lifetime = match self.lifetime {
|
let lifetime = match self.lifetime {
|
||||||
|
@ -73,11 +70,12 @@ pub(crate) mod syntax {
|
||||||
TextOrFloat::Text(s) => {
|
TextOrFloat::Text(s) => {
|
||||||
if s == "inherit" {
|
if s == "inherit" {
|
||||||
// Match lifetime of first section of sprite
|
// Match lifetime of first section of sprite
|
||||||
match &sprite.start_at {
|
let sprite = content.get_sprite(sprite);
|
||||||
StartEdge::Top { section } | StartEdge::Bot { section } => {
|
let sec = match sprite.start_at {
|
||||||
section.frame_duration * section.frames.len() as f32
|
StartEdge::Top { section } => sprite.get_section(section),
|
||||||
}
|
StartEdge::Bot { section } => sprite.get_section(section),
|
||||||
}
|
};
|
||||||
|
sec.frame_duration * sec.frames.len() as f32
|
||||||
} else {
|
} else {
|
||||||
bail!("bad effect lifetime, must be float or \"inherit\"",)
|
bail!("bad effect lifetime, must be float or \"inherit\"",)
|
||||||
}
|
}
|
||||||
|
@ -109,8 +107,11 @@ pub(crate) mod syntax {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let e = Arc::new(super::Effect {
|
let handle = EffectHandle {
|
||||||
name: name.to_string(),
|
index: content.effects.len(),
|
||||||
|
};
|
||||||
|
content.effects.push(super::Effect {
|
||||||
|
handle,
|
||||||
sprite,
|
sprite,
|
||||||
velocity,
|
velocity,
|
||||||
size: self.size,
|
size: self.size,
|
||||||
|
@ -125,11 +126,7 @@ pub(crate) mod syntax {
|
||||||
fade_rng: self.fade_rng.unwrap_or(0.0),
|
fade_rng: self.fade_rng.unwrap_or(0.0),
|
||||||
});
|
});
|
||||||
|
|
||||||
content
|
return Ok(handle);
|
||||||
.effects
|
|
||||||
.insert(ContentIndex::new(&e.name), e.clone());
|
|
||||||
|
|
||||||
return Ok(e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,23 +135,22 @@ pub(crate) mod syntax {
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
#[serde(untagged)]
|
#[serde(untagged)]
|
||||||
pub enum EffectReference {
|
pub enum EffectReference {
|
||||||
Label(ContentIndex),
|
Label(String),
|
||||||
Effect(Effect),
|
Effect(Effect),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EffectReference {
|
impl EffectReference {
|
||||||
pub fn resolve(
|
pub fn to_handle(
|
||||||
self,
|
self,
|
||||||
build_context: &mut ContentBuildContext,
|
build_context: &mut ContentBuildContext,
|
||||||
content: &mut Content,
|
content: &mut Content,
|
||||||
name: &str,
|
) -> Result<EffectHandle> {
|
||||||
) -> Result<Arc<super::Effect>> {
|
|
||||||
// We do not insert anything into build_context here,
|
// We do not insert anything into build_context here,
|
||||||
// since inline effects cannot be referenced by name.
|
// since inline effects cannot be referenced by name.
|
||||||
Ok(match self {
|
Ok(match self {
|
||||||
Self::Effect(e) => e.add_to(build_context, content, name)?,
|
Self::Effect(e) => e.add_to(build_context, content)?,
|
||||||
Self::Label(l) => match build_context.effect_index.get(&l) {
|
Self::Label(l) => match build_context.effect_index.get(&l) {
|
||||||
Some(h) => h.clone(),
|
Some(h) => *h,
|
||||||
None => bail!("no effect named `{}`", l),
|
None => bail!("no effect named `{}`", l),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
@ -165,11 +161,11 @@ pub(crate) mod syntax {
|
||||||
/// The effect a projectile will spawn when it hits something
|
/// The effect a projectile will spawn when it hits something
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Effect {
|
pub struct Effect {
|
||||||
/// The sprite to use for this effect.
|
/// This effect's handle
|
||||||
pub sprite: Arc<Sprite>,
|
pub handle: EffectHandle,
|
||||||
|
|
||||||
/// This effect's name
|
/// The sprite to use for this effect.
|
||||||
pub name: String,
|
pub sprite: SpriteHandle,
|
||||||
|
|
||||||
/// The height of this effect, in game units.
|
/// The height of this effect, in game units.
|
||||||
pub size: f32,
|
pub size: f32,
|
||||||
|
@ -244,11 +240,9 @@ impl crate::Build for Effect {
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
for (effect_name, effect) in effects {
|
for (effect_name, effect) in effects {
|
||||||
let h = effect
|
let h = effect
|
||||||
.add_to(build_context, content, &effect_name)
|
.add_to(build_context, content)
|
||||||
.with_context(|| format!("while evaluating effect `{}`", effect_name))?;
|
.with_context(|| format!("while evaluating effect `{}`", effect_name))?;
|
||||||
build_context
|
build_context.effect_index.insert(effect_name, h);
|
||||||
.effect_index
|
|
||||||
.insert(ContentIndex::new(&effect_name), h);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
|
|
@ -1,15 +1,13 @@
|
||||||
use anyhow::{bail, Result};
|
use anyhow::{bail, Result};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::{collections::HashMap, sync::Arc};
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::{Content, ContentBuildContext, ContentIndex};
|
use crate::{handle::FactionHandle, Content, ContentBuildContext};
|
||||||
|
|
||||||
pub(crate) mod syntax {
|
pub(crate) mod syntax {
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use crate::ContentIndex;
|
|
||||||
// Raw serde syntax structs.
|
// Raw serde syntax structs.
|
||||||
// These are never seen by code outside this crate.
|
// These are never seen by code outside this crate.
|
||||||
|
|
||||||
|
@ -17,7 +15,7 @@ pub(crate) mod syntax {
|
||||||
pub struct Faction {
|
pub struct Faction {
|
||||||
pub display_name: String,
|
pub display_name: String,
|
||||||
pub color: [f32; 3],
|
pub color: [f32; 3],
|
||||||
pub relationship: HashMap<ContentIndex, super::Relationship>,
|
pub relationship: HashMap<String, super::Relationship>,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,18 +44,18 @@ pub enum Relationship {
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Faction {
|
pub struct Faction {
|
||||||
/// The name of this faction
|
/// The name of this faction
|
||||||
pub index: ContentIndex,
|
pub name: String,
|
||||||
|
|
||||||
/// The pretty name of this faction
|
|
||||||
pub display_name: String,
|
|
||||||
|
|
||||||
/// This faction's color.
|
/// This faction's color.
|
||||||
/// Format is RGB, with each color between 0 and 1.
|
/// Format is RGB, with each color between 0 and 1.
|
||||||
pub color: [f32; 3],
|
pub color: [f32; 3],
|
||||||
|
|
||||||
|
/// This faction's handle
|
||||||
|
pub handle: FactionHandle,
|
||||||
|
|
||||||
/// Relationships between this faction and other factions
|
/// Relationships between this faction and other factions
|
||||||
/// This is guaranteed to contain an entry for ALL factions.
|
/// This is guaranteed to contain an entry for ALL factions.
|
||||||
pub relationships: HashMap<ContentIndex, Relationship>,
|
pub relationships: HashMap<FactionHandle, Relationship>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl crate::Build for Faction {
|
impl crate::Build for Faction {
|
||||||
|
@ -68,21 +66,34 @@ impl crate::Build for Faction {
|
||||||
_build_context: &mut ContentBuildContext,
|
_build_context: &mut ContentBuildContext,
|
||||||
content: &mut Content,
|
content: &mut Content,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
for (faction_name, faction) in &factions {
|
// Keeps track of position in faction array.
|
||||||
|
// This lets us build FactionHandles before finishing all factions.
|
||||||
|
let faction_names: Vec<String> = factions.keys().map(|x| x.to_owned()).collect();
|
||||||
|
|
||||||
|
// Indexing will break if this is false.
|
||||||
|
assert!(content.factions.len() == 0);
|
||||||
|
|
||||||
|
for f_idx in 0..faction_names.len() {
|
||||||
|
let faction_name = &faction_names[f_idx];
|
||||||
|
let faction = &factions[faction_name];
|
||||||
|
|
||||||
|
// Handle for this faction
|
||||||
|
let h = FactionHandle { index: f_idx };
|
||||||
|
|
||||||
// Compute relationships
|
// Compute relationships
|
||||||
let mut relationships = HashMap::new();
|
let mut relationships = HashMap::new();
|
||||||
for (other_name, other_faction) in &factions {
|
for i in 0..faction_names.len() {
|
||||||
if let Some(r) = faction.relationship.get(&ContentIndex::new(other_name)) {
|
let f_other = &faction_names[i];
|
||||||
relationships.insert(ContentIndex::new(other_name), *r);
|
let h_other = FactionHandle { index: i };
|
||||||
|
if let Some(r) = faction.relationship.get(f_other) {
|
||||||
|
relationships.insert(h_other, *r);
|
||||||
} else {
|
} else {
|
||||||
// Default relationship, if not specified
|
// Default relationship, if not specified
|
||||||
|
|
||||||
// Look at reverse direction...
|
// Look at reverse direction...
|
||||||
let other = other_faction
|
let other = factions[f_other].relationship.get(faction_name);
|
||||||
.relationship
|
|
||||||
.get(&ContentIndex::new(&faction_name));
|
|
||||||
relationships.insert(
|
relationships.insert(
|
||||||
ContentIndex::new(other_name),
|
h_other,
|
||||||
// ... and pick a relationship based on that.
|
// ... and pick a relationship based on that.
|
||||||
match other {
|
match other {
|
||||||
Some(Relationship::Hostile) => Relationship::Hostile {},
|
Some(Relationship::Hostile) => Relationship::Hostile {},
|
||||||
|
@ -105,15 +116,12 @@ impl crate::Build for Faction {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
content.factions.insert(
|
content.factions.push(Self {
|
||||||
ContentIndex::new(faction_name),
|
name: faction_name.to_owned(),
|
||||||
Arc::new(Self {
|
handle: h,
|
||||||
index: ContentIndex::new(faction_name),
|
relationships,
|
||||||
display_name: faction.display_name.to_owned(),
|
color: faction.color,
|
||||||
relationships,
|
});
|
||||||
color: faction.color,
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
|
|
@ -1,18 +1,14 @@
|
||||||
use anyhow::{anyhow, Context, Result};
|
use anyhow::{anyhow, Context, Result};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::{collections::HashMap, sync::Arc};
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
resolve_edge_as_edge, Content, ContentBuildContext, ContentIndex, Effect, OutfitSpace,
|
handle::SpriteHandle, resolve_edge_as_edge, Content, ContentBuildContext, EffectHandle,
|
||||||
SectionEdge, Sprite,
|
OutfitHandle, OutfitSpace, SectionEdge,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub(crate) mod syntax {
|
pub(crate) mod syntax {
|
||||||
use std::sync::Arc;
|
use crate::{effect, part::outfitspace, sprite::syntax::SectionEdge, ContentBuildContext};
|
||||||
|
|
||||||
use crate::{
|
|
||||||
effect, part::outfitspace, sprite::syntax::SectionEdge, ContentBuildContext, ContentIndex,
|
|
||||||
};
|
|
||||||
use anyhow::{bail, Result};
|
use anyhow::{bail, Result};
|
||||||
use galactica_util::to_radians;
|
use galactica_util::to_radians;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
@ -21,8 +17,7 @@ pub(crate) mod syntax {
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct Outfit {
|
pub struct Outfit {
|
||||||
pub thumbnail: ContentIndex,
|
pub thumbnail: String,
|
||||||
pub name: String,
|
|
||||||
pub engine: Option<Engine>,
|
pub engine: Option<Engine>,
|
||||||
pub steering: Option<Steering>,
|
pub steering: Option<Steering>,
|
||||||
pub space: outfitspace::syntax::OutfitSpace,
|
pub space: outfitspace::syntax::OutfitSpace,
|
||||||
|
@ -46,7 +41,7 @@ pub(crate) mod syntax {
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct EngineFlare {
|
pub struct EngineFlare {
|
||||||
pub sprite: ContentIndex,
|
pub sprite: String,
|
||||||
pub on_start: Option<SectionEdge>,
|
pub on_start: Option<SectionEdge>,
|
||||||
pub on_stop: Option<SectionEdge>,
|
pub on_stop: Option<SectionEdge>,
|
||||||
}
|
}
|
||||||
|
@ -69,33 +64,30 @@ pub(crate) mod syntax {
|
||||||
build_context: &mut ContentBuildContext,
|
build_context: &mut ContentBuildContext,
|
||||||
content: &mut crate::Content,
|
content: &mut crate::Content,
|
||||||
) -> Result<super::Gun> {
|
) -> Result<super::Gun> {
|
||||||
let projectile_sprite = match content
|
let projectile_sprite_handle = match content.sprite_index.get(&self.projectile.sprite) {
|
||||||
.sprites
|
|
||||||
.get(&ContentIndex::new(&self.projectile.sprite))
|
|
||||||
{
|
|
||||||
None => bail!(
|
None => bail!(
|
||||||
"projectile sprite `{}` doesn't exist",
|
"projectile sprite `{}` doesn't exist",
|
||||||
self.projectile.sprite,
|
self.projectile.sprite,
|
||||||
),
|
),
|
||||||
Some(t) => t.clone(),
|
Some(t) => *t,
|
||||||
};
|
};
|
||||||
|
|
||||||
let impact_effect = match self.projectile.impact_effect {
|
let impact_effect = match self.projectile.impact_effect {
|
||||||
Some(e) => Some(e.resolve(build_context, content, "")?),
|
Some(e) => Some(e.to_handle(build_context, content)?),
|
||||||
None => None,
|
None => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let expire_effect = match self.projectile.expire_effect {
|
let expire_effect = match self.projectile.expire_effect {
|
||||||
Some(e) => Some(e.resolve(build_context, content, "")?),
|
Some(e) => Some(e.to_handle(build_context, content)?),
|
||||||
None => None,
|
None => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
return Ok(super::Gun {
|
return Ok(super::Gun {
|
||||||
rate: self.rate,
|
rate: self.rate,
|
||||||
rate_rng: self.rate_rng.unwrap_or(0.0),
|
rate_rng: self.rate_rng.unwrap_or(0.0),
|
||||||
projectile: Arc::new(super::Projectile {
|
projectile: super::Projectile {
|
||||||
force: self.projectile.force,
|
force: self.projectile.force,
|
||||||
sprite: projectile_sprite,
|
sprite: projectile_sprite_handle,
|
||||||
size: self.projectile.size,
|
size: self.projectile.size,
|
||||||
size_rng: self.projectile.size_rng,
|
size_rng: self.projectile.size_rng,
|
||||||
speed: self.projectile.speed,
|
speed: self.projectile.speed,
|
||||||
|
@ -110,7 +102,7 @@ pub(crate) mod syntax {
|
||||||
impact_effect,
|
impact_effect,
|
||||||
expire_effect,
|
expire_effect,
|
||||||
collider: self.projectile.collider,
|
collider: self.projectile.collider,
|
||||||
}),
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -137,16 +129,16 @@ pub(crate) mod syntax {
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Outfit {
|
pub struct Outfit {
|
||||||
/// This outfit's thumbnail
|
/// This outfit's thumbnail
|
||||||
pub thumbnail: Arc<Sprite>,
|
pub thumbnail: SpriteHandle,
|
||||||
|
|
||||||
/// How much space this outfit requires
|
/// How much space this outfit requires
|
||||||
pub space: OutfitSpace,
|
pub space: OutfitSpace,
|
||||||
|
|
||||||
/// The name of this outfit
|
/// This outfit's handle
|
||||||
pub display_name: String,
|
pub handle: OutfitHandle,
|
||||||
|
|
||||||
/// Thie outfit's index
|
/// The name of this outfit
|
||||||
pub index: ContentIndex,
|
pub name: String,
|
||||||
|
|
||||||
/// How much engine thrust this outfit produces
|
/// How much engine thrust this outfit produces
|
||||||
pub engine_thrust: f32,
|
pub engine_thrust: f32,
|
||||||
|
@ -157,7 +149,7 @@ pub struct Outfit {
|
||||||
/// The engine flare sprite this outfit creates.
|
/// The engine flare sprite this outfit creates.
|
||||||
/// Its location and size is determined by a ship's
|
/// Its location and size is determined by a ship's
|
||||||
/// engine points.
|
/// engine points.
|
||||||
pub engine_flare_sprite: Option<Arc<Sprite>>,
|
pub engine_flare_sprite: Option<SpriteHandle>,
|
||||||
|
|
||||||
/// Jump to this edge when engines turn on
|
/// Jump to this edge when engines turn on
|
||||||
pub engine_flare_on_start: Option<SectionEdge>,
|
pub engine_flare_on_start: Option<SectionEdge>,
|
||||||
|
@ -199,7 +191,7 @@ pub struct BallCollider {
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Gun {
|
pub struct Gun {
|
||||||
/// The projectile this gun produces
|
/// The projectile this gun produces
|
||||||
pub projectile: Arc<Projectile>,
|
pub projectile: Projectile,
|
||||||
|
|
||||||
/// Average delay between projectiles, in seconds.
|
/// Average delay between projectiles, in seconds.
|
||||||
pub rate: f32,
|
pub rate: f32,
|
||||||
|
@ -213,7 +205,7 @@ pub struct Gun {
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Projectile {
|
pub struct Projectile {
|
||||||
/// The projectile sprite
|
/// The projectile sprite
|
||||||
pub sprite: Arc<Sprite>,
|
pub sprite: SpriteHandle,
|
||||||
|
|
||||||
/// The average size of this projectile
|
/// The average size of this projectile
|
||||||
/// (height in game units)
|
/// (height in game units)
|
||||||
|
@ -242,10 +234,10 @@ pub struct Projectile {
|
||||||
pub angle_rng: f32,
|
pub angle_rng: f32,
|
||||||
|
|
||||||
/// The effect this projectile will spawn when it hits something
|
/// The effect this projectile will spawn when it hits something
|
||||||
pub impact_effect: Option<Arc<Effect>>,
|
pub impact_effect: Option<EffectHandle>,
|
||||||
|
|
||||||
/// The effect this projectile will spawn when it expires
|
/// The effect this projectile will spawn when it expires
|
||||||
pub expire_effect: Option<Arc<Effect>>,
|
pub expire_effect: Option<EffectHandle>,
|
||||||
|
|
||||||
/// Collider parameters for this projectile
|
/// Collider parameters for this projectile
|
||||||
pub collider: ProjectileCollider,
|
pub collider: ProjectileCollider,
|
||||||
|
@ -260,6 +252,10 @@ impl crate::Build for Outfit {
|
||||||
content: &mut Content,
|
content: &mut Content,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
for (outfit_name, outfit) in outfits {
|
for (outfit_name, outfit) in outfits {
|
||||||
|
let handle = OutfitHandle {
|
||||||
|
index: content.outfits.len(),
|
||||||
|
};
|
||||||
|
|
||||||
let gun = match outfit.gun {
|
let gun = match outfit.gun {
|
||||||
None => None,
|
None => None,
|
||||||
Some(g) => Some(
|
Some(g) => Some(
|
||||||
|
@ -268,7 +264,7 @@ impl crate::Build for Outfit {
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
|
|
||||||
let thumbnail = match content.sprites.get(&outfit.thumbnail) {
|
let thumb_handle = match content.sprite_index.get(&outfit.thumbnail) {
|
||||||
None => {
|
None => {
|
||||||
return Err(anyhow!(
|
return Err(anyhow!(
|
||||||
"thumbnail sprite `{}` doesn't exist",
|
"thumbnail sprite `{}` doesn't exist",
|
||||||
|
@ -276,14 +272,14 @@ impl crate::Build for Outfit {
|
||||||
))
|
))
|
||||||
.with_context(|| format!("in outfit `{}`", outfit_name));
|
.with_context(|| format!("in outfit `{}`", outfit_name));
|
||||||
}
|
}
|
||||||
Some(t) => t.clone(),
|
Some(t) => *t,
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut o = Self {
|
let mut o = Self {
|
||||||
index: ContentIndex::new(&outfit_name),
|
thumbnail: thumb_handle,
|
||||||
display_name: outfit.name,
|
|
||||||
thumbnail,
|
|
||||||
gun,
|
gun,
|
||||||
|
handle,
|
||||||
|
name: outfit_name.clone(),
|
||||||
engine_thrust: 0.0,
|
engine_thrust: 0.0,
|
||||||
steer_power: 0.0,
|
steer_power: 0.0,
|
||||||
engine_flare_sprite: None,
|
engine_flare_sprite: None,
|
||||||
|
@ -297,7 +293,7 @@ impl crate::Build for Outfit {
|
||||||
|
|
||||||
// Engine stats
|
// Engine stats
|
||||||
if let Some(engine) = outfit.engine {
|
if let Some(engine) = outfit.engine {
|
||||||
let sprite = match content.sprites.get(&engine.flare.sprite) {
|
let sprite_handle = match content.sprite_index.get(&engine.flare.sprite) {
|
||||||
None => {
|
None => {
|
||||||
return Err(anyhow!(
|
return Err(anyhow!(
|
||||||
"flare sprite `{}` doesn't exist",
|
"flare sprite `{}` doesn't exist",
|
||||||
|
@ -305,10 +301,11 @@ impl crate::Build for Outfit {
|
||||||
))
|
))
|
||||||
.with_context(|| format!("in outfit `{}`", outfit_name));
|
.with_context(|| format!("in outfit `{}`", outfit_name));
|
||||||
}
|
}
|
||||||
Some(t) => t.clone(),
|
Some(t) => *t,
|
||||||
};
|
};
|
||||||
o.engine_thrust = engine.thrust;
|
o.engine_thrust = engine.thrust;
|
||||||
o.engine_flare_sprite = Some(sprite.clone());
|
o.engine_flare_sprite = Some(sprite_handle);
|
||||||
|
let sprite = content.get_sprite(sprite_handle);
|
||||||
|
|
||||||
// Flare animation will traverse this edge when the player presses the thrust key
|
// Flare animation will traverse this edge when the player presses the thrust key
|
||||||
// This leads from the idle animation to the transition animation
|
// This leads from the idle animation to the transition animation
|
||||||
|
@ -318,9 +315,11 @@ impl crate::Build for Outfit {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
let x = x.unwrap();
|
let x = x.unwrap();
|
||||||
let mut e = resolve_edge_as_edge(&sprite.sections, &x.val, 0.0)
|
let mut e = resolve_edge_as_edge(&x.val, 0.0, |x| {
|
||||||
.with_context(|| format!("in outfit `{}`", outfit_name))?;
|
sprite.get_section_handle_by_name(x)
|
||||||
match &mut e {
|
})
|
||||||
|
.with_context(|| format!("in outfit `{}`", outfit_name))?;
|
||||||
|
match e {
|
||||||
// Inherit duration from transition sequence
|
// Inherit duration from transition sequence
|
||||||
SectionEdge::Top {
|
SectionEdge::Top {
|
||||||
section,
|
section,
|
||||||
|
@ -330,7 +329,7 @@ impl crate::Build for Outfit {
|
||||||
section,
|
section,
|
||||||
ref mut duration,
|
ref mut duration,
|
||||||
} => {
|
} => {
|
||||||
*duration = section.frame_duration;
|
*duration = sprite.get_section(section).frame_duration;
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
return Err(anyhow!(
|
return Err(anyhow!(
|
||||||
|
@ -352,9 +351,11 @@ impl crate::Build for Outfit {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
let x = x.unwrap();
|
let x = x.unwrap();
|
||||||
let mut e = resolve_edge_as_edge(&sprite.sections, &x.val, 0.0)
|
let mut e = resolve_edge_as_edge(&x.val, 0.0, |x| {
|
||||||
.with_context(|| format!("in outfit `{}`", outfit_name))?;
|
sprite.get_section_handle_by_name(x)
|
||||||
match &mut e {
|
})
|
||||||
|
.with_context(|| format!("in outfit `{}`", outfit_name))?;
|
||||||
|
match e {
|
||||||
// Inherit duration from transition sequence
|
// Inherit duration from transition sequence
|
||||||
SectionEdge::Top {
|
SectionEdge::Top {
|
||||||
section,
|
section,
|
||||||
|
@ -364,7 +365,7 @@ impl crate::Build for Outfit {
|
||||||
section,
|
section,
|
||||||
ref mut duration,
|
ref mut duration,
|
||||||
} => {
|
} => {
|
||||||
*duration = section.frame_duration;
|
*duration = sprite.get_section(section).frame_duration;
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
return Err(anyhow!(
|
return Err(anyhow!(
|
||||||
|
@ -391,7 +392,7 @@ impl crate::Build for Outfit {
|
||||||
o.shield_strength = shield.strength.unwrap_or(0.0);
|
o.shield_strength = shield.strength.unwrap_or(0.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
content.outfits.insert(o.index.clone(), Arc::new(o));
|
content.outfits.push(o);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
|
|
@ -2,15 +2,12 @@ use anyhow::{bail, Context, Result};
|
||||||
use galactica_util::to_radians;
|
use galactica_util::to_radians;
|
||||||
use nalgebra::{Point2, Rotation2, Vector2};
|
use nalgebra::{Point2, Rotation2, Vector2};
|
||||||
use rapier2d::geometry::{Collider, ColliderBuilder};
|
use rapier2d::geometry::{Collider, ColliderBuilder};
|
||||||
use std::{collections::HashMap, fmt::Debug, hash::Hash, sync::Arc};
|
use std::{collections::HashMap, fmt::Debug, hash::Hash};
|
||||||
|
|
||||||
use crate::{Content, ContentBuildContext, ContentIndex, Effect, OutfitSpace, Sprite};
|
use crate::{handle::SpriteHandle, Content, ContentBuildContext, EffectHandle, OutfitSpace};
|
||||||
|
|
||||||
pub(crate) mod syntax {
|
pub(crate) mod syntax {
|
||||||
use crate::{
|
use crate::part::{effect::syntax::EffectReference, outfitspace};
|
||||||
part::{effect::syntax::EffectReference, outfitspace},
|
|
||||||
ContentIndex,
|
|
||||||
};
|
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
// Raw serde syntax structs.
|
// Raw serde syntax structs.
|
||||||
|
@ -18,9 +15,8 @@ pub(crate) mod syntax {
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct Ship {
|
pub struct Ship {
|
||||||
pub name: String,
|
pub sprite: String,
|
||||||
pub sprite: ContentIndex,
|
pub thumbnail: String,
|
||||||
pub thumbnail: ContentIndex,
|
|
||||||
pub size: f32,
|
pub size: f32,
|
||||||
pub engines: Vec<Engine>,
|
pub engines: Vec<Engine>,
|
||||||
pub guns: Vec<Gun>,
|
pub guns: Vec<Gun>,
|
||||||
|
@ -95,17 +91,14 @@ pub(crate) mod syntax {
|
||||||
/// Represents a ship chassis.
|
/// Represents a ship chassis.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Ship {
|
pub struct Ship {
|
||||||
/// This ship's display name
|
/// This ship's name
|
||||||
pub display_name: String,
|
pub name: String,
|
||||||
|
|
||||||
/// This object's index
|
|
||||||
pub index: ContentIndex,
|
|
||||||
|
|
||||||
/// This ship's sprite
|
/// This ship's sprite
|
||||||
pub sprite: Arc<Sprite>,
|
pub sprite: SpriteHandle,
|
||||||
|
|
||||||
/// This ship's thumbnail
|
/// This ship's thumbnail
|
||||||
pub thumbnail: Arc<Sprite>,
|
pub thumbnail: SpriteHandle,
|
||||||
|
|
||||||
/// The size of this ship.
|
/// The size of this ship.
|
||||||
/// Measured as unrotated height,
|
/// Measured as unrotated height,
|
||||||
|
@ -129,6 +122,9 @@ pub struct Ship {
|
||||||
/// Collision shape for this ship
|
/// Collision shape for this ship
|
||||||
pub collider: CollisionDebugWrapper,
|
pub collider: CollisionDebugWrapper,
|
||||||
|
|
||||||
|
/// Remove later
|
||||||
|
pub aspect: f32,
|
||||||
|
|
||||||
/// Reduction in angular velocity over time
|
/// Reduction in angular velocity over time
|
||||||
pub angular_drag: f32,
|
pub angular_drag: f32,
|
||||||
|
|
||||||
|
@ -221,7 +217,7 @@ pub struct ShipDamage {
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct DamageEffectSpawner {
|
pub struct DamageEffectSpawner {
|
||||||
/// The effect to create
|
/// The effect to create
|
||||||
pub effect: Arc<Effect>,
|
pub effect: EffectHandle,
|
||||||
|
|
||||||
/// How often to create this effect
|
/// How often to create this effect
|
||||||
pub frequency: f32,
|
pub frequency: f32,
|
||||||
|
@ -235,7 +231,7 @@ pub struct DamageEffectSpawner {
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct CollapseEffectSpawner {
|
pub struct CollapseEffectSpawner {
|
||||||
/// The effect to create
|
/// The effect to create
|
||||||
pub effect: Arc<Effect>,
|
pub effect: EffectHandle,
|
||||||
|
|
||||||
/// How many effects to create
|
/// How many effects to create
|
||||||
pub count: f32,
|
pub count: f32,
|
||||||
|
@ -271,25 +267,26 @@ impl crate::Build for Ship {
|
||||||
ct: &mut Content,
|
ct: &mut Content,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
for (ship_name, ship) in ship {
|
for (ship_name, ship) in ship {
|
||||||
let sprite = match ct.sprites.get(&ship.sprite) {
|
let sprite = match ct.sprite_index.get(&ship.sprite) {
|
||||||
None => bail!(
|
None => bail!(
|
||||||
"In ship `{}`: sprite `{}` doesn't exist",
|
"In ship `{}`: sprite `{}` doesn't exist",
|
||||||
ship_name,
|
ship_name,
|
||||||
ship.sprite
|
ship.sprite
|
||||||
),
|
),
|
||||||
Some(t) => t.clone(),
|
Some(t) => *t,
|
||||||
};
|
};
|
||||||
|
|
||||||
let thumbnail = match ct.sprites.get(&ship.thumbnail) {
|
let thumbnail = match ct.sprite_index.get(&ship.thumbnail) {
|
||||||
None => bail!(
|
None => bail!(
|
||||||
"In ship `{}`: thumbnail sprite `{}` doesn't exist",
|
"In ship `{}`: thumbnail sprite `{}` doesn't exist",
|
||||||
ship_name,
|
ship_name,
|
||||||
ship.thumbnail
|
ship.thumbnail
|
||||||
),
|
),
|
||||||
Some(t) => t.clone(),
|
Some(t) => *t,
|
||||||
};
|
};
|
||||||
|
|
||||||
let size = ship.size;
|
let size = ship.size;
|
||||||
|
let aspect = ct.get_sprite(sprite).aspect;
|
||||||
|
|
||||||
let collapse = {
|
let collapse = {
|
||||||
if let Some(c) = ship.collapse {
|
if let Some(c) = ship.collapse {
|
||||||
|
@ -298,11 +295,11 @@ impl crate::Build for Ship {
|
||||||
effects.push(CollapseEffectSpawner {
|
effects.push(CollapseEffectSpawner {
|
||||||
effect: e
|
effect: e
|
||||||
.effect
|
.effect
|
||||||
.resolve(build_context, ct, "")
|
.to_handle(build_context, ct)
|
||||||
.with_context(|| format!("while loading ship `{}`", ship_name))?,
|
.with_context(|| format!("while loading ship `{}`", ship_name))?,
|
||||||
count: e.count,
|
count: e.count,
|
||||||
pos: e.pos.map(|p| {
|
pos: e.pos.map(|p| {
|
||||||
Point2::new(p[0] * (size / 2.0) * sprite.aspect, p[1] * size / 2.0)
|
Point2::new(p[0] * (size / 2.0) * aspect, p[1] * size / 2.0)
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -316,14 +313,14 @@ impl crate::Build for Ship {
|
||||||
effects.push(CollapseEffectSpawner {
|
effects.push(CollapseEffectSpawner {
|
||||||
effect: g
|
effect: g
|
||||||
.effect
|
.effect
|
||||||
.resolve(build_context, ct, "")
|
.to_handle(build_context, ct)
|
||||||
.with_context(|| {
|
.with_context(|| {
|
||||||
format!("while loading ship `{}`", ship_name)
|
format!("while loading ship `{}`", ship_name)
|
||||||
})?,
|
})?,
|
||||||
count: g.count,
|
count: g.count,
|
||||||
pos: g.pos.map(|p| {
|
pos: g.pos.map(|p| {
|
||||||
Point2::new(
|
Point2::new(
|
||||||
p[0] * (size / 2.0) * sprite.aspect,
|
p[0] * (size / 2.0) * aspect,
|
||||||
p[1] * size / 2.0,
|
p[1] * size / 2.0,
|
||||||
)
|
)
|
||||||
}),
|
}),
|
||||||
|
@ -360,11 +357,11 @@ impl crate::Build for Ship {
|
||||||
effects.push(DamageEffectSpawner {
|
effects.push(DamageEffectSpawner {
|
||||||
effect: e
|
effect: e
|
||||||
.effect
|
.effect
|
||||||
.resolve(build_context, ct, "")
|
.to_handle(build_context, ct)
|
||||||
.with_context(|| format!("while loading ship `{}`", ship_name))?,
|
.with_context(|| format!("while loading ship `{}`", ship_name))?,
|
||||||
frequency: e.frequency,
|
frequency: e.frequency,
|
||||||
pos: e.pos.map(|p| {
|
pos: e.pos.map(|p| {
|
||||||
Point2::new(p[0] * (size / 2.0) * sprite.aspect, p[1] * size / 2.0)
|
Point2::new(p[0] * (size / 2.0) * aspect, p[1] * size / 2.0)
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -391,7 +388,7 @@ impl crate::Build for Ship {
|
||||||
// Angle adjustment, since sprites point north
|
// Angle adjustment, since sprites point north
|
||||||
// and 0 degrees is east in the game
|
// and 0 degrees is east in the game
|
||||||
pos: Rotation2::new(to_radians(-90.0))
|
pos: Rotation2::new(to_radians(-90.0))
|
||||||
* Vector2::new(g.x * size * sprite.aspect / 2.0, g.y * size / 2.0),
|
* Vector2::new(g.x * size * aspect / 2.0, g.y * size / 2.0),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -421,7 +418,7 @@ impl crate::Build for Ship {
|
||||||
// If we don't, rapier2 will compute local points pre-rotation,
|
// If we don't, rapier2 will compute local points pre-rotation,
|
||||||
// which will break effect placement on top of ships (i.e, collapse effects)
|
// which will break effect placement on top of ships (i.e, collapse effects)
|
||||||
Rotation2::new(to_radians(-90.0))
|
Rotation2::new(to_radians(-90.0))
|
||||||
* Point2::new(x[0] * (size / 2.0) * sprite.aspect, x[1] * size / 2.0)
|
* Point2::new(x[0] * (size / 2.0) * aspect, x[1] * size / 2.0)
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
@ -430,36 +427,33 @@ impl crate::Build for Ship {
|
||||||
.build()
|
.build()
|
||||||
};
|
};
|
||||||
|
|
||||||
ct.ships.insert(
|
ct.ships.push(Self {
|
||||||
ContentIndex::new(&ship_name),
|
sprite,
|
||||||
Arc::new(Self {
|
thumbnail,
|
||||||
sprite: sprite.clone(),
|
aspect,
|
||||||
thumbnail,
|
collapse,
|
||||||
collapse,
|
damage,
|
||||||
damage,
|
name: ship_name,
|
||||||
index: ContentIndex::new(&ship_name),
|
mass: ship.mass,
|
||||||
display_name: ship.name,
|
space: OutfitSpace::from(ship.space),
|
||||||
mass: ship.mass,
|
angular_drag: ship.angular_drag,
|
||||||
space: OutfitSpace::from(ship.space),
|
linear_drag: ship.linear_drag,
|
||||||
angular_drag: ship.angular_drag,
|
size,
|
||||||
linear_drag: ship.linear_drag,
|
hull: ship.hull,
|
||||||
size,
|
|
||||||
hull: ship.hull,
|
|
||||||
|
|
||||||
engines: ship
|
engines: ship
|
||||||
.engines
|
.engines
|
||||||
.iter()
|
.iter()
|
||||||
.map(|e| EnginePoint {
|
.map(|e| EnginePoint {
|
||||||
pos: Vector2::new(e.x * size * sprite.aspect / 2.0, e.y * size / 2.0),
|
pos: Vector2::new(e.x * size * aspect / 2.0, e.y * size / 2.0),
|
||||||
size: e.size,
|
size: e.size,
|
||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
|
|
||||||
guns,
|
guns,
|
||||||
|
|
||||||
collider: CollisionDebugWrapper(collider),
|
collider: CollisionDebugWrapper(collider),
|
||||||
}),
|
});
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
use anyhow::{anyhow, bail, Context, Result};
|
use anyhow::{anyhow, bail, Context, Result};
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use std::{collections::HashMap, sync::Arc};
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::{Content, ContentBuildContext, ContentIndex};
|
use crate::{handle::SpriteHandle, Content, ContentBuildContext};
|
||||||
|
|
||||||
pub(crate) mod syntax {
|
pub(crate) mod syntax {
|
||||||
use crate::{Content, ContentIndex};
|
use crate::{AnimSectionHandle, Content};
|
||||||
use anyhow::{bail, Ok, Result};
|
use anyhow::{bail, Ok, Result};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::{collections::HashMap, path::PathBuf, sync::Arc};
|
use std::{collections::HashMap, path::PathBuf};
|
||||||
|
|
||||||
// Raw serde syntax structs.
|
// Raw serde syntax structs.
|
||||||
// These are never seen by code outside this crate.
|
// These are never seen by code outside this crate.
|
||||||
|
@ -49,7 +49,7 @@ pub(crate) mod syntax {
|
||||||
/// The proper, full sprite definition
|
/// The proper, full sprite definition
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct CompleteSprite {
|
pub struct CompleteSprite {
|
||||||
pub section: HashMap<ContentIndex, SpriteSection>,
|
pub section: HashMap<String, SpriteSection>,
|
||||||
pub start_at: SectionEdge,
|
pub start_at: SectionEdge,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,28 +63,14 @@ pub(crate) mod syntax {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SpriteSection {
|
impl SpriteSection {
|
||||||
pub fn resolve_edges(
|
pub fn add_to<F>(
|
||||||
&self,
|
&self,
|
||||||
sections: &HashMap<ContentIndex, Arc<super::SpriteSection>>,
|
ct: &mut Content,
|
||||||
sec: &mut super::SpriteSection,
|
get_handle: F,
|
||||||
) -> Result<()> {
|
) -> Result<((u32, u32), super::SpriteSection)>
|
||||||
let edge_top = match &self.top {
|
where
|
||||||
Some(x) => super::resolve_edge_as_edge(sections, &x.val, sec.frame_duration)?,
|
F: Fn(&str) -> Option<AnimSectionHandle>,
|
||||||
None => super::SectionEdge::Stop,
|
{
|
||||||
};
|
|
||||||
|
|
||||||
let edge_bot = match &self.bot {
|
|
||||||
Some(x) => super::resolve_edge_as_edge(sections, &x.val, sec.frame_duration)?,
|
|
||||||
None => super::SectionEdge::Stop,
|
|
||||||
};
|
|
||||||
|
|
||||||
sec.edge_bot = edge_bot;
|
|
||||||
sec.edge_top = edge_top;
|
|
||||||
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn add_to(&self, ct: &mut Content) -> Result<((u32, u32), super::SpriteSection)> {
|
|
||||||
// Make sure all frames have the same size and add them
|
// Make sure all frames have the same size and add them
|
||||||
// to the frame vector
|
// to the frame vector
|
||||||
let mut dim = None;
|
let mut dim = None;
|
||||||
|
@ -125,14 +111,23 @@ pub(crate) mod syntax {
|
||||||
bail!("frame duration must be positive (and therefore nonzero).")
|
bail!("frame duration must be positive (and therefore nonzero).")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let edge_top = match &self.top {
|
||||||
|
Some(x) => super::resolve_edge_as_edge(&x.val, frame_duration, &get_handle)?,
|
||||||
|
None => super::SectionEdge::Stop,
|
||||||
|
};
|
||||||
|
|
||||||
|
let edge_bot = match &self.bot {
|
||||||
|
Some(x) => super::resolve_edge_as_edge(&x.val, frame_duration, &get_handle)?,
|
||||||
|
None => super::SectionEdge::Stop,
|
||||||
|
};
|
||||||
|
|
||||||
return Ok((
|
return Ok((
|
||||||
dim,
|
dim,
|
||||||
super::SpriteSection {
|
super::SpriteSection {
|
||||||
frames,
|
frames,
|
||||||
frame_duration,
|
frame_duration,
|
||||||
// These are changed later, after all sections are built
|
edge_top,
|
||||||
edge_top: super::SectionEdge::Stop,
|
edge_bot,
|
||||||
edge_bot: super::SectionEdge::Stop,
|
|
||||||
},
|
},
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
@ -146,8 +141,27 @@ pub(crate) mod syntax {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A handle for an animation section inside a sprite
|
||||||
|
#[derive(Debug, Copy, Clone)]
|
||||||
|
pub enum AnimSectionHandle {
|
||||||
|
/// The hidden section
|
||||||
|
Hidden,
|
||||||
|
|
||||||
|
/// An index into this sprite's section array
|
||||||
|
Idx(usize),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AnimSectionHandle {
|
||||||
|
fn get_idx(&self) -> Option<usize> {
|
||||||
|
match self {
|
||||||
|
Self::Hidden => None,
|
||||||
|
Self::Idx(idx) => Some(*idx),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// An edge between two animation sections
|
/// An edge between two animation sections
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub enum SectionEdge {
|
pub enum SectionEdge {
|
||||||
/// Stop at the last frame of this section
|
/// Stop at the last frame of this section
|
||||||
Stop,
|
Stop,
|
||||||
|
@ -155,7 +169,7 @@ pub enum SectionEdge {
|
||||||
/// Play the given section from the bottm
|
/// Play the given section from the bottm
|
||||||
Bot {
|
Bot {
|
||||||
/// The section to play
|
/// The section to play
|
||||||
section: Arc<SpriteSection>,
|
section: AnimSectionHandle,
|
||||||
|
|
||||||
/// The length of this edge, in seconds
|
/// The length of this edge, in seconds
|
||||||
duration: f32,
|
duration: f32,
|
||||||
|
@ -164,7 +178,7 @@ pub enum SectionEdge {
|
||||||
/// Play the given section from the top
|
/// Play the given section from the top
|
||||||
Top {
|
Top {
|
||||||
/// The section to play
|
/// The section to play
|
||||||
section: Arc<SpriteSection>,
|
section: AnimSectionHandle,
|
||||||
|
|
||||||
/// The length of this edge, in seconds
|
/// The length of this edge, in seconds
|
||||||
duration: f32,
|
duration: f32,
|
||||||
|
@ -184,18 +198,18 @@ pub enum SectionEdge {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Where to start an animation
|
/// Where to start an animation
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub enum StartEdge {
|
pub enum StartEdge {
|
||||||
/// Play the given section from the bottm
|
/// Play the given section from the bottm
|
||||||
Bot {
|
Bot {
|
||||||
/// The section to play
|
/// The section to play
|
||||||
section: Arc<SpriteSection>,
|
section: AnimSectionHandle,
|
||||||
},
|
},
|
||||||
|
|
||||||
/// Play the given section from the top
|
/// Play the given section from the top
|
||||||
Top {
|
Top {
|
||||||
/// The section to play
|
/// The section to play
|
||||||
section: Arc<SpriteSection>,
|
section: AnimSectionHandle,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -217,50 +231,80 @@ impl Into<SectionEdge> for StartEdge {
|
||||||
/// Represents a sprite that may be used in the game.
|
/// Represents a sprite that may be used in the game.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Sprite {
|
pub struct Sprite {
|
||||||
/// This object's index
|
/// The name of this sprite
|
||||||
pub index: ContentIndex,
|
pub name: String,
|
||||||
|
|
||||||
|
/// This sprite's handle
|
||||||
|
pub handle: SpriteHandle,
|
||||||
|
|
||||||
/// Where this sprite starts playing
|
/// Where this sprite starts playing
|
||||||
pub start_at: StartEdge,
|
pub start_at: StartEdge,
|
||||||
|
|
||||||
/// This sprite's animation sections
|
/// This sprite's animation sections
|
||||||
pub sections: HashMap<ContentIndex, Arc<SpriteSection>>,
|
sections: Vec<SpriteSection>,
|
||||||
|
|
||||||
|
/// Allows us to get sprite sections by name
|
||||||
|
sections_by_name: HashMap<String, AnimSectionHandle>,
|
||||||
|
|
||||||
/// Aspect ratio of this sprite (width / height)
|
/// Aspect ratio of this sprite (width / height)
|
||||||
pub aspect: f32,
|
pub aspect: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
/// The universal "hidden" sprite section, available in all sprites.
|
static ref HIDDEN_SECTION: SpriteSection = SpriteSection {
|
||||||
/// A SpriteAutomaton in this section will not be drawn.
|
|
||||||
pub static ref HIDDEN_SPRITE_SECTION: Arc<SpriteSection> = Arc::new(SpriteSection {
|
|
||||||
frames: vec![0],
|
frames: vec![0],
|
||||||
frame_duration: 0.0,
|
frame_duration: 0.0,
|
||||||
edge_bot: SectionEdge::Stop,
|
edge_bot: SectionEdge::Stop,
|
||||||
edge_top: SectionEdge::Stop,
|
edge_top: SectionEdge::Stop,
|
||||||
});
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Sprite {
|
impl Sprite {
|
||||||
|
/// Get an animation section from a handle
|
||||||
|
pub fn get_section(&self, section: AnimSectionHandle) -> &SpriteSection {
|
||||||
|
match section {
|
||||||
|
AnimSectionHandle::Hidden => &HIDDEN_SECTION,
|
||||||
|
AnimSectionHandle::Idx(idx) => &self.sections[idx],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get an animation section by name.
|
||||||
|
/// Returns None for invalid names
|
||||||
|
pub fn get_section_by_name(&self, name: &str) -> Option<&SpriteSection> {
|
||||||
|
match self.sections_by_name.get(name) {
|
||||||
|
None => return None,
|
||||||
|
Some(h) => Some(self.get_section(*h)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get an animation section's handle by name.
|
||||||
|
/// Returns None for invalid names
|
||||||
|
pub fn get_section_handle_by_name(&self, name: &str) -> Option<AnimSectionHandle> {
|
||||||
|
match self.sections_by_name.get(name) {
|
||||||
|
None => return None,
|
||||||
|
Some(h) => Some(*h),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Get the index of the texture of this sprite's first frame
|
/// Get the index of the texture of this sprite's first frame
|
||||||
pub fn get_first_frame(&self) -> u32 {
|
pub fn get_first_frame(&self) -> u32 {
|
||||||
match &self.start_at {
|
match self.start_at {
|
||||||
StartEdge::Bot { section } => *section.frames.last().unwrap(),
|
StartEdge::Bot { section } => *self.get_section(section).frames.last().unwrap(),
|
||||||
StartEdge::Top { section } => *section.frames.first().unwrap(),
|
StartEdge::Top { section } => *self.get_section(section).frames.first().unwrap(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get this sprite's starting section
|
/// Get this sprite's starting section
|
||||||
pub fn get_start_section(&self) -> Arc<SpriteSection> {
|
pub fn get_start_section(&self) -> AnimSectionHandle {
|
||||||
match &self.start_at {
|
match self.start_at {
|
||||||
StartEdge::Bot { section } => section.clone(),
|
StartEdge::Bot { section } => section,
|
||||||
StartEdge::Top { section } => section.clone(),
|
StartEdge::Top { section } => section,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Iterate this sprite's sections
|
/// Iterate this sprite's sections
|
||||||
pub fn iter_sections(&self) -> impl Iterator<Item = &Arc<SpriteSection>> {
|
pub fn iter_sections(&self) -> impl Iterator<Item = &SpriteSection> {
|
||||||
self.sections.values()
|
self.sections.iter()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -283,73 +327,66 @@ pub struct SpriteSection {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Resolve an edge specification string as a StartEdge
|
/// Resolve an edge specification string as a StartEdge
|
||||||
pub fn resolve_edge_as_start(
|
pub fn resolve_edge_as_start<F>(s: &str, get_handle: F) -> Result<super::StartEdge>
|
||||||
sections: &HashMap<ContentIndex, Arc<SpriteSection>>,
|
where
|
||||||
edge_string: &str,
|
F: Fn(&str) -> Option<AnimSectionHandle>,
|
||||||
) -> Result<super::StartEdge> {
|
{
|
||||||
let e = resolve_edge_as_edge(sections, edge_string, 0.0)
|
let e = resolve_edge_as_edge(s, 0.0, get_handle)
|
||||||
.with_context(|| format!("while resolving start edge"))?;
|
.with_context(|| format!("while resolving start edge"))?;
|
||||||
match e {
|
match e {
|
||||||
super::SectionEdge::Bot { section, .. } => Ok(super::StartEdge::Bot { section }),
|
super::SectionEdge::Bot { section, .. } => Ok(super::StartEdge::Bot { section }),
|
||||||
super::SectionEdge::Top { section, .. } => Ok(super::StartEdge::Top { section }),
|
super::SectionEdge::Top { section, .. } => Ok(super::StartEdge::Top { section }),
|
||||||
_ => {
|
_ => {
|
||||||
bail!("bad section start specification `{}`", edge_string);
|
bail!("bad section start specification `{}`", s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Resolve an edge specifiation string as a SectionEdge
|
/// Resolve an edge specifiation string as a SectionEdge
|
||||||
pub fn resolve_edge_as_edge(
|
pub fn resolve_edge_as_edge<F>(s: &str, duration: f32, get_handle: F) -> Result<super::SectionEdge>
|
||||||
sections: &HashMap<ContentIndex, Arc<SpriteSection>>,
|
where
|
||||||
edge_string: &str,
|
F: Fn(&str) -> Option<AnimSectionHandle>,
|
||||||
duration: f32,
|
{
|
||||||
) -> Result<super::SectionEdge> {
|
if s == "hidden" {
|
||||||
if edge_string == "hidden" {
|
|
||||||
return Ok(super::SectionEdge::Top {
|
return Ok(super::SectionEdge::Top {
|
||||||
section: crate::HIDDEN_SPRITE_SECTION.clone(),
|
section: crate::AnimSectionHandle::Hidden,
|
||||||
duration,
|
duration,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if edge_string == "stop" {
|
if s == "stop" {
|
||||||
return Ok(super::SectionEdge::Stop);
|
return Ok(super::SectionEdge::Stop);
|
||||||
}
|
}
|
||||||
|
|
||||||
if edge_string == "reverse" {
|
if s == "reverse" {
|
||||||
return Ok(super::SectionEdge::Reverse { duration });
|
return Ok(super::SectionEdge::Reverse { duration });
|
||||||
}
|
}
|
||||||
|
|
||||||
if edge_string == "repeat" {
|
if s == "repeat" {
|
||||||
return Ok(super::SectionEdge::Repeat { duration });
|
return Ok(super::SectionEdge::Repeat { duration });
|
||||||
}
|
}
|
||||||
|
|
||||||
let (section_name, start_point) = match edge_string.split_once(":") {
|
let (s, p) = match s.split_once(":") {
|
||||||
Some(x) => x,
|
Some(x) => x,
|
||||||
None => {
|
None => {
|
||||||
bail!("bad section edge specification `{}`", edge_string);
|
bail!("bad section edge specification `{}`", s);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let section = match sections.get(&ContentIndex::new(section_name)) {
|
let section = match get_handle(s) {
|
||||||
Some(s) => s,
|
Some(s) => s,
|
||||||
None => {
|
None => {
|
||||||
return Err(anyhow!("bad section edge specification `{}`", section_name))
|
return Err(anyhow!("bad section edge specification `{}`", s))
|
||||||
.with_context(|| format!("section `{}` doesn't exist", section_name));
|
.with_context(|| format!("section `{}` doesn't exist", s));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
match start_point {
|
match p {
|
||||||
"top" => Ok(super::SectionEdge::Top {
|
"top" => Ok(super::SectionEdge::Top { section, duration }),
|
||||||
section: section.clone(),
|
"bot" => Ok(super::SectionEdge::Bot { section, duration }),
|
||||||
duration,
|
|
||||||
}),
|
|
||||||
"bot" => Ok(super::SectionEdge::Bot {
|
|
||||||
section: section.clone(),
|
|
||||||
duration,
|
|
||||||
}),
|
|
||||||
_ => {
|
_ => {
|
||||||
return Err(anyhow!("bad section edge specification `{}`", section_name))
|
return Err(anyhow!("bad section edge specification `{}`", s))
|
||||||
.with_context(|| format!("invalid target `{}`", start_point));
|
.with_context(|| format!("invalid target `{}`", p));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -382,60 +419,93 @@ impl crate::Build for Sprite {
|
||||||
let img = &ct.sprite_atlas.get_by_idx(idx);
|
let img = &ct.sprite_atlas.get_by_idx(idx);
|
||||||
let aspect = img.w / img.h;
|
let aspect = img.w / img.h;
|
||||||
|
|
||||||
let section = Arc::new(SpriteSection {
|
let h = SpriteHandle {
|
||||||
frames: vec![img.idx.into()],
|
index: ct.sprites.len(),
|
||||||
// We implement unanimated sprites with a very fast framerate
|
};
|
||||||
// and STOP endpoints.
|
|
||||||
frame_duration: 0.01,
|
|
||||||
edge_top: SectionEdge::Stop,
|
|
||||||
edge_bot: SectionEdge::Stop,
|
|
||||||
});
|
|
||||||
|
|
||||||
let sprite = Arc::new(Self {
|
ct.sprite_index.insert(sprite_name.clone(), h);
|
||||||
index: ContentIndex::new(&sprite_name),
|
|
||||||
|
ct.sprites.push(Self {
|
||||||
|
name: sprite_name,
|
||||||
start_at: StartEdge::Top {
|
start_at: StartEdge::Top {
|
||||||
section: section.clone(),
|
section: AnimSectionHandle::Idx(0),
|
||||||
},
|
|
||||||
sections: {
|
|
||||||
let mut h = HashMap::new();
|
|
||||||
h.insert(ContentIndex::new("anim"), section);
|
|
||||||
h
|
|
||||||
},
|
},
|
||||||
|
sections: vec![SpriteSection {
|
||||||
|
frames: vec![img.idx.into()],
|
||||||
|
// We implement unanimated sprites with a very fast framerate
|
||||||
|
// and STOP endpoints.
|
||||||
|
frame_duration: 0.01,
|
||||||
|
edge_top: SectionEdge::Stop,
|
||||||
|
edge_bot: SectionEdge::Stop,
|
||||||
|
}],
|
||||||
|
sections_by_name: HashMap::new(),
|
||||||
|
handle: h,
|
||||||
aspect,
|
aspect,
|
||||||
});
|
});
|
||||||
ct.sprites.insert(sprite.index.clone(), sprite);
|
|
||||||
}
|
}
|
||||||
syntax::Sprite::OneSection(s) => {
|
syntax::Sprite::OneSection(s) => {
|
||||||
|
let sprite_handle = SpriteHandle {
|
||||||
|
index: ct.sprites.len(),
|
||||||
|
};
|
||||||
|
ct.sprite_index.insert(sprite_name.clone(), sprite_handle);
|
||||||
|
|
||||||
let (dim, section) = s
|
let (dim, section) = s
|
||||||
.add_to(ct)
|
.add_to(ct, |s| {
|
||||||
|
if s == "anim" {
|
||||||
|
Some(AnimSectionHandle::Idx(0))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
.with_context(|| format!("while parsing sprite `{}`", sprite_name))?;
|
.with_context(|| format!("while parsing sprite `{}`", sprite_name))?;
|
||||||
let aspect = dim.0 as f32 / dim.1 as f32;
|
let aspect = dim.0 as f32 / dim.1 as f32;
|
||||||
|
|
||||||
let section = Arc::new(section);
|
let mut sections = Vec::new();
|
||||||
|
sections.push(section);
|
||||||
|
|
||||||
let sprite = Arc::new(Self {
|
ct.sprites.push(Self {
|
||||||
index: ContentIndex::new(&sprite_name),
|
name: sprite_name,
|
||||||
|
sections,
|
||||||
start_at: StartEdge::Top {
|
start_at: StartEdge::Top {
|
||||||
section: section.clone(),
|
section: AnimSectionHandle::Idx(0),
|
||||||
},
|
},
|
||||||
sections: {
|
sections_by_name: {
|
||||||
let mut h = HashMap::new();
|
let mut h = HashMap::new();
|
||||||
h.insert(ContentIndex::new("anim"), section.clone());
|
h.insert("anim".to_string(), AnimSectionHandle::Idx(0));
|
||||||
h
|
h
|
||||||
},
|
},
|
||||||
|
handle: sprite_handle,
|
||||||
aspect,
|
aspect,
|
||||||
});
|
});
|
||||||
ct.sprites.insert(sprite.index.clone(), sprite);
|
|
||||||
}
|
}
|
||||||
syntax::Sprite::Complete(s) => {
|
syntax::Sprite::Complete(s) => {
|
||||||
let mut sections = HashMap::new();
|
let mut section_names = HashMap::new();
|
||||||
|
for (name, _) in &s.section {
|
||||||
|
section_names
|
||||||
|
.insert(name.to_owned(), AnimSectionHandle::Idx(section_names.len()));
|
||||||
|
}
|
||||||
|
|
||||||
|
let sprite_handle = SpriteHandle {
|
||||||
|
index: ct.sprites.len(),
|
||||||
|
};
|
||||||
|
ct.sprite_index.insert(sprite_name.clone(), sprite_handle);
|
||||||
|
|
||||||
|
let start_at =
|
||||||
|
resolve_edge_as_start(&s.start_at.val, |x| section_names.get(x).copied())
|
||||||
|
.with_context(|| format!("while loading sprite `{}`", sprite_name))?;
|
||||||
|
|
||||||
|
let mut sections = Vec::with_capacity(section_names.len());
|
||||||
let mut dim = None;
|
let mut dim = None;
|
||||||
|
|
||||||
for (name, sec) in &s.section {
|
// Make sure we add sections in order
|
||||||
let (d, s) = sec
|
let mut names = section_names.iter().collect::<Vec<_>>();
|
||||||
.add_to(ct)
|
names.sort_by(|a, b| (a.1).get_idx().unwrap().cmp(&(b.1).get_idx().unwrap()));
|
||||||
.with_context(|| format!("while parsing section `{}`", name))
|
|
||||||
|
for (k, _) in names {
|
||||||
|
let v = s.section.get(k).unwrap();
|
||||||
|
let (d, s) = v
|
||||||
|
.add_to(ct, |x| section_names.get(x).copied())
|
||||||
|
.with_context(|| format!("while parsing section `{}`", k))
|
||||||
.with_context(|| format!("while parsing sprite `{}`", sprite_name))?;
|
.with_context(|| format!("while parsing sprite `{}`", sprite_name))?;
|
||||||
|
|
||||||
// Make sure all dimensions are the same
|
// Make sure all dimensions are the same
|
||||||
|
@ -445,35 +515,23 @@ impl crate::Build for Sprite {
|
||||||
bail!(
|
bail!(
|
||||||
"could not load sprite `{}`, image sizes in section `{}` are different",
|
"could not load sprite `{}`, image sizes in section `{}` are different",
|
||||||
sprite_name,
|
sprite_name,
|
||||||
name
|
k
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
sections.insert(name.clone(), Arc::new(s));
|
sections.push(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
// We need to clone this here, thanks to self-reference.
|
|
||||||
let tmp_sections = sections.clone();
|
|
||||||
|
|
||||||
for (name, sec) in &s.section {
|
|
||||||
let parsed_sec = sections.get_mut(name).unwrap();
|
|
||||||
let parsed_sec = Arc::make_mut(parsed_sec);
|
|
||||||
sec.resolve_edges(&tmp_sections, parsed_sec)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
let dim = dim.unwrap();
|
let dim = dim.unwrap();
|
||||||
let aspect = dim.0 as f32 / dim.1 as f32;
|
let aspect = dim.0 as f32 / dim.1 as f32;
|
||||||
|
|
||||||
let start_at = resolve_edge_as_start(§ions, &s.start_at.val)
|
ct.sprites.push(Self {
|
||||||
.with_context(|| format!("while loading sprite `{}`", sprite_name))?;
|
name: sprite_name,
|
||||||
|
|
||||||
let sprite = Arc::new(Self {
|
|
||||||
index: ContentIndex::new(&sprite_name),
|
|
||||||
start_at,
|
|
||||||
sections,
|
sections,
|
||||||
|
start_at,
|
||||||
|
handle: sprite_handle,
|
||||||
|
sections_by_name: section_names,
|
||||||
aspect,
|
aspect,
|
||||||
});
|
});
|
||||||
ct.sprites.insert(sprite.index.clone(), sprite);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,43 +1,37 @@
|
||||||
use anyhow::{bail, Context, Result};
|
use anyhow::{anyhow, bail, Context, Result};
|
||||||
use galactica_util::to_radians;
|
use galactica_util::to_radians;
|
||||||
use nalgebra::{Point2, Point3};
|
use nalgebra::{Point2, Point3};
|
||||||
use std::{
|
use std::collections::{HashMap, HashSet};
|
||||||
collections::{HashMap, HashSet},
|
|
||||||
sync::Arc,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::{util::Polar, Content, ContentBuildContext, ContentIndex, Outfit, Sprite};
|
use crate::{
|
||||||
|
handle::SpriteHandle, util::Polar, Content, ContentBuildContext, SystemHandle,
|
||||||
|
SystemObjectHandle,
|
||||||
|
};
|
||||||
|
|
||||||
pub(crate) mod syntax {
|
pub(crate) mod syntax {
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::ContentIndex;
|
|
||||||
// Raw serde syntax structs.
|
// Raw serde syntax structs.
|
||||||
// These are never seen by code outside this crate.
|
// These are never seen by code outside this crate.
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct System {
|
pub struct System {
|
||||||
pub name: String,
|
pub object: HashMap<String, Object>,
|
||||||
pub object: HashMap<ContentIndex, Object>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct Object {
|
pub struct Object {
|
||||||
pub sprite: ContentIndex,
|
pub sprite: String,
|
||||||
pub position: Position,
|
pub position: Position,
|
||||||
|
|
||||||
pub size: f32,
|
pub size: f32,
|
||||||
|
|
||||||
pub radius: Option<f32>,
|
pub radius: Option<f32>,
|
||||||
pub angle: Option<f32>,
|
pub angle: Option<f32>,
|
||||||
pub landable: Option<Landable>,
|
pub landable: Option<bool>,
|
||||||
}
|
pub name: Option<String>,
|
||||||
|
pub desc: Option<String>,
|
||||||
#[derive(Debug, Deserialize)]
|
pub image: Option<String>,
|
||||||
pub struct Landable {
|
|
||||||
pub name: String,
|
|
||||||
pub desc: String,
|
|
||||||
pub image: ContentIndex,
|
|
||||||
pub outfitter: Vec<ContentIndex>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
|
@ -58,14 +52,14 @@ pub(crate) mod syntax {
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
#[serde(untagged)]
|
#[serde(untagged)]
|
||||||
pub enum CoordinatesTwo {
|
pub enum CoordinatesTwo {
|
||||||
Label(ContentIndex),
|
Label(String),
|
||||||
Coords([f32; 2]),
|
Coords([f32; 2]),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToString for CoordinatesTwo {
|
impl ToString for CoordinatesTwo {
|
||||||
fn to_string(&self) -> String {
|
fn to_string(&self) -> String {
|
||||||
match self {
|
match self {
|
||||||
Self::Label(s) => s.to_string(),
|
Self::Label(s) => s.to_owned(),
|
||||||
Self::Coords(v) => format!("{:?}", v),
|
Self::Coords(v) => format!("{:?}", v),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -74,14 +68,14 @@ pub(crate) mod syntax {
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
#[serde(untagged)]
|
#[serde(untagged)]
|
||||||
pub enum CoordinatesThree {
|
pub enum CoordinatesThree {
|
||||||
Label(ContentIndex),
|
Label(String),
|
||||||
Coords([f32; 3]),
|
Coords([f32; 3]),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToString for CoordinatesThree {
|
impl ToString for CoordinatesThree {
|
||||||
fn to_string(&self) -> String {
|
fn to_string(&self) -> String {
|
||||||
match self {
|
match self {
|
||||||
Self::Label(s) => s.to_string(),
|
Self::Label(s) => s.to_owned(),
|
||||||
Self::Coords(v) => format!("{:?}", v),
|
Self::Coords(v) => format!("{:?}", v),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -94,14 +88,14 @@ pub(crate) mod syntax {
|
||||||
/// Represents a star system
|
/// Represents a star system
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct System {
|
pub struct System {
|
||||||
/// This object's name
|
/// This star system's name
|
||||||
pub display_name: String,
|
pub name: String,
|
||||||
|
|
||||||
/// This object's index
|
/// This star system's handle
|
||||||
pub index: ContentIndex,
|
pub handle: SystemHandle,
|
||||||
|
|
||||||
/// Objects in this system
|
/// Objects in this system
|
||||||
pub objects: HashMap<ContentIndex, Arc<SystemObject>>,
|
pub objects: Vec<SystemObject>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Represents an orbiting body in a star system
|
/// Represents an orbiting body in a star system
|
||||||
|
@ -110,11 +104,11 @@ pub struct System {
|
||||||
/// System objects to not interact with the physics engine.
|
/// System objects to not interact with the physics engine.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct SystemObject {
|
pub struct SystemObject {
|
||||||
/// This object's index
|
|
||||||
pub index: ContentIndex,
|
|
||||||
|
|
||||||
/// This object's sprite
|
/// This object's sprite
|
||||||
pub sprite: Arc<Sprite>,
|
pub sprite: SpriteHandle,
|
||||||
|
|
||||||
|
/// This object's handle
|
||||||
|
pub handle: SystemObjectHandle,
|
||||||
|
|
||||||
/// This object's size.
|
/// This object's size.
|
||||||
/// Measured as height in game units.
|
/// Measured as height in game units.
|
||||||
|
@ -130,29 +124,26 @@ pub struct SystemObject {
|
||||||
pub angle: f32,
|
pub angle: f32,
|
||||||
|
|
||||||
/// If true, ships may land on this object
|
/// If true, ships may land on this object
|
||||||
pub landable: Option<LandableSystemObject>,
|
pub landable: bool,
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
/// The pretty display name of this object
|
||||||
pub struct LandableSystemObject {
|
pub name: Option<String>,
|
||||||
/// This object's name
|
|
||||||
pub display_name: String,
|
|
||||||
|
|
||||||
/// The description of this object
|
/// The system-unique label of this object
|
||||||
pub desc: String,
|
pub label: String,
|
||||||
|
|
||||||
/// This object's image
|
/// The description of this object (shown on landed ui)
|
||||||
pub image: Arc<Sprite>,
|
pub desc: Option<String>,
|
||||||
|
|
||||||
/// The outfits we can buy here
|
/// This object's image (shown on landed ui)
|
||||||
pub outfitter: Vec<Arc<Outfit>>,
|
pub image: Option<SpriteHandle>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Helper function for resolve_position, never called on its own.
|
/// Helper function for resolve_position, never called on its own.
|
||||||
fn resolve_coordinates(
|
fn resolve_coordinates(
|
||||||
objects: &HashMap<ContentIndex, syntax::Object>,
|
objects: &HashMap<String, syntax::Object>,
|
||||||
cor: &syntax::CoordinatesThree,
|
cor: &syntax::CoordinatesThree,
|
||||||
mut cycle_detector: HashSet<ContentIndex>,
|
mut cycle_detector: HashSet<String>,
|
||||||
) -> Result<Point3<f32>> {
|
) -> Result<Point3<f32>> {
|
||||||
match cor {
|
match cor {
|
||||||
syntax::CoordinatesThree::Coords(c) => Ok((*c).into()),
|
syntax::CoordinatesThree::Coords(c) => Ok((*c).into()),
|
||||||
|
@ -169,7 +160,7 @@ fn resolve_coordinates(
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
cycle_detector.insert(l.clone());
|
cycle_detector.insert(l.to_owned());
|
||||||
|
|
||||||
let p = match objects.get(l) {
|
let p = match objects.get(l) {
|
||||||
Some(p) => p,
|
Some(p) => p,
|
||||||
|
@ -183,9 +174,9 @@ fn resolve_coordinates(
|
||||||
|
|
||||||
/// Given an object, resolve its position as a Point3.
|
/// Given an object, resolve its position as a Point3.
|
||||||
fn resolve_position(
|
fn resolve_position(
|
||||||
objects: &HashMap<ContentIndex, syntax::Object>,
|
objects: &HashMap<String, syntax::Object>,
|
||||||
obj: &syntax::Object,
|
obj: &syntax::Object,
|
||||||
cycle_detector: HashSet<ContentIndex>,
|
cycle_detector: HashSet<String>,
|
||||||
) -> Result<Point3<f32>> {
|
) -> Result<Point3<f32>> {
|
||||||
match &obj.position {
|
match &obj.position {
|
||||||
syntax::Position::Cartesian(c) => Ok(resolve_coordinates(objects, &c, cycle_detector)?),
|
syntax::Position::Cartesian(c) => Ok(resolve_coordinates(objects, &c, cycle_detector)?),
|
||||||
|
@ -217,78 +208,99 @@ impl crate::Build for System {
|
||||||
content: &mut Content,
|
content: &mut Content,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
for (system_name, system) in system {
|
for (system_name, system) in system {
|
||||||
let mut objects = HashMap::new();
|
let mut objects = Vec::new();
|
||||||
|
|
||||||
for (index, object) in &system.object {
|
let system_handle = SystemHandle {
|
||||||
|
index: content.systems.len(),
|
||||||
|
};
|
||||||
|
|
||||||
|
for (label, obj) in &system.object {
|
||||||
let mut cycle_detector = HashSet::new();
|
let mut cycle_detector = HashSet::new();
|
||||||
cycle_detector.insert(index.clone());
|
cycle_detector.insert(label.clone());
|
||||||
|
|
||||||
let sprite = match content.sprites.get(&object.sprite) {
|
let sprite_handle = match content.sprite_index.get(&obj.sprite) {
|
||||||
None => bail!(
|
None => bail!(
|
||||||
"In system `{}`: sprite `{}` doesn't exist",
|
"In system `{}`: sprite `{}` doesn't exist",
|
||||||
system_name,
|
system_name,
|
||||||
object.sprite
|
obj.sprite
|
||||||
),
|
),
|
||||||
Some(t) => t.clone(),
|
Some(t) => *t,
|
||||||
};
|
};
|
||||||
|
|
||||||
let landable = 'landable: {
|
let image_handle = match &obj.image {
|
||||||
if object.landable.is_none() {
|
Some(x) => match content.sprite_index.get(x) {
|
||||||
break 'landable None;
|
|
||||||
}
|
|
||||||
let l = object.landable.as_ref().unwrap();
|
|
||||||
|
|
||||||
let image = match content.sprites.get(&l.image) {
|
|
||||||
None => bail!(
|
None => bail!(
|
||||||
"In system `{}`: sprite `{}` doesn't exist",
|
"In system `{}`: sprite `{}` doesn't exist",
|
||||||
system_name,
|
system_name,
|
||||||
object.sprite
|
obj.sprite
|
||||||
),
|
),
|
||||||
Some(t) => t.clone(),
|
Some(t) => Some(*t),
|
||||||
};
|
},
|
||||||
|
None => None,
|
||||||
let mut outfitter = Vec::new();
|
|
||||||
for o in &l.outfitter {
|
|
||||||
match content.outfits.get(&o) {
|
|
||||||
Some(x) => outfitter.push(x.clone()),
|
|
||||||
None => {
|
|
||||||
bail!("In system `{}`: outfit `{}` doesn't exist", system_name, o)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
break 'landable Some(LandableSystemObject {
|
|
||||||
image,
|
|
||||||
outfitter,
|
|
||||||
display_name: l.name.clone(),
|
|
||||||
// TODO: better linebreaks, handle double spaces
|
|
||||||
// Tabs
|
|
||||||
desc: l.desc.replace("\n", " ").replace("<br>", "\n"),
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
objects.insert(
|
if obj.landable.unwrap_or(false) {
|
||||||
index.clone(),
|
if obj.name.is_none() {
|
||||||
Arc::new(SystemObject {
|
return Err(anyhow!("if an object is landable, it must have a name"))
|
||||||
index: index.clone(),
|
.with_context(|| format!("in object labeled `{}`", label))
|
||||||
sprite,
|
.with_context(|| format!("in system `{}`", system_name));
|
||||||
pos: resolve_position(&system.object, &object, cycle_detector)
|
}
|
||||||
.with_context(|| format!("in object {:#?}", index))?,
|
|
||||||
size: object.size,
|
if obj.desc.is_none() {
|
||||||
angle: to_radians(object.angle.unwrap_or(0.0)),
|
return Err(anyhow!(
|
||||||
landable: landable,
|
"if an object is landable, it must have a description"
|
||||||
}),
|
))
|
||||||
);
|
.with_context(|| format!("in object labeled `{}`", label))
|
||||||
|
.with_context(|| format!("in system `{}`", system_name));
|
||||||
|
}
|
||||||
|
|
||||||
|
if obj.image.is_none() {
|
||||||
|
return Err(anyhow!("if an object is landable, it must have an image"))
|
||||||
|
.with_context(|| format!("in object labeled `{}`", label))
|
||||||
|
.with_context(|| format!("in system `{}`", system_name));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
objects.push(SystemObject {
|
||||||
|
label: label.clone(),
|
||||||
|
sprite: sprite_handle,
|
||||||
|
image: image_handle,
|
||||||
|
pos: resolve_position(&system.object, &obj, cycle_detector)
|
||||||
|
.with_context(|| format!("in object {:#?}", label))?,
|
||||||
|
size: obj.size,
|
||||||
|
angle: to_radians(obj.angle.unwrap_or(0.0)),
|
||||||
|
handle: SystemObjectHandle {
|
||||||
|
system_handle,
|
||||||
|
body_index: 0,
|
||||||
|
},
|
||||||
|
landable: obj.landable.unwrap_or(false),
|
||||||
|
name: obj.name.as_ref().map(|x| x.clone()),
|
||||||
|
// TODO: better linebreaks, handle double spaces
|
||||||
|
// Tabs
|
||||||
|
desc: obj
|
||||||
|
.desc
|
||||||
|
.as_ref()
|
||||||
|
.map(|x| x.replace("\n", " ").replace("<br>", "\n")),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
content.systems.insert(
|
// Sort by z-distance. This is important, since these are
|
||||||
ContentIndex::new(&system_name),
|
// rendered in this order. We need far objects to be behind
|
||||||
Arc::new(Self {
|
// near objects!
|
||||||
index: ContentIndex::new(&system_name),
|
objects.sort_by(|a, b| b.pos.z.total_cmp(&a.pos.z));
|
||||||
display_name: system.name,
|
|
||||||
objects,
|
// Update object handles
|
||||||
}),
|
let mut i = 0;
|
||||||
);
|
for o in &mut objects {
|
||||||
|
o.handle.body_index = i;
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
content.systems.push(Self {
|
||||||
|
handle: system_handle,
|
||||||
|
name: system_name,
|
||||||
|
objects,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
use crate::{SectionEdge, Sprite, SpriteSection, StartEdge};
|
use crate::{AnimSectionHandle, Content, SectionEdge, SpriteHandle, StartEdge};
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
/// A single frame's state
|
/// A single frame's state
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
@ -44,11 +43,11 @@ enum AnimDirection {
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct SpriteAutomaton {
|
pub struct SpriteAutomaton {
|
||||||
/// The sprite we're animating
|
/// The sprite we're animating
|
||||||
sprite: Arc<Sprite>,
|
sprite: SpriteHandle,
|
||||||
|
|
||||||
/// Which animation section we're on
|
/// Which animation section we're on
|
||||||
/// This MUST be a section from this Automaton's sprite
|
/// This MUST be a section from this Automaton's sprite
|
||||||
current_section: Arc<SpriteSection>,
|
current_section: AnimSectionHandle,
|
||||||
|
|
||||||
/// Which frame we're on
|
/// Which frame we're on
|
||||||
current_frame: usize,
|
current_frame: usize,
|
||||||
|
@ -76,35 +75,39 @@ pub struct SpriteAutomaton {
|
||||||
|
|
||||||
impl SpriteAutomaton {
|
impl SpriteAutomaton {
|
||||||
/// Create a new AnimAutomaton
|
/// Create a new AnimAutomaton
|
||||||
pub fn new(sprite: Arc<Sprite>) -> Self {
|
pub fn new(ct: &Content, sprite_handle: SpriteHandle) -> Self {
|
||||||
let (current_section, texture, current_direction) = {
|
let sprite = ct.get_sprite(sprite_handle);
|
||||||
match &sprite.start_at {
|
|
||||||
StartEdge::Top { section } => (
|
let (current_section, texture, current_direction) = match sprite.start_at {
|
||||||
section,
|
StartEdge::Top { section } => (
|
||||||
*section.frames.first().unwrap(),
|
section,
|
||||||
AnimDirection::Down,
|
*sprite.get_section(section).frames.first().unwrap(),
|
||||||
),
|
AnimDirection::Down,
|
||||||
StartEdge::Bot { section } => {
|
),
|
||||||
(section, *section.frames.last().unwrap(), AnimDirection::Up)
|
StartEdge::Bot { section } => (
|
||||||
}
|
section,
|
||||||
}
|
*sprite.get_section(section).frames.last().unwrap(),
|
||||||
|
AnimDirection::Up,
|
||||||
|
),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let sec = sprite.get_section(current_section);
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
sprite: sprite.clone(),
|
sprite: sprite.handle,
|
||||||
current_frame: 0,
|
current_frame: 0,
|
||||||
|
|
||||||
current_edge_progress: match current_direction {
|
current_edge_progress: match current_direction {
|
||||||
AnimDirection::Down => 0.0,
|
AnimDirection::Down => 0.0,
|
||||||
AnimDirection::Up => current_section.frame_duration,
|
AnimDirection::Up => sec.frame_duration,
|
||||||
AnimDirection::Stop => unreachable!("how'd you get here?"),
|
AnimDirection::Stop => unreachable!("how'd you get here?"),
|
||||||
},
|
},
|
||||||
|
|
||||||
current_edge_duration: current_section.frame_duration,
|
current_edge_duration: sec.frame_duration,
|
||||||
next_edge_override: None,
|
next_edge_override: None,
|
||||||
|
|
||||||
current_direction,
|
current_direction,
|
||||||
current_section: current_section.clone(),
|
current_section,
|
||||||
last_texture: texture,
|
last_texture: texture,
|
||||||
next_texture: texture,
|
next_texture: texture,
|
||||||
}
|
}
|
||||||
|
@ -116,11 +119,14 @@ impl SpriteAutomaton {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Force a transition to the given section right now
|
/// Force a transition to the given section right now
|
||||||
pub fn jump_to(&mut self, start: &SectionEdge) {
|
pub fn jump_to(&mut self, ct: &Content, start: SectionEdge) {
|
||||||
self.take_edge(start);
|
self.take_edge(ct, start);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn take_edge(&mut self, e: &SectionEdge) {
|
fn take_edge(&mut self, ct: &Content, e: SectionEdge) {
|
||||||
|
let sprite = ct.get_sprite(self.sprite);
|
||||||
|
let current_section = sprite.get_section(self.current_section);
|
||||||
|
|
||||||
let last = match self.current_direction {
|
let last = match self.current_direction {
|
||||||
AnimDirection::Stop => self.next_texture,
|
AnimDirection::Stop => self.next_texture,
|
||||||
AnimDirection::Down => self.next_texture,
|
AnimDirection::Down => self.next_texture,
|
||||||
|
@ -135,7 +141,7 @@ impl SpriteAutomaton {
|
||||||
self.current_frame = 0;
|
self.current_frame = 0;
|
||||||
}
|
}
|
||||||
AnimDirection::Down => {
|
AnimDirection::Down => {
|
||||||
self.current_frame = self.current_section.frames.len() - 1;
|
self.current_frame = current_section.frames.len() - 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -143,28 +149,29 @@ impl SpriteAutomaton {
|
||||||
self.current_direction = AnimDirection::Stop;
|
self.current_direction = AnimDirection::Stop;
|
||||||
}
|
}
|
||||||
SectionEdge::Top { section, duration } => {
|
SectionEdge::Top { section, duration } => {
|
||||||
self.current_section = section.clone();
|
self.current_section = section;
|
||||||
self.current_edge_duration = *duration;
|
self.current_edge_duration = duration;
|
||||||
self.current_frame = 0;
|
self.current_frame = 0;
|
||||||
self.current_direction = AnimDirection::Down;
|
self.current_direction = AnimDirection::Down;
|
||||||
}
|
}
|
||||||
SectionEdge::Bot { section, duration } => {
|
SectionEdge::Bot { section, duration } => {
|
||||||
self.current_section = section.clone();
|
let s = sprite.get_section(section);
|
||||||
self.current_frame = section.frames.len() - 1;
|
self.current_section = section;
|
||||||
self.current_edge_duration = *duration;
|
self.current_frame = s.frames.len() - 1;
|
||||||
|
self.current_edge_duration = duration;
|
||||||
self.current_direction = AnimDirection::Up;
|
self.current_direction = AnimDirection::Up;
|
||||||
}
|
}
|
||||||
SectionEdge::Repeat { duration } => {
|
SectionEdge::Repeat { duration } => {
|
||||||
match self.current_direction {
|
match self.current_direction {
|
||||||
AnimDirection::Stop => {}
|
AnimDirection::Stop => {}
|
||||||
AnimDirection::Up => {
|
AnimDirection::Up => {
|
||||||
self.current_frame = self.current_section.frames.len() - 1;
|
self.current_frame = current_section.frames.len() - 1;
|
||||||
}
|
}
|
||||||
AnimDirection::Down => {
|
AnimDirection::Down => {
|
||||||
self.current_frame = 0;
|
self.current_frame = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.current_edge_duration = *duration;
|
self.current_edge_duration = duration;
|
||||||
}
|
}
|
||||||
SectionEdge::Reverse { duration } => {
|
SectionEdge::Reverse { duration } => {
|
||||||
match self.current_direction {
|
match self.current_direction {
|
||||||
|
@ -173,7 +180,7 @@ impl SpriteAutomaton {
|
||||||
// Jump to SECOND frame, since we've already shown the
|
// Jump to SECOND frame, since we've already shown the
|
||||||
// first during the fade transition
|
// first during the fade transition
|
||||||
self.current_frame = {
|
self.current_frame = {
|
||||||
if self.current_section.frames.len() == 1 {
|
if current_section.frames.len() == 1 {
|
||||||
0
|
0
|
||||||
} else {
|
} else {
|
||||||
1
|
1
|
||||||
|
@ -183,39 +190,45 @@ impl SpriteAutomaton {
|
||||||
}
|
}
|
||||||
AnimDirection::Down => {
|
AnimDirection::Down => {
|
||||||
self.current_frame = {
|
self.current_frame = {
|
||||||
if self.current_section.frames.len() == 1 {
|
if current_section.frames.len() == 1 {
|
||||||
0
|
0
|
||||||
} else {
|
} else {
|
||||||
self.current_section.frames.len() - 2
|
current_section.frames.len() - 2
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
self.current_direction = AnimDirection::Up;
|
self.current_direction = AnimDirection::Up;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.current_edge_duration = *duration;
|
self.current_edge_duration = duration;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
match self.current_direction {
|
match self.current_direction {
|
||||||
AnimDirection::Stop => {
|
AnimDirection::Stop => {
|
||||||
self.next_texture = self.current_section.frames[self.current_frame];
|
let current_section = sprite.get_section(self.current_section);
|
||||||
self.last_texture = self.current_section.frames[self.current_frame];
|
self.next_texture = current_section.frames[self.current_frame];
|
||||||
|
self.last_texture = current_section.frames[self.current_frame];
|
||||||
}
|
}
|
||||||
AnimDirection::Down => {
|
AnimDirection::Down => {
|
||||||
|
let current_section = sprite.get_section(self.current_section);
|
||||||
self.last_texture = last;
|
self.last_texture = last;
|
||||||
self.next_texture = self.current_section.frames[self.current_frame];
|
self.next_texture = current_section.frames[self.current_frame];
|
||||||
self.current_edge_progress = 0.0;
|
self.current_edge_progress = 0.0;
|
||||||
}
|
}
|
||||||
AnimDirection::Up => {
|
AnimDirection::Up => {
|
||||||
|
let current_section = sprite.get_section(self.current_section);
|
||||||
self.next_texture = last;
|
self.next_texture = last;
|
||||||
self.last_texture = self.current_section.frames[self.current_frame];
|
self.last_texture = current_section.frames[self.current_frame];
|
||||||
self.current_edge_progress = self.current_edge_duration;
|
self.current_edge_progress = self.current_edge_duration;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Step this animation by `t` seconds
|
/// Step this animation by `t` seconds
|
||||||
pub fn step(&mut self, t: f32) {
|
pub fn step(&mut self, ct: &Content, t: f32) {
|
||||||
|
let sprite = ct.get_sprite(self.sprite);
|
||||||
|
let current_section = sprite.get_section(self.current_section);
|
||||||
|
|
||||||
// Current_fade and current_frame keep track of where we are in the current section.
|
// Current_fade and current_frame keep track of where we are in the current section.
|
||||||
// current_frame indexes this section frames. When it exceeds the number of frames
|
// current_frame indexes this section frames. When it exceeds the number of frames
|
||||||
// or falls below zero (when moving in reverse), we switch to the next section.
|
// or falls below zero (when moving in reverse), we switch to the next section.
|
||||||
|
@ -227,16 +240,15 @@ impl SpriteAutomaton {
|
||||||
// Note that frame_duration may be zero!
|
// Note that frame_duration may be zero!
|
||||||
// This is only possible in the hidden texture, since
|
// This is only possible in the hidden texture, since
|
||||||
// user-provided sections are always checked to be positive.
|
// user-provided sections are always checked to be positive.
|
||||||
assert!(self.current_section.frame_duration >= 0.0);
|
assert!(current_section.frame_duration >= 0.0);
|
||||||
|
|
||||||
match self.current_direction {
|
match self.current_direction {
|
||||||
AnimDirection::Stop => {
|
AnimDirection::Stop => {
|
||||||
// Edge case: we're stopped and got a request to transition.
|
// Edge case: we're stopped and got a request to transition.
|
||||||
// we should transition right away.
|
// we should transition right away.
|
||||||
|
|
||||||
if self.next_edge_override.is_some() {
|
if let Some(e) = self.next_edge_override.take() {
|
||||||
let e = self.next_edge_override.take().unwrap();
|
self.take_edge(ct, e);
|
||||||
self.take_edge(&e);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
@ -247,21 +259,21 @@ impl SpriteAutomaton {
|
||||||
|
|
||||||
// We're stepping foward and finished this frame
|
// We're stepping foward and finished this frame
|
||||||
if self.current_edge_progress > self.current_edge_duration {
|
if self.current_edge_progress > self.current_edge_duration {
|
||||||
if self.current_frame < self.current_section.frames.len() - 1 {
|
if self.current_frame < current_section.frames.len() - 1 {
|
||||||
self.current_frame += 1;
|
self.current_frame += 1;
|
||||||
self.last_texture = self.next_texture;
|
self.last_texture = self.next_texture;
|
||||||
self.next_texture = self.current_section.frames[self.current_frame];
|
self.next_texture = current_section.frames[self.current_frame];
|
||||||
self.current_edge_progress = 0.0;
|
self.current_edge_progress = 0.0;
|
||||||
self.current_edge_duration = self.current_section.frame_duration;
|
self.current_edge_duration = current_section.frame_duration;
|
||||||
} else {
|
} else {
|
||||||
let e = {
|
let e = {
|
||||||
if self.next_edge_override.is_some() {
|
if self.next_edge_override.is_some() {
|
||||||
self.next_edge_override.take().unwrap()
|
self.next_edge_override.take().unwrap()
|
||||||
} else {
|
} else {
|
||||||
self.current_section.edge_bot.clone()
|
current_section.edge_bot.clone()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
self.take_edge(&e);
|
self.take_edge(ct, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -274,18 +286,18 @@ impl SpriteAutomaton {
|
||||||
if self.current_frame > 0 {
|
if self.current_frame > 0 {
|
||||||
self.current_frame -= 1;
|
self.current_frame -= 1;
|
||||||
self.next_texture = self.last_texture;
|
self.next_texture = self.last_texture;
|
||||||
self.last_texture = self.current_section.frames[self.current_frame];
|
self.last_texture = current_section.frames[self.current_frame];
|
||||||
self.current_edge_progress = self.current_section.frame_duration;
|
self.current_edge_progress = current_section.frame_duration;
|
||||||
self.current_edge_duration = self.current_section.frame_duration;
|
self.current_edge_duration = current_section.frame_duration;
|
||||||
} else {
|
} else {
|
||||||
let e = {
|
let e = {
|
||||||
if self.next_edge_override.is_some() {
|
if self.next_edge_override.is_some() {
|
||||||
self.next_edge_override.take().unwrap()
|
self.next_edge_override.take().unwrap()
|
||||||
} else {
|
} else {
|
||||||
self.current_section.edge_top.clone()
|
current_section.edge_top.clone()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
self.take_edge(&e);
|
self.take_edge(ct, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -302,7 +314,7 @@ impl SpriteAutomaton {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the sprite this automaton is using
|
/// Get the sprite this automaton is using
|
||||||
pub fn get_sprite(&self) -> Arc<Sprite> {
|
pub fn get_sprite(&self) -> SpriteHandle {
|
||||||
self.sprite.clone()
|
self.sprite
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
use galactica_content::{Content, ContentIndex};
|
use galactica_content::{Content, FactionHandle, OutfitHandle, ShipHandle, SystemHandle};
|
||||||
use galactica_playeragent::PlayerAgent;
|
use galactica_playeragent::PlayerAgent;
|
||||||
use galactica_system::data::ShipPersonality;
|
use galactica_system::data::ShipPersonality;
|
||||||
use galactica_system::phys::{PhysImage, PhysSim, PhysSimShipHandle, PhysStepResources};
|
use galactica_system::phys::{PhysImage, PhysSim, PhysSimShipHandle, PhysStepResources};
|
||||||
use galactica_system::PlayerDirective;
|
|
||||||
use galactica_util::timing::Timing;
|
use galactica_util::timing::Timing;
|
||||||
use nalgebra::Point2;
|
use nalgebra::Point2;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
@ -25,101 +24,82 @@ unsafe impl<'a> Send for Game {}
|
||||||
impl<'a> Game {
|
impl<'a> Game {
|
||||||
pub fn make_player(&mut self) -> PhysSimShipHandle {
|
pub fn make_player(&mut self) -> PhysSimShipHandle {
|
||||||
let player = self.phys_sim.add_ship(
|
let player = self.phys_sim.add_ship(
|
||||||
self.ct
|
&self.ct,
|
||||||
.ships
|
ShipHandle { index: 0 },
|
||||||
.get(&ContentIndex::new("gypsum"))
|
FactionHandle { index: 0 },
|
||||||
.unwrap()
|
|
||||||
.clone(),
|
|
||||||
self.ct
|
|
||||||
.factions
|
|
||||||
.get(&ContentIndex::new("player"))
|
|
||||||
.unwrap()
|
|
||||||
.clone(),
|
|
||||||
ShipPersonality::Player,
|
ShipPersonality::Player,
|
||||||
Point2::new(0.0, 4000.0),
|
Point2::new(0.0, 4000.0),
|
||||||
);
|
);
|
||||||
|
|
||||||
let s = self.phys_sim.get_ship_mut(&player).unwrap();
|
let s = self.phys_sim.get_ship_mut(&player).unwrap();
|
||||||
s.add_outfits([
|
s.add_outfits(
|
||||||
self.ct
|
&self.ct,
|
||||||
.outfits
|
[
|
||||||
.get(&ContentIndex::new("plasma engines"))
|
OutfitHandle { index: 0 },
|
||||||
.unwrap()
|
OutfitHandle { index: 1 },
|
||||||
.clone(),
|
OutfitHandle { index: 2 },
|
||||||
self.ct
|
],
|
||||||
.outfits
|
);
|
||||||
.get(&ContentIndex::new("shield generator"))
|
|
||||||
.unwrap()
|
|
||||||
.clone(),
|
|
||||||
self.ct
|
|
||||||
.outfits
|
|
||||||
.get(&ContentIndex::new("blaster"))
|
|
||||||
.unwrap()
|
|
||||||
.clone(),
|
|
||||||
self.ct
|
|
||||||
.outfits
|
|
||||||
.get(&ContentIndex::new("blaster"))
|
|
||||||
.unwrap()
|
|
||||||
.clone(),
|
|
||||||
]);
|
|
||||||
|
|
||||||
return player;
|
return player;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(ct: Arc<Content>) -> Self {
|
pub fn new(ct: Arc<Content>) -> Self {
|
||||||
let mut phys_sim = PhysSim::new();
|
let mut phys_sim = PhysSim::new(&ct, SystemHandle { index: 0 });
|
||||||
|
|
||||||
let a = phys_sim.add_ship(
|
let a = phys_sim.add_ship(
|
||||||
ct.ships.get(&ContentIndex::new("gypsum")).unwrap().clone(),
|
&ct,
|
||||||
ct.factions
|
ShipHandle { index: 0 },
|
||||||
.get(&ContentIndex::new("enemy"))
|
FactionHandle { index: 1 },
|
||||||
.unwrap()
|
ShipPersonality::Point,
|
||||||
.clone(),
|
Point2::new(1000.0, 0.0),
|
||||||
|
);
|
||||||
|
|
||||||
|
let s = phys_sim.get_ship_mut(&a).unwrap();
|
||||||
|
s.add_outfits(
|
||||||
|
&ct,
|
||||||
|
[
|
||||||
|
OutfitHandle { index: 0 },
|
||||||
|
OutfitHandle { index: 1 },
|
||||||
|
OutfitHandle { index: 2 },
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
let a = phys_sim.add_ship(
|
||||||
|
&ct,
|
||||||
|
ShipHandle { index: 0 },
|
||||||
|
FactionHandle { index: 1 },
|
||||||
ShipPersonality::Point,
|
ShipPersonality::Point,
|
||||||
Point2::new(1000.0, 4000.0),
|
Point2::new(1000.0, 4000.0),
|
||||||
);
|
);
|
||||||
|
|
||||||
let s = phys_sim.get_ship_mut(&a).unwrap();
|
let s = phys_sim.get_ship_mut(&a).unwrap();
|
||||||
s.add_outfits([
|
s.add_outfits(
|
||||||
ct.outfits
|
&ct,
|
||||||
.get(&ContentIndex::new("plasma engines"))
|
[
|
||||||
.unwrap()
|
OutfitHandle { index: 0 },
|
||||||
.clone(),
|
OutfitHandle { index: 1 },
|
||||||
ct.outfits
|
OutfitHandle { index: 2 },
|
||||||
.get(&ContentIndex::new("shield generator"))
|
],
|
||||||
.unwrap()
|
);
|
||||||
.clone(),
|
|
||||||
ct.outfits
|
|
||||||
.get(&ContentIndex::new("blaster"))
|
|
||||||
.unwrap()
|
|
||||||
.clone(),
|
|
||||||
]);
|
|
||||||
|
|
||||||
let a = phys_sim.add_ship(
|
let a = phys_sim.add_ship(
|
||||||
ct.ships.get(&ContentIndex::new("gypsum")).unwrap().clone(),
|
&ct,
|
||||||
ct.factions
|
ShipHandle { index: 0 },
|
||||||
.get(&ContentIndex::new("player"))
|
FactionHandle { index: 0 },
|
||||||
.unwrap()
|
|
||||||
.clone(),
|
|
||||||
ShipPersonality::Dummy,
|
ShipPersonality::Dummy,
|
||||||
Point2::new(200.0, 2000.0),
|
Point2::new(200.0, 2000.0),
|
||||||
);
|
);
|
||||||
|
|
||||||
let s = phys_sim.get_ship_mut(&a).unwrap();
|
let s = phys_sim.get_ship_mut(&a).unwrap();
|
||||||
s.add_outfits([
|
s.add_outfits(
|
||||||
ct.outfits
|
&ct,
|
||||||
.get(&ContentIndex::new("plasma engines"))
|
[
|
||||||
.unwrap()
|
OutfitHandle { index: 0 },
|
||||||
.clone(),
|
OutfitHandle { index: 1 },
|
||||||
ct.outfits
|
OutfitHandle { index: 2 },
|
||||||
.get(&ContentIndex::new("shield generator"))
|
],
|
||||||
.unwrap()
|
);
|
||||||
.clone(),
|
|
||||||
ct.outfits
|
|
||||||
.get(&ContentIndex::new("blaster"))
|
|
||||||
.unwrap()
|
|
||||||
.clone(),
|
|
||||||
]);
|
|
||||||
|
|
||||||
Game {
|
Game {
|
||||||
ct,
|
ct,
|
||||||
|
@ -131,10 +111,8 @@ impl<'a> Game {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn apply_directive(&mut self, directive: PlayerDirective, player: &PlayerAgent) {
|
pub fn update_player_controls(&mut self, player: &mut PlayerAgent) {
|
||||||
match directive {
|
self.phys_sim.update_player_controls(&self.ct, player)
|
||||||
_ => self.phys_sim.apply_directive(directive, player),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn step(&mut self, phys_img: &PhysImage) {
|
pub fn step(&mut self, phys_img: &PhysImage) {
|
||||||
|
|
|
@ -2,10 +2,13 @@ mod game;
|
||||||
|
|
||||||
use anyhow::{bail, Result};
|
use anyhow::{bail, Result};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use galactica_content::Content;
|
use galactica_content::{Content, SystemHandle};
|
||||||
use galactica_playeragent::PlayerAgent;
|
use galactica_playeragent::{PlayerAgent, PlayerStatus};
|
||||||
use galactica_render::{InputEvent, RenderInput};
|
use galactica_render::RenderInput;
|
||||||
use galactica_system::phys::PhysImage;
|
use galactica_system::{
|
||||||
|
data::ShipState,
|
||||||
|
phys::{PhysImage, PhysSimShipHandle},
|
||||||
|
};
|
||||||
use galactica_util::constants::ASSET_CACHE;
|
use galactica_util::constants::ASSET_CACHE;
|
||||||
use log::LevelFilter;
|
use log::LevelFilter;
|
||||||
use log4rs::{
|
use log4rs::{
|
||||||
|
@ -14,13 +17,15 @@ use log4rs::{
|
||||||
encode::pattern::PatternEncoder,
|
encode::pattern::PatternEncoder,
|
||||||
Config,
|
Config,
|
||||||
};
|
};
|
||||||
|
use nalgebra::Vector2;
|
||||||
use std::{
|
use std::{
|
||||||
fs,
|
fs,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
|
time::Instant,
|
||||||
};
|
};
|
||||||
use winit::{
|
use winit::{
|
||||||
event::{ElementState, Event, KeyboardInput, MouseButton, MouseScrollDelta, WindowEvent},
|
event::{Event, KeyboardInput, WindowEvent},
|
||||||
event_loop::{ControlFlow, EventLoop},
|
event_loop::{ControlFlow, EventLoop},
|
||||||
window::WindowBuilder,
|
window::WindowBuilder,
|
||||||
};
|
};
|
||||||
|
@ -123,21 +128,31 @@ fn try_main() -> Result<()> {
|
||||||
let mut game = game::Game::new(content.clone());
|
let mut game = game::Game::new(content.clone());
|
||||||
let p = game.make_player();
|
let p = game.make_player();
|
||||||
|
|
||||||
let player = Arc::new(PlayerAgent::new(&content, p.0));
|
let mut player = Arc::new(PlayerAgent::new(p.0));
|
||||||
|
Arc::get_mut(&mut player).unwrap().set_camera_aspect(
|
||||||
|
gpu.window().inner_size().width as f32 / gpu.window().inner_size().height as f32,
|
||||||
|
);
|
||||||
|
|
||||||
let mut phys_img = Arc::new(PhysImage::new());
|
let mut phys_img = Arc::new(PhysImage::new());
|
||||||
|
let mut last_run = Instant::now();
|
||||||
|
|
||||||
event_loop.run(move |event, _, control_flow| {
|
event_loop.run(move |event, _, control_flow| {
|
||||||
match event {
|
match event {
|
||||||
Event::RedrawRequested(window_id) if window_id == gpu.window().id() => {
|
Event::RedrawRequested(window_id) if window_id == gpu.window().id() => {
|
||||||
match gpu.render(RenderInput {
|
let render_input = RenderInput {
|
||||||
|
camera_pos: player.camera.pos,
|
||||||
|
camera_zoom: player.camera.zoom,
|
||||||
current_time: game.get_current_time(),
|
current_time: game.get_current_time(),
|
||||||
ct: content.clone(),
|
ct: content.clone(),
|
||||||
phys_img: phys_img.clone(),
|
phys_img: phys_img.clone(),
|
||||||
player: player.clone(),
|
player: player.clone(),
|
||||||
// TODO: this is a hack for testing.
|
time_since_last_run: last_run.elapsed().as_secs_f32(),
|
||||||
current_system: content.systems.values().next().unwrap().clone(),
|
current_system: SystemHandle { index: 0 },
|
||||||
timing: game.get_timing().clone(),
|
timing: game.get_timing().clone(),
|
||||||
}) {
|
};
|
||||||
|
last_run = Instant::now();
|
||||||
|
|
||||||
|
match gpu.render(render_input) {
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
Err(wgpu::SurfaceError::Lost) => gpu.resize(&content),
|
Err(wgpu::SurfaceError::Lost) => gpu.resize(&content),
|
||||||
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
|
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
|
||||||
|
@ -147,8 +162,41 @@ fn try_main() -> Result<()> {
|
||||||
}
|
}
|
||||||
|
|
||||||
Event::MainEventsCleared => {
|
Event::MainEventsCleared => {
|
||||||
|
game.update_player_controls(Arc::get_mut(&mut player).unwrap());
|
||||||
game.step(&phys_img);
|
game.step(&phys_img);
|
||||||
game.update_image(Arc::get_mut(&mut phys_img).unwrap());
|
game.update_image(Arc::get_mut(&mut phys_img).unwrap());
|
||||||
|
|
||||||
|
// TODO: clean up
|
||||||
|
let player_status = {
|
||||||
|
let pos = {
|
||||||
|
let o = phys_img.get_ship(&PhysSimShipHandle(player.ship.unwrap()));
|
||||||
|
if let Some(o) = o {
|
||||||
|
match o.ship.get_data().get_state() {
|
||||||
|
ShipState::Landing { .. }
|
||||||
|
| ShipState::UnLanding { .. }
|
||||||
|
| ShipState::Collapsing { .. }
|
||||||
|
| ShipState::Flying { .. } => Some(*o.rigidbody.translation()),
|
||||||
|
|
||||||
|
ShipState::Landed { target } => {
|
||||||
|
let b = content.get_system_object(*target);
|
||||||
|
Some(Vector2::new(b.pos.x, b.pos.y))
|
||||||
|
}
|
||||||
|
|
||||||
|
ShipState::Dead => None,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
PlayerStatus { pos }
|
||||||
|
};
|
||||||
|
|
||||||
|
// This must be updated BEFORE rendering!
|
||||||
|
Arc::get_mut(&mut player)
|
||||||
|
.unwrap()
|
||||||
|
.step(&content, player_status);
|
||||||
|
Arc::get_mut(&mut player).unwrap().input.clear_inputs();
|
||||||
gpu.window().request_redraw();
|
gpu.window().request_redraw();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -157,7 +205,7 @@ fn try_main() -> Result<()> {
|
||||||
window_id,
|
window_id,
|
||||||
} if window_id == gpu.window().id() => match event {
|
} if window_id == gpu.window().id() => match event {
|
||||||
WindowEvent::Focused(_state) => {
|
WindowEvent::Focused(_state) => {
|
||||||
// TODO: handle focus loss
|
//game.set_paused(!state);
|
||||||
}
|
}
|
||||||
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
|
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
|
||||||
WindowEvent::KeyboardInput {
|
WindowEvent::KeyboardInput {
|
||||||
|
@ -169,94 +217,42 @@ fn try_main() -> Result<()> {
|
||||||
},
|
},
|
||||||
..
|
..
|
||||||
} => {
|
} => {
|
||||||
let directive = gpu
|
Arc::get_mut(&mut player)
|
||||||
.process_input(
|
.unwrap()
|
||||||
RenderInput {
|
.input
|
||||||
current_time: game.get_current_time(),
|
.process_key(state, key);
|
||||||
ct: content.clone(),
|
|
||||||
phys_img: phys_img.clone(),
|
|
||||||
player: player.clone(),
|
|
||||||
current_system: content.systems.values().next().unwrap().clone(),
|
|
||||||
timing: game.get_timing().clone(),
|
|
||||||
},
|
|
||||||
InputEvent::Keyboard {
|
|
||||||
down: state == &ElementState::Pressed,
|
|
||||||
key: *key,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
game.apply_directive(directive, &player);
|
|
||||||
}
|
}
|
||||||
WindowEvent::CursorMoved { position, .. } => {
|
WindowEvent::CursorMoved { position, .. } => {
|
||||||
let directive = gpu
|
Arc::get_mut(&mut player)
|
||||||
.process_input(
|
.unwrap()
|
||||||
RenderInput {
|
.input
|
||||||
current_time: game.get_current_time(),
|
.process_mouse(position);
|
||||||
ct: content.clone(),
|
|
||||||
phys_img: phys_img.clone(),
|
|
||||||
player: player.clone(),
|
|
||||||
current_system: content.systems.values().next().unwrap().clone(),
|
|
||||||
timing: game.get_timing().clone(),
|
|
||||||
},
|
|
||||||
InputEvent::MouseMove(position.cast()),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
game.apply_directive(directive, &player);
|
|
||||||
}
|
}
|
||||||
WindowEvent::MouseInput { state, button, .. } => {
|
WindowEvent::MouseInput { state, button, .. } => {
|
||||||
let down = state == &ElementState::Pressed;
|
Arc::get_mut(&mut player)
|
||||||
let event = match button {
|
.unwrap()
|
||||||
MouseButton::Left => Some(InputEvent::MouseLeftClick(down)),
|
.input
|
||||||
MouseButton::Right => Some(InputEvent::MouseRightClick(down)),
|
.process_click(state, button);
|
||||||
_ => None,
|
|
||||||
};
|
|
||||||
if let Some(event) = event {
|
|
||||||
let directive = gpu
|
|
||||||
.process_input(
|
|
||||||
RenderInput {
|
|
||||||
current_time: game.get_current_time(),
|
|
||||||
ct: content.clone(),
|
|
||||||
phys_img: phys_img.clone(),
|
|
||||||
player: player.clone(),
|
|
||||||
current_system: content
|
|
||||||
.systems
|
|
||||||
.values()
|
|
||||||
.next()
|
|
||||||
.unwrap()
|
|
||||||
.clone(),
|
|
||||||
timing: game.get_timing().clone(),
|
|
||||||
},
|
|
||||||
event,
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
game.apply_directive(directive, &player);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
WindowEvent::MouseWheel { delta, .. } => {
|
WindowEvent::MouseWheel { delta, phase, .. } => {
|
||||||
let directive = gpu
|
Arc::get_mut(&mut player)
|
||||||
.process_input(
|
.unwrap()
|
||||||
RenderInput {
|
.input
|
||||||
current_time: game.get_current_time(),
|
.process_scroll(delta, phase);
|
||||||
ct: content.clone(),
|
|
||||||
phys_img: phys_img.clone(),
|
|
||||||
player: player.clone(),
|
|
||||||
current_system: content.systems.values().next().unwrap().clone(),
|
|
||||||
timing: game.get_timing().clone(),
|
|
||||||
},
|
|
||||||
InputEvent::Scroll(match delta {
|
|
||||||
MouseScrollDelta::LineDelta(_h, v) => *v,
|
|
||||||
MouseScrollDelta::PixelDelta(v) => v.x as f32,
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
game.apply_directive(directive, &player);
|
|
||||||
}
|
}
|
||||||
WindowEvent::Resized(_) => {
|
WindowEvent::Resized(_) => {
|
||||||
gpu.resize(&content);
|
gpu.resize(&content);
|
||||||
|
Arc::get_mut(&mut player).unwrap().set_camera_aspect(
|
||||||
|
gpu.window().inner_size().width as f32
|
||||||
|
/ gpu.window().inner_size().height as f32,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
WindowEvent::ScaleFactorChanged { .. } => {
|
WindowEvent::ScaleFactorChanged { .. } => {
|
||||||
gpu.resize(&content);
|
gpu.resize(&content);
|
||||||
gpu.window().request_redraw();
|
Arc::get_mut(&mut player).unwrap().set_camera_aspect(
|
||||||
|
gpu.window().inner_size().width as f32
|
||||||
|
/ gpu.window().inner_size().height as f32,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
},
|
},
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
use nalgebra::Vector2;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub struct Camera {
|
||||||
|
/// Camera center
|
||||||
|
pub pos: Vector2<f32>,
|
||||||
|
|
||||||
|
/// Camera zoom
|
||||||
|
/// (How many game units tall is the viewport?)
|
||||||
|
pub zoom: f32,
|
||||||
|
|
||||||
|
/// Aspect ratio of viewport (width / height)
|
||||||
|
pub aspect: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Camera {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
pos: Vector2::new(0.0, 0.0),
|
||||||
|
zoom: 500.0,
|
||||||
|
aspect: 1.0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,151 @@
|
||||||
|
use winit::{
|
||||||
|
dpi::PhysicalPosition,
|
||||||
|
event::{ElementState, MouseButton, MouseScrollDelta, TouchPhase, VirtualKeyCode},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct InputStatus {
|
||||||
|
// Parameters
|
||||||
|
scroll_speed: f32,
|
||||||
|
|
||||||
|
mouse_position: PhysicalPosition<f32>,
|
||||||
|
|
||||||
|
// Continuous keys
|
||||||
|
key_left: bool,
|
||||||
|
key_right: bool,
|
||||||
|
key_thrust: bool,
|
||||||
|
key_guns: bool,
|
||||||
|
key_leftclick: bool,
|
||||||
|
|
||||||
|
// One-shot keys (automatically released at the end of each frame)
|
||||||
|
key_land: bool,
|
||||||
|
v_scroll: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl InputStatus {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
InputStatus {
|
||||||
|
key_left: false,
|
||||||
|
key_right: false,
|
||||||
|
key_thrust: false,
|
||||||
|
key_guns: false,
|
||||||
|
key_land: false,
|
||||||
|
key_leftclick: false,
|
||||||
|
mouse_position: PhysicalPosition { x: 0.0, y: 0.0 },
|
||||||
|
v_scroll: 0.0,
|
||||||
|
scroll_speed: 10.0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn release_all(&mut self) {
|
||||||
|
self.key_left = false;
|
||||||
|
self.key_right = false;
|
||||||
|
self.key_thrust = false;
|
||||||
|
self.key_guns = false;
|
||||||
|
self.key_land = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Called at the end of every frame,
|
||||||
|
/// resets one-shot keys.
|
||||||
|
pub fn clear_inputs(&mut self) {
|
||||||
|
self.key_land = false;
|
||||||
|
self.v_scroll = 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn process_key(&mut self, state: &ElementState, key: &VirtualKeyCode) {
|
||||||
|
let down = state == &ElementState::Pressed;
|
||||||
|
match key {
|
||||||
|
VirtualKeyCode::Left => {
|
||||||
|
self.key_left = down;
|
||||||
|
if down {
|
||||||
|
self.key_right = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
VirtualKeyCode::Right => {
|
||||||
|
self.key_right = down;
|
||||||
|
if down {
|
||||||
|
self.key_left = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
VirtualKeyCode::Up => self.key_thrust = down,
|
||||||
|
VirtualKeyCode::Space => self.key_guns = down,
|
||||||
|
VirtualKeyCode::L => self.key_land = down,
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn process_mouse(&mut self, position: &PhysicalPosition<f64>) {
|
||||||
|
self.mouse_position = PhysicalPosition {
|
||||||
|
x: position.x as f32,
|
||||||
|
y: position.y as f32,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn process_click(&mut self, state: &ElementState, key: &MouseButton) {
|
||||||
|
let down = state == &ElementState::Pressed;
|
||||||
|
match key {
|
||||||
|
MouseButton::Left => self.key_leftclick = down,
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn process_scroll(&mut self, delta: &MouseScrollDelta, _phase: &TouchPhase) {
|
||||||
|
match delta {
|
||||||
|
MouseScrollDelta::LineDelta(_h, v) => {
|
||||||
|
self.v_scroll -= self.scroll_speed * v;
|
||||||
|
}
|
||||||
|
// TODO: handle this better
|
||||||
|
MouseScrollDelta::PixelDelta(v) => {
|
||||||
|
self.v_scroll -= v.x as f32;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Public get-state methods
|
||||||
|
impl InputStatus {
|
||||||
|
/// Has the player applied vertical scroll?
|
||||||
|
/// This is measured in lines, scaled by scroll_speed
|
||||||
|
///
|
||||||
|
/// A positive value means scroll up, a negative value means scroll down.
|
||||||
|
/// This is reset to zero at the end of each frame.
|
||||||
|
pub fn get_v_scroll(&self) -> f32 {
|
||||||
|
self.v_scroll
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the current mouse position
|
||||||
|
pub fn get_mouse_pos(&self) -> PhysicalPosition<f32> {
|
||||||
|
self.mouse_position
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Is the player pressing the "turn left" key?
|
||||||
|
pub fn pressed_left(&self) -> bool {
|
||||||
|
self.key_left
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Is the player pressing the "turn right" key?
|
||||||
|
pub fn pressed_right(&self) -> bool {
|
||||||
|
self.key_right
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Is the player pressing the "fowards" key?
|
||||||
|
pub fn pressed_thrust(&self) -> bool {
|
||||||
|
self.key_thrust
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Is the player pressing the "fire guns" key?
|
||||||
|
pub fn pressed_guns(&self) -> bool {
|
||||||
|
self.key_guns
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Is the player pressing the left mouse button?
|
||||||
|
pub fn pressed_leftclick(&self) -> bool {
|
||||||
|
self.key_leftclick
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Has the player pressed the "land" key?
|
||||||
|
/// (One-shot, reset to false at the start of each frame)
|
||||||
|
pub fn pressed_land(&self) -> bool {
|
||||||
|
self.key_land
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,9 @@
|
||||||
|
mod camera;
|
||||||
|
mod inputstatus;
|
||||||
mod playeragent;
|
mod playeragent;
|
||||||
mod playerstatus;
|
mod playerstatus;
|
||||||
|
|
||||||
|
pub use camera::Camera;
|
||||||
|
pub use inputstatus::InputStatus;
|
||||||
pub use playeragent::PlayerAgent;
|
pub use playeragent::PlayerAgent;
|
||||||
pub use playerstatus::PlayerStatus;
|
pub use playerstatus::PlayerStatus;
|
||||||
|
|
|
@ -1,24 +1,25 @@
|
||||||
use galactica_content::{Content, ContentIndex, SystemObject};
|
use galactica_content::{Content, SystemHandle, SystemObjectHandle};
|
||||||
use rapier2d::geometry::ColliderHandle;
|
use rapier2d::geometry::ColliderHandle;
|
||||||
use std::sync::Arc;
|
|
||||||
|
use crate::{camera::Camera, inputstatus::InputStatus, PlayerStatus};
|
||||||
|
|
||||||
/// What the player has selected
|
/// What the player has selected
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub enum PlayerSelection {
|
pub enum PlayerSelection {
|
||||||
/// We have nothing selected
|
/// We have nothing selected
|
||||||
None,
|
None,
|
||||||
|
|
||||||
/// We have a system body selected
|
/// We have a system body selected
|
||||||
OrbitingBody(Arc<SystemObject>),
|
OrbitingBody(SystemObjectHandle),
|
||||||
|
|
||||||
/// We have a ship selected
|
/// We have a ship selected
|
||||||
Ship(ColliderHandle),
|
Ship(ColliderHandle),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PlayerSelection {
|
impl PlayerSelection {
|
||||||
pub fn get_planet(&self) -> Option<&Arc<SystemObject>> {
|
pub fn get_planet(&self) -> Option<SystemObjectHandle> {
|
||||||
match self {
|
match self {
|
||||||
Self::OrbitingBody(h) => Some(h),
|
Self::OrbitingBody(h) => Some(*h),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,22 +32,39 @@ pub struct PlayerAgent {
|
||||||
|
|
||||||
/// What the player has selected
|
/// What the player has selected
|
||||||
pub selection: PlayerSelection,
|
pub selection: PlayerSelection,
|
||||||
|
|
||||||
|
/// This player's camera
|
||||||
|
pub camera: Camera,
|
||||||
|
|
||||||
|
/// What buttons this player is pressing
|
||||||
|
pub input: InputStatus,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PlayerAgent {
|
impl PlayerAgent {
|
||||||
pub fn new(ct: &Content, ship: ColliderHandle) -> Self {
|
pub fn new(ship: ColliderHandle) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
input: InputStatus::new(),
|
||||||
|
camera: Camera::new(),
|
||||||
ship: Some(ship),
|
ship: Some(ship),
|
||||||
selection: PlayerSelection::OrbitingBody(
|
selection: PlayerSelection::OrbitingBody(SystemObjectHandle {
|
||||||
ct.systems
|
system_handle: SystemHandle { index: 0 },
|
||||||
.values()
|
body_index: 1,
|
||||||
.next()
|
}),
|
||||||
.unwrap()
|
}
|
||||||
.objects
|
}
|
||||||
.get(&ContentIndex::new("earth"))
|
|
||||||
.unwrap()
|
pub fn set_camera_aspect(&mut self, v: f32) {
|
||||||
.clone(),
|
self.camera.aspect = v
|
||||||
),
|
}
|
||||||
|
|
||||||
|
pub fn step(&mut self, ct: &Content, status: PlayerStatus) {
|
||||||
|
if self.input.get_v_scroll() != 0.0 {
|
||||||
|
self.camera.zoom = (self.camera.zoom + self.input.get_v_scroll())
|
||||||
|
.clamp(ct.get_config().zoom_min, ct.get_config().zoom_max);
|
||||||
|
}
|
||||||
|
|
||||||
|
if status.pos.is_some() {
|
||||||
|
self.camera.pos = status.pos.unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use bytemuck;
|
use bytemuck;
|
||||||
use galactica_content::Content;
|
use galactica_content::Content;
|
||||||
use galactica_system::{data::ShipState, phys::PhysSimShipHandle, PlayerDirective};
|
use galactica_system::data::ShipState;
|
||||||
use galactica_util::to_radians;
|
use galactica_util::to_radians;
|
||||||
use glyphon::{FontSystem, Resolution, SwashCache, TextAtlas, TextRenderer};
|
use glyphon::{FontSystem, Resolution, SwashCache, TextAtlas, TextRenderer};
|
||||||
use nalgebra::{Point2, Point3, Vector2};
|
use nalgebra::{Point2, Point3};
|
||||||
use std::{cell::RefCell, iter, rc::Rc, sync::Arc};
|
use std::{cell::RefCell, iter, rc::Rc, sync::Arc};
|
||||||
use wgpu;
|
use wgpu;
|
||||||
use winit::{self};
|
use winit;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
globaluniform::{GlobalDataContent, GlobalUniform, ObjectData},
|
globaluniform::{GlobalDataContent, GlobalUniform, ObjectData},
|
||||||
|
@ -17,7 +17,7 @@ use crate::{
|
||||||
texturearray::TextureArray,
|
texturearray::TextureArray,
|
||||||
ui::UiScriptExecutor,
|
ui::UiScriptExecutor,
|
||||||
vertexbuffer::{consts::SPRITE_INDICES, types::ObjectInstance},
|
vertexbuffer::{consts::SPRITE_INDICES, types::ObjectInstance},
|
||||||
InputEvent, RenderInput, RenderState, VertexBuffers,
|
RenderInput, RenderState, VertexBuffers,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// A high-level GPU wrapper. Reads game state (via RenderInput), produces pretty pictures.
|
/// A high-level GPU wrapper. Reads game state (via RenderInput), produces pretty pictures.
|
||||||
|
@ -120,20 +120,21 @@ impl GPUState {
|
||||||
glyphon::fontdb::Database::new(),
|
glyphon::fontdb::Database::new(),
|
||||||
);
|
);
|
||||||
|
|
||||||
for font in &ct.config.font_files {
|
let conf = ct.get_config();
|
||||||
|
for font in &conf.font_files {
|
||||||
text_font_system.db_mut().load_font_file(font)?;
|
text_font_system.db_mut().load_font_file(font)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: nice error if no family with this name is found
|
// TODO: nice error if no family with this name is found
|
||||||
text_font_system
|
text_font_system
|
||||||
.db_mut()
|
.db_mut()
|
||||||
.set_sans_serif_family(ct.config.font_sans.clone());
|
.set_sans_serif_family(conf.font_sans.clone());
|
||||||
text_font_system
|
text_font_system
|
||||||
.db_mut()
|
.db_mut()
|
||||||
.set_serif_family(ct.config.font_serif.clone());
|
.set_serif_family(conf.font_serif.clone());
|
||||||
text_font_system
|
text_font_system
|
||||||
.db_mut()
|
.db_mut()
|
||||||
.set_monospace_family(ct.config.font_mono.clone());
|
.set_monospace_family(conf.font_mono.clone());
|
||||||
//text_font_system
|
//text_font_system
|
||||||
// .db_mut()
|
// .db_mut()
|
||||||
// .set_cursive_family(conf.font_cursive.clone());
|
// .set_cursive_family(conf.font_cursive.clone());
|
||||||
|
@ -259,8 +260,6 @@ impl GPUState {
|
||||||
self.config.height = new_size.height;
|
self.config.height = new_size.height;
|
||||||
self.surface.configure(&self.device, &self.config);
|
self.surface.configure(&self.device, &self.config);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: this takes a long time. fix!
|
|
||||||
self.starfield.update_buffer(ct, &mut self.state);
|
self.starfield.update_buffer(ct, &mut self.state);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -276,64 +275,28 @@ impl GPUState {
|
||||||
self.starfield.update_buffer(ct, &mut self.state);
|
self.starfield.update_buffer(ct, &mut self.state);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handle user input
|
|
||||||
pub fn process_input(
|
|
||||||
&mut self,
|
|
||||||
input: RenderInput,
|
|
||||||
event: InputEvent,
|
|
||||||
) -> Result<PlayerDirective> {
|
|
||||||
let input = Arc::new(input);
|
|
||||||
self.ui.process_input(&mut self.state, input, event)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Main render function. Draws sprites on a window.
|
/// Main render function. Draws sprites on a window.
|
||||||
pub fn render(&mut self, input: RenderInput) -> Result<(), wgpu::SurfaceError> {
|
pub fn render(&mut self, input: RenderInput) -> Result<(), wgpu::SurfaceError> {
|
||||||
let input = Arc::new(input);
|
let input = Arc::new(input);
|
||||||
|
|
||||||
if let Some(ship) = input.player.ship {
|
|
||||||
let o = input.phys_img.get_ship(&PhysSimShipHandle(ship));
|
|
||||||
if let Some(o) = o {
|
|
||||||
match o.ship.get_data().get_state() {
|
|
||||||
ShipState::Landing { .. }
|
|
||||||
| ShipState::UnLanding { .. }
|
|
||||||
| ShipState::Collapsing { .. }
|
|
||||||
| ShipState::Flying { .. } => self
|
|
||||||
.ui
|
|
||||||
.state
|
|
||||||
.borrow_mut()
|
|
||||||
.camera
|
|
||||||
.set_pos(*o.rigidbody.translation()),
|
|
||||||
|
|
||||||
ShipState::Landed { target } => self
|
|
||||||
.ui
|
|
||||||
.state
|
|
||||||
.borrow_mut()
|
|
||||||
.camera
|
|
||||||
.set_pos(Vector2::new(target.pos.x, target.pos.y)),
|
|
||||||
|
|
||||||
ShipState::Dead => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update global values
|
// Update global values
|
||||||
self.state.queue.write_buffer(
|
self.state.queue.write_buffer(
|
||||||
&self.state.global_uniform.data_buffer,
|
&self.state.global_uniform.data_buffer,
|
||||||
0,
|
0,
|
||||||
bytemuck::cast_slice(&[GlobalDataContent {
|
bytemuck::cast_slice(&[GlobalDataContent {
|
||||||
camera_position_x: self.ui.state.borrow().camera.get_pos().x,
|
camera_position_x: input.camera_pos.x,
|
||||||
camera_position_y: self.ui.state.borrow().camera.get_pos().y,
|
camera_position_y: input.camera_pos.y,
|
||||||
camera_zoom: self.ui.state.borrow().camera.get_zoom(),
|
camera_zoom: input.camera_zoom,
|
||||||
camera_zoom_min: input.ct.config.zoom_min,
|
camera_zoom_min: input.ct.get_config().zoom_min,
|
||||||
camera_zoom_max: input.ct.config.zoom_max,
|
camera_zoom_max: input.ct.get_config().zoom_max,
|
||||||
window_size_w: self.state.window_size.width as f32,
|
window_size_w: self.state.window_size.width as f32,
|
||||||
window_size_h: self.state.window_size.height as f32,
|
window_size_h: self.state.window_size.height as f32,
|
||||||
window_scale: self.state.window.scale_factor() as f32,
|
window_scale: self.state.window.scale_factor() as f32,
|
||||||
window_aspect: self.state.window_aspect,
|
window_aspect: self.state.window_aspect,
|
||||||
starfield_sprite: input.ct.config.starfield_texture.into(),
|
starfield_sprite: input.ct.get_config().starfield_texture.into(),
|
||||||
starfield_tile_size: input.ct.config.starfield_size,
|
starfield_tile_size: input.ct.get_config().starfield_size,
|
||||||
starfield_size_min: input.ct.config.starfield_min_size,
|
starfield_size_min: input.ct.get_config().starfield_min_size,
|
||||||
starfield_size_max: input.ct.config.starfield_max_size,
|
starfield_size_max: input.ct.get_config().starfield_max_size,
|
||||||
}]),
|
}]),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -376,10 +339,8 @@ impl GPUState {
|
||||||
|
|
||||||
// Game coordinates (relative to camera) of ne and sw corners of screen.
|
// Game coordinates (relative to camera) of ne and sw corners of screen.
|
||||||
// Used to skip off-screen sprites.
|
// Used to skip off-screen sprites.
|
||||||
let clip_ne = Point2::new(-self.state.window_aspect, 1.0)
|
let clip_ne = Point2::new(-self.state.window_aspect, 1.0) * input.camera_zoom;
|
||||||
* self.ui.state.borrow().camera.get_zoom();
|
let clip_sw = Point2::new(self.state.window_aspect, -1.0) * input.camera_zoom;
|
||||||
let clip_sw = Point2::new(self.state.window_aspect, -1.0)
|
|
||||||
* self.ui.state.borrow().camera.get_zoom();
|
|
||||||
|
|
||||||
// Order matters, it determines what is drawn on top.
|
// Order matters, it determines what is drawn on top.
|
||||||
// The order inside ships and projectiles doesn't matter,
|
// The order inside ships and projectiles doesn't matter,
|
||||||
|
@ -464,9 +425,7 @@ impl GPUState {
|
||||||
},
|
},
|
||||||
(*self.ui.state)
|
(*self.ui.state)
|
||||||
.borrow_mut()
|
.borrow_mut()
|
||||||
.get_textareas(&input, &self.state.window)
|
.get_textareas(&input, &self.state.window),
|
||||||
.iter()
|
|
||||||
.map(|x| x.get_textarea()),
|
|
||||||
&mut self.state.text_cache,
|
&mut self.state.text_cache,
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
@ -509,7 +468,7 @@ impl GPUState {
|
||||||
ship_pos = Point3::new(pos.x, pos.y, 1.0);
|
ship_pos = Point3::new(pos.x, pos.y, 1.0);
|
||||||
let ship_rot = r.rotation();
|
let ship_rot = r.rotation();
|
||||||
ship_ang = ship_rot.angle();
|
ship_ang = ship_rot.angle();
|
||||||
ship_cnt = s.ship.get_data().get_content();
|
ship_cnt = input.ct.get_ship(s.ship.get_data().get_content());
|
||||||
}
|
}
|
||||||
|
|
||||||
ShipState::UnLanding { current_z, .. } | ShipState::Landing { current_z, .. } => {
|
ShipState::UnLanding { current_z, .. } | ShipState::Landing { current_z, .. } => {
|
||||||
|
@ -518,23 +477,23 @@ impl GPUState {
|
||||||
ship_pos = Point3::new(pos.x, pos.y, *current_z);
|
ship_pos = Point3::new(pos.x, pos.y, *current_z);
|
||||||
let ship_rot = r.rotation();
|
let ship_rot = r.rotation();
|
||||||
ship_ang = ship_rot.angle();
|
ship_ang = ship_rot.angle();
|
||||||
ship_cnt = s.ship.get_data().get_content();
|
ship_cnt = input.ct.get_ship(s.ship.get_data().get_content());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Position adjusted for parallax
|
// Position adjusted for parallax
|
||||||
// TODO: adjust parallax for zoom?
|
// TODO: adjust parallax for zoom?
|
||||||
// 1.0 is z-coordinate, which is constant for ships
|
// 1.0 is z-coordinate, which is constant for ships
|
||||||
let pos: Point2<f32> = (Point2::new(ship_pos.x, ship_pos.y)
|
let pos: Point2<f32> =
|
||||||
- self.ui.state.borrow().camera.get_pos())
|
(Point2::new(ship_pos.x, ship_pos.y) - input.camera_pos) / ship_pos.z;
|
||||||
/ ship_pos.z;
|
|
||||||
|
|
||||||
// Game dimensions of this sprite post-scale.
|
// Game dimensions of this sprite post-scale.
|
||||||
// Post-scale width or height, whichever is larger.
|
// Post-scale width or height, whichever is larger.
|
||||||
// This is in game units.
|
// This is in game units.
|
||||||
//
|
//
|
||||||
// We take the maximum to account for rotated sprites.
|
// We take the maximum to account for rotated sprites.
|
||||||
let m = (ship_cnt.size / ship_pos.z) * ship_cnt.sprite.aspect.max(1.0);
|
let m =
|
||||||
|
(ship_cnt.size / ship_pos.z) * input.ct.get_sprite(ship_cnt.sprite).aspect.max(1.0);
|
||||||
|
|
||||||
// Don't draw sprites that are off the screen
|
// Don't draw sprites that are off the screen
|
||||||
if pos.x < screen_clip.0.x - m
|
if pos.x < screen_clip.0.x - m
|
||||||
|
@ -631,14 +590,14 @@ impl GPUState {
|
||||||
// Position adjusted for parallax
|
// Position adjusted for parallax
|
||||||
// TODO: adjust parallax for zoom?
|
// TODO: adjust parallax for zoom?
|
||||||
// 1.0 is z-coordinate, which is constant for projectiles
|
// 1.0 is z-coordinate, which is constant for projectiles
|
||||||
let pos = (proj_pos - self.ui.state.borrow().camera.get_pos()) / 1.0;
|
let pos = (proj_pos - input.camera_pos) / 1.0;
|
||||||
|
|
||||||
// Game dimensions of this sprite post-scale.
|
// Game dimensions of this sprite post-scale.
|
||||||
// Post-scale width or height, whichever is larger.
|
// Post-scale width or height, whichever is larger.
|
||||||
// This is in game units.
|
// This is in game units.
|
||||||
//
|
//
|
||||||
// We take the maximum to account for rotated sprites.
|
// We take the maximum to account for rotated sprites.
|
||||||
let m = (proj_cnt.size / 1.0) * proj_cnt.sprite.aspect.max(1.0);
|
let m = (proj_cnt.size / 1.0) * input.ct.get_sprite(proj_cnt.sprite).aspect.max(1.0);
|
||||||
|
|
||||||
// Don't draw sprites that are off the screen
|
// Don't draw sprites that are off the screen
|
||||||
if pos.x < screen_clip.0.x - m
|
if pos.x < screen_clip.0.x - m
|
||||||
|
@ -682,25 +641,18 @@ impl GPUState {
|
||||||
// NE and SW corners of screen
|
// NE and SW corners of screen
|
||||||
screen_clip: (Point2<f32>, Point2<f32>),
|
screen_clip: (Point2<f32>, Point2<f32>),
|
||||||
) {
|
) {
|
||||||
// TODO: sort once, give objects state
|
let system = input.ct.get_system(input.current_system);
|
||||||
|
|
||||||
// Sort by z-distance. This is important, since these are
|
for o in &system.objects {
|
||||||
// rendered in this order. We need far objects to be behind
|
|
||||||
// near objects!
|
|
||||||
let mut v: Vec<_> = input.current_system.objects.values().collect();
|
|
||||||
v.sort_by(|a, b| b.pos.z.total_cmp(&a.pos.z));
|
|
||||||
|
|
||||||
for o in v {
|
|
||||||
// Position adjusted for parallax
|
// Position adjusted for parallax
|
||||||
let pos: Point2<f32> =
|
let pos: Point2<f32> = (Point2::new(o.pos.x, o.pos.y) - input.camera_pos) / o.pos.z;
|
||||||
(Point2::new(o.pos.x, o.pos.y) - self.ui.state.borrow().camera.get_pos()) / o.pos.z;
|
|
||||||
|
|
||||||
// Game dimensions of this sprite post-scale.
|
// Game dimensions of this sprite post-scale.
|
||||||
// Post-scale width or height, whichever is larger.
|
// Post-scale width or height, whichever is larger.
|
||||||
// This is in game units.
|
// This is in game units.
|
||||||
//
|
//
|
||||||
// We take the maximum to account for rotated sprites.
|
// We take the maximum to account for rotated sprites.
|
||||||
let m = (o.size / o.pos.z) * o.sprite.aspect.max(1.0);
|
let m = (o.size / o.pos.z) * input.ct.get_sprite(o.sprite).aspect.max(1.0);
|
||||||
|
|
||||||
// Don't draw sprites that are off the screen
|
// Don't draw sprites that are off the screen
|
||||||
if pos.x < screen_clip.0.x - m
|
if pos.x < screen_clip.0.x - m
|
||||||
|
@ -728,7 +680,8 @@ impl GPUState {
|
||||||
}]),
|
}]),
|
||||||
);
|
);
|
||||||
|
|
||||||
let texture_a = o.sprite.get_first_frame(); // ANIMATE
|
let sprite = input.ct.get_sprite(o.sprite);
|
||||||
|
let texture_a = sprite.get_first_frame(); // ANIMATE
|
||||||
|
|
||||||
// Push this object's instance
|
// Push this object's instance
|
||||||
self.state.push_object_buffer(ObjectInstance {
|
self.state.push_object_buffer(ObjectInstance {
|
||||||
|
@ -755,14 +708,19 @@ impl GPUState {
|
||||||
// Position adjusted for parallax
|
// Position adjusted for parallax
|
||||||
// TODO: adjust parallax for zoom?
|
// TODO: adjust parallax for zoom?
|
||||||
// 1.0 is z-coordinate, which is constant for projectiles
|
// 1.0 is z-coordinate, which is constant for projectiles
|
||||||
let adjusted_pos = (pos - self.ui.state.borrow().camera.get_pos()) / 1.0;
|
let adjusted_pos = (pos - input.camera_pos) / 1.0;
|
||||||
|
|
||||||
// Game dimensions of this sprite post-scale.
|
// Game dimensions of this sprite post-scale.
|
||||||
// Post-scale width or height, whichever is larger.
|
// Post-scale width or height, whichever is larger.
|
||||||
// This is in game units.
|
// This is in game units.
|
||||||
//
|
//
|
||||||
// We take the maximum to account for rotated sprites.
|
// We take the maximum to account for rotated sprites.
|
||||||
let m = (p.effect.size / 1.0) * p.effect.anim.get_sprite().aspect.max(1.0);
|
let m = (p.effect.size / 1.0)
|
||||||
|
* input
|
||||||
|
.ct
|
||||||
|
.get_sprite(p.effect.anim.get_sprite())
|
||||||
|
.aspect
|
||||||
|
.max(1.0);
|
||||||
|
|
||||||
// Don't draw sprites that are off the screen
|
// Don't draw sprites that are off the screen
|
||||||
if adjusted_pos.x < screen_clip.0.x - m
|
if adjusted_pos.x < screen_clip.0.x - m
|
||||||
|
|
|
@ -1,29 +0,0 @@
|
||||||
use winit::{dpi::PhysicalPosition, event::VirtualKeyCode};
|
|
||||||
|
|
||||||
/// Input received from the user
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum InputEvent {
|
|
||||||
/// Mouse was moved
|
|
||||||
MouseMove(PhysicalPosition<f32>),
|
|
||||||
|
|
||||||
/// Mouse left button was clicked.
|
|
||||||
/// True if pressed, false if released.
|
|
||||||
MouseLeftClick(bool),
|
|
||||||
|
|
||||||
/// Mouse left button was clicked.
|
|
||||||
/// True if pressed, false if released.
|
|
||||||
MouseRightClick(bool),
|
|
||||||
|
|
||||||
/// Mouse was scrolled.
|
|
||||||
/// Value is number of lines, positive or negative.
|
|
||||||
Scroll(f32),
|
|
||||||
|
|
||||||
/// A key was pressed or released
|
|
||||||
Keyboard {
|
|
||||||
/// True if pressed, false if released
|
|
||||||
down: bool,
|
|
||||||
|
|
||||||
/// The key that was pressed
|
|
||||||
key: VirtualKeyCode,
|
|
||||||
},
|
|
||||||
}
|
|
|
@ -9,7 +9,6 @@
|
||||||
|
|
||||||
mod globaluniform;
|
mod globaluniform;
|
||||||
mod gpustate;
|
mod gpustate;
|
||||||
mod inputevent;
|
|
||||||
mod pipeline;
|
mod pipeline;
|
||||||
mod renderinput;
|
mod renderinput;
|
||||||
mod renderstate;
|
mod renderstate;
|
||||||
|
@ -19,8 +18,7 @@ mod texturearray;
|
||||||
mod ui;
|
mod ui;
|
||||||
mod vertexbuffer;
|
mod vertexbuffer;
|
||||||
|
|
||||||
pub use gpustate::*;
|
pub use gpustate::GPUState;
|
||||||
pub use inputevent::*;
|
|
||||||
pub use renderinput::RenderInput;
|
pub use renderinput::RenderInput;
|
||||||
use renderstate::*;
|
use renderstate::*;
|
||||||
|
|
||||||
|
|
|
@ -1,18 +1,25 @@
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use galactica_content::{Content, System};
|
use galactica_content::{Content, SystemHandle};
|
||||||
use galactica_playeragent::PlayerAgent;
|
use galactica_playeragent::PlayerAgent;
|
||||||
use galactica_system::phys::PhysImage;
|
use galactica_system::phys::PhysImage;
|
||||||
use galactica_util::timing::Timing;
|
use galactica_util::timing::Timing;
|
||||||
|
use nalgebra::Vector2;
|
||||||
|
|
||||||
/// Bundles parameters passed to a single call to GPUState::render
|
/// Bundles parameters passed to a single call to GPUState::render
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct RenderInput {
|
pub struct RenderInput {
|
||||||
|
/// Camera position, in world units
|
||||||
|
pub camera_pos: Vector2<f32>,
|
||||||
|
|
||||||
/// Player ship data
|
/// Player ship data
|
||||||
pub player: Arc<PlayerAgent>,
|
pub player: Arc<PlayerAgent>,
|
||||||
|
|
||||||
/// The system we're currently in
|
/// The system we're currently in
|
||||||
pub current_system: Arc<System>,
|
pub current_system: SystemHandle,
|
||||||
|
|
||||||
|
/// Height of screen, in world units
|
||||||
|
pub camera_zoom: f32,
|
||||||
|
|
||||||
/// The world state to render
|
/// The world state to render
|
||||||
pub phys_img: Arc<PhysImage>,
|
pub phys_img: Arc<PhysImage>,
|
||||||
|
@ -21,6 +28,9 @@ pub struct RenderInput {
|
||||||
/// The current time, in seconds
|
/// The current time, in seconds
|
||||||
pub current_time: f32,
|
pub current_time: f32,
|
||||||
|
|
||||||
|
/// The amount of time that has passed since the last frame was drawn
|
||||||
|
pub time_since_last_run: f32,
|
||||||
|
|
||||||
/// Game content
|
/// Game content
|
||||||
pub ct: Arc<Content>,
|
pub ct: Arc<Content>,
|
||||||
|
|
||||||
|
|
|
@ -39,7 +39,7 @@ impl<'a> VertexBuffers {
|
||||||
ui_counter: 0,
|
ui_counter: 0,
|
||||||
radialbar_counter: 0,
|
radialbar_counter: 0,
|
||||||
starfield_counter: 0,
|
starfield_counter: 0,
|
||||||
starfield_limit: ct.config.starfield_instance_limit,
|
starfield_limit: ct.get_config().starfield_instance_limit,
|
||||||
|
|
||||||
object: VertexBuffer::new::<TexturedVertex, ObjectInstance>(
|
object: VertexBuffer::new::<TexturedVertex, ObjectInstance>(
|
||||||
"object",
|
"object",
|
||||||
|
@ -54,7 +54,7 @@ impl<'a> VertexBuffers {
|
||||||
&device,
|
&device,
|
||||||
Some(SPRITE_VERTICES),
|
Some(SPRITE_VERTICES),
|
||||||
Some(SPRITE_INDICES),
|
Some(SPRITE_INDICES),
|
||||||
ct.config.starfield_instance_limit,
|
ct.get_config().starfield_instance_limit,
|
||||||
),
|
),
|
||||||
|
|
||||||
ui: VertexBuffer::new::<TexturedVertex, UiInstance>(
|
ui: VertexBuffer::new::<TexturedVertex, UiInstance>(
|
||||||
|
|
|
@ -30,32 +30,36 @@ impl Starfield {
|
||||||
pub fn regenerate(&mut self, ct: &Content) {
|
pub fn regenerate(&mut self, ct: &Content) {
|
||||||
// TODO: save seed in system, regenerate on jump
|
// TODO: save seed in system, regenerate on jump
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
let sz = ct.config.starfield_size as f32 / 2.0;
|
let sz = ct.get_config().starfield_size as f32 / 2.0;
|
||||||
self.stars = (0..ct.config.starfield_count)
|
self.stars = (0..ct.get_config().starfield_count)
|
||||||
.map(|_| StarfieldStar {
|
.map(|_| StarfieldStar {
|
||||||
pos: Point3::new(
|
pos: Point3::new(
|
||||||
rng.gen_range(-sz..=sz),
|
rng.gen_range(-sz..=sz),
|
||||||
rng.gen_range(-sz..=sz),
|
rng.gen_range(-sz..=sz),
|
||||||
rng.gen_range(ct.config.starfield_min_dist..=ct.config.starfield_max_dist),
|
rng.gen_range(
|
||||||
|
ct.get_config().starfield_min_dist..=ct.get_config().starfield_max_dist,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
size: rng.gen_range(
|
||||||
|
ct.get_config().starfield_min_size..ct.get_config().starfield_max_size,
|
||||||
),
|
),
|
||||||
size: rng.gen_range(ct.config.starfield_min_size..ct.config.starfield_max_size),
|
|
||||||
tint: Vector2::new(rng.gen_range(0.0..=1.0), rng.gen_range(0.0..=1.0)),
|
tint: Vector2::new(rng.gen_range(0.0..=1.0), rng.gen_range(0.0..=1.0)),
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_buffer(&mut self, ct: &Content, state: &mut RenderState) {
|
pub fn update_buffer(&mut self, ct: &Content, state: &mut RenderState) {
|
||||||
let sz = ct.config.starfield_size as f32;
|
let sz = ct.get_config().starfield_size as f32;
|
||||||
|
|
||||||
// Compute window size in starfield tiles
|
// Compute window size in starfield tiles
|
||||||
let mut nw_tile = {
|
let mut nw_tile = {
|
||||||
// Game coordinates (relative to camera) of nw corner of screen.
|
// Game coordinates (relative to camera) of nw corner of screen.
|
||||||
let clip_nw = Point2::new(state.window_aspect, 1.0) * ct.config.zoom_max;
|
let clip_nw = Point2::new(state.window_aspect, 1.0) * ct.get_config().zoom_max;
|
||||||
|
|
||||||
// Parallax correction.
|
// Parallax correction.
|
||||||
// Also, adjust v for mod to work properly
|
// Also, adjust v for mod to work properly
|
||||||
// (v is centered at 0)
|
// (v is centered at 0)
|
||||||
let v: Point2<f32> = clip_nw * ct.config.starfield_min_dist;
|
let v: Point2<f32> = clip_nw * ct.get_config().starfield_min_dist;
|
||||||
let v_adj = Point2::new(v.x + (sz / 2.0), v.y + (sz / 2.0));
|
let v_adj = Point2::new(v.x + (sz / 2.0), v.y + (sz / 2.0));
|
||||||
|
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
|
@ -79,8 +83,8 @@ impl Starfield {
|
||||||
|
|
||||||
// Truncate tile grid to buffer size
|
// Truncate tile grid to buffer size
|
||||||
// (The window won't be full of stars if our instance limit is too small)
|
// (The window won't be full of stars if our instance limit is too small)
|
||||||
while ((nw_tile.x * 2 + 1) * (nw_tile.y * 2 + 1) * ct.config.starfield_count as i32)
|
while ((nw_tile.x * 2 + 1) * (nw_tile.y * 2 + 1) * ct.get_config().starfield_count as i32)
|
||||||
> ct.config.starfield_instance_limit as i32
|
> ct.get_config().starfield_instance_limit as i32
|
||||||
{
|
{
|
||||||
nw_tile -= Vector2::new(1, 1);
|
nw_tile -= Vector2::new(1, 1);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,28 +0,0 @@
|
||||||
use rhai::{plugin::*, Dynamic, Module};
|
|
||||||
|
|
||||||
#[export_module]
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
#[allow(non_upper_case_globals)]
|
|
||||||
pub mod player_directive_module {
|
|
||||||
use galactica_system::PlayerDirective;
|
|
||||||
|
|
||||||
pub const None: PlayerDirective = PlayerDirective::None;
|
|
||||||
pub const Land: PlayerDirective = PlayerDirective::Land;
|
|
||||||
pub const UnLand: PlayerDirective = PlayerDirective::UnLand;
|
|
||||||
|
|
||||||
pub fn Engine(state: bool) -> PlayerDirective {
|
|
||||||
PlayerDirective::Engine(state)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn TurnLeft(state: bool) -> PlayerDirective {
|
|
||||||
PlayerDirective::TurnLeft(state)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn TurnRight(state: bool) -> PlayerDirective {
|
|
||||||
PlayerDirective::TurnRight(state)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn Guns(state: bool) -> PlayerDirective {
|
|
||||||
PlayerDirective::Guns(state)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -38,31 +38,3 @@ impl CustomType for PlayerShipStateEvent {
|
||||||
builder.with_name("PlayerShipStateEvent");
|
builder.with_name("PlayerShipStateEvent");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct KeyboardEvent {
|
|
||||||
pub down: bool,
|
|
||||||
pub key: ImmutableString,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CustomType for KeyboardEvent {
|
|
||||||
fn build(mut builder: TypeBuilder<Self>) {
|
|
||||||
builder
|
|
||||||
.with_name("KeyboardEvent")
|
|
||||||
.with_fn("is_down", |s: &mut Self| s.down)
|
|
||||||
.with_fn("key", |s: &mut Self| s.key.clone());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct ScrollEvent {
|
|
||||||
pub val: f32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CustomType for ScrollEvent {
|
|
||||||
fn build(mut builder: TypeBuilder<Self>) {
|
|
||||||
builder
|
|
||||||
.with_name("ScrollEvent")
|
|
||||||
.with_fn("val", |s: &mut Self| s.val);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,13 +1,11 @@
|
||||||
mod conf;
|
mod conf;
|
||||||
mod radialbar;
|
mod radialbar;
|
||||||
mod scrollbox;
|
|
||||||
mod sprite;
|
mod sprite;
|
||||||
mod textbox;
|
mod textbox;
|
||||||
mod ui;
|
mod ui;
|
||||||
|
|
||||||
pub use conf::build_conf_module;
|
pub use conf::build_conf_module;
|
||||||
pub use radialbar::build_radialbar_module;
|
pub use radialbar::build_radialbar_module;
|
||||||
pub use scrollbox::build_scrollbox_module;
|
|
||||||
pub use sprite::build_sprite_module;
|
pub use sprite::build_sprite_module;
|
||||||
pub use textbox::build_textbox_module;
|
pub use textbox::build_textbox_module;
|
||||||
pub use ui::build_ui_module;
|
pub use ui::build_ui_module;
|
||||||
|
|
|
@ -3,7 +3,7 @@ use rhai::{FnNamespace, FuncRegistration, ImmutableString, Module};
|
||||||
use std::{cell::RefCell, rc::Rc};
|
use std::{cell::RefCell, rc::Rc};
|
||||||
|
|
||||||
use super::super::{Color, Rect};
|
use super::super::{Color, Rect};
|
||||||
use crate::ui::{elements::UiRadialBar, UiElement, UiState};
|
use crate::ui::{elements::RadialBar, UiElement, UiState};
|
||||||
|
|
||||||
pub fn build_radialbar_module(state_src: Rc<RefCell<UiState>>) -> Module {
|
pub fn build_radialbar_module(state_src: Rc<RefCell<UiState>>) -> Module {
|
||||||
let mut module = Module::new();
|
let mut module = Module::new();
|
||||||
|
@ -23,13 +23,11 @@ pub fn build_radialbar_module(state_src: Rc<RefCell<UiState>>) -> Module {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ui_state.add_element(UiElement::RadialBar(UiRadialBar::new(
|
ui_state.names.push(name.clone());
|
||||||
|
ui_state.elements.insert(
|
||||||
name.clone(),
|
name.clone(),
|
||||||
stroke,
|
UiElement::RadialBar(RadialBar::new(name.clone(), stroke, color, rect, 1.0)),
|
||||||
color,
|
);
|
||||||
rect,
|
|
||||||
1.0,
|
|
||||||
)));
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -1,77 +0,0 @@
|
||||||
use log::error;
|
|
||||||
use rhai::{FnNamespace, FuncRegistration, ImmutableString, Module};
|
|
||||||
use std::{cell::RefCell, rc::Rc};
|
|
||||||
|
|
||||||
use super::super::Rect;
|
|
||||||
use crate::ui::{elements::UiScrollbox, UiElement, UiState};
|
|
||||||
|
|
||||||
pub fn build_scrollbox_module(state_src: Rc<RefCell<UiState>>) -> Module {
|
|
||||||
let mut module = Module::new();
|
|
||||||
module.set_id("GalacticaScrollboxModule");
|
|
||||||
|
|
||||||
let state = state_src.clone();
|
|
||||||
let _ = FuncRegistration::new("add")
|
|
||||||
.with_namespace(FnNamespace::Internal)
|
|
||||||
.set_into_module(&mut module, move |name: ImmutableString, rect: Rect| {
|
|
||||||
let mut ui_state = state.borrow_mut();
|
|
||||||
|
|
||||||
if ui_state.elements.contains_key(&name) {
|
|
||||||
error!("tried to make a scrollbox using an existing name `{name}`");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ui_state.add_element(UiElement::Scrollbox(UiScrollbox::new(name.clone(), rect)));
|
|
||||||
});
|
|
||||||
|
|
||||||
let state = state_src.clone();
|
|
||||||
let _ = FuncRegistration::new("add_element")
|
|
||||||
.with_namespace(FnNamespace::Internal)
|
|
||||||
.set_into_module(
|
|
||||||
&mut module,
|
|
||||||
move |name: ImmutableString, target: ImmutableString| {
|
|
||||||
let mut ui_state = state.borrow_mut();
|
|
||||||
match ui_state.get_mut_by_name(&name) {
|
|
||||||
Some(UiElement::Scrollbox(_)) => {
|
|
||||||
match ui_state.get_mut_by_name(&target) {
|
|
||||||
Some(UiElement::Text(_)) | Some(UiElement::Sprite(_)) => {
|
|
||||||
let e = match ui_state.remove_element_incomplete(&target) {
|
|
||||||
Some(UiElement::Sprite(s)) => {
|
|
||||||
Rc::new(RefCell::new(UiElement::Sprite(s)))
|
|
||||||
}
|
|
||||||
Some(UiElement::Text(t)) => {
|
|
||||||
Rc::new(RefCell::new(UiElement::Text(t)))
|
|
||||||
}
|
|
||||||
_ => unreachable!(),
|
|
||||||
};
|
|
||||||
|
|
||||||
// Add a subelement pointing to this sprite
|
|
||||||
ui_state.add_element(UiElement::SubElement {
|
|
||||||
parent: name.clone(),
|
|
||||||
element: e.clone(),
|
|
||||||
});
|
|
||||||
|
|
||||||
// Add this sprite to a scrollbox
|
|
||||||
match ui_state.get_mut_by_name(&name) {
|
|
||||||
Some(UiElement::Scrollbox(s)) => {
|
|
||||||
s.add_element(e);
|
|
||||||
}
|
|
||||||
_ => unreachable!(),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
Some(_) => {
|
|
||||||
error!("cannot add `{name}` to scrollbox `{name}`, invalid type.")
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
error!("called `scrollbox::add_element` with a non-existing target `{target}`")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
error!("called `scrollbox::add_element` on an invalid name `{name}`")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
return module;
|
|
||||||
}
|
|
|
@ -3,7 +3,7 @@ use log::error;
|
||||||
use rhai::{FnNamespace, FuncRegistration, ImmutableString, Module};
|
use rhai::{FnNamespace, FuncRegistration, ImmutableString, Module};
|
||||||
use std::{cell::RefCell, rc::Rc, sync::Arc};
|
use std::{cell::RefCell, rc::Rc, sync::Arc};
|
||||||
|
|
||||||
use crate::ui::{elements::UiSprite, UiElement, UiState};
|
use crate::ui::{elements::Sprite, UiElement, UiState};
|
||||||
|
|
||||||
use super::super::{Color, Rect};
|
use super::super::{Color, Rect};
|
||||||
|
|
||||||
|
@ -17,12 +17,12 @@ pub fn build_sprite_module(ct_src: Arc<Content>, state_src: Rc<RefCell<UiState>>
|
||||||
.with_namespace(FnNamespace::Internal)
|
.with_namespace(FnNamespace::Internal)
|
||||||
.set_into_module(
|
.set_into_module(
|
||||||
&mut module,
|
&mut module,
|
||||||
move |name: ImmutableString, sprite_name: ImmutableString, rect: Rect| {
|
move |name: ImmutableString, sprite: ImmutableString, rect: Rect| {
|
||||||
let mut ui_state = state.borrow_mut();
|
let mut ui_state = state.borrow_mut();
|
||||||
|
|
||||||
let sprite = ct.sprites.get(&sprite_name.clone().into());
|
let sprite_handle = ct.get_sprite_handle(sprite.as_str());
|
||||||
if sprite.is_none() {
|
if sprite_handle.is_none() {
|
||||||
error!("made a sprite using an invalid source `{sprite_name}`");
|
error!("made a sprite using an invalid source `{sprite}`");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,11 +31,11 @@ pub fn build_sprite_module(ct_src: Arc<Content>, state_src: Rc<RefCell<UiState>>
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ui_state.add_element(UiElement::Sprite(UiSprite::new(
|
ui_state.names.push(name.clone());
|
||||||
|
ui_state.elements.insert(
|
||||||
name.clone(),
|
name.clone(),
|
||||||
sprite.unwrap().clone(),
|
UiElement::Sprite(Sprite::new(&ct, name.clone(), sprite_handle.unwrap(), rect)),
|
||||||
rect,
|
);
|
||||||
)));
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -56,7 +56,8 @@ pub fn build_sprite_module(ct_src: Arc<Content>, state_src: Rc<RefCell<UiState>>
|
||||||
.set_into_module(&mut module, move |name: ImmutableString| {
|
.set_into_module(&mut module, move |name: ImmutableString| {
|
||||||
let mut ui_state = state.borrow_mut();
|
let mut ui_state = state.borrow_mut();
|
||||||
if ui_state.elements.contains_key(&name) {
|
if ui_state.elements.contains_key(&name) {
|
||||||
ui_state.remove_element(&name);
|
ui_state.elements.remove(&name).unwrap();
|
||||||
|
ui_state.names.retain(|x| *x != name);
|
||||||
} else {
|
} else {
|
||||||
error!("called `sprite::remove` on an invalid name `{name}`")
|
error!("called `sprite::remove` on an invalid name `{name}`")
|
||||||
}
|
}
|
||||||
|
@ -73,12 +74,12 @@ pub fn build_sprite_module(ct_src: Arc<Content>, state_src: Rc<RefCell<UiState>>
|
||||||
|
|
||||||
match ui_state.get_mut_by_name(&name) {
|
match ui_state.get_mut_by_name(&name) {
|
||||||
Some(UiElement::Sprite(x)) => {
|
Some(UiElement::Sprite(x)) => {
|
||||||
let m = ct.sprites.get(&mask.clone().into()).clone();
|
let m = ct.get_sprite_handle(mask.as_str());
|
||||||
if m.is_none() {
|
if m.is_none() {
|
||||||
error!("called `set_sprite_mask` with an invalid mask `{mask}`");
|
error!("called `set_sprite_mask` with an invalid mask `{mask}`");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
x.set_mask(m.cloned())
|
x.set_mask(m)
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => {
|
_ => {
|
||||||
|
@ -89,7 +90,8 @@ pub fn build_sprite_module(ct_src: Arc<Content>, state_src: Rc<RefCell<UiState>>
|
||||||
);
|
);
|
||||||
|
|
||||||
let state = state_src.clone();
|
let state = state_src.clone();
|
||||||
let _ = FuncRegistration::new("jump_to")
|
let ct = ct_src.clone();
|
||||||
|
let _ = FuncRegistration::new("take_edge")
|
||||||
.with_namespace(FnNamespace::Internal)
|
.with_namespace(FnNamespace::Internal)
|
||||||
.set_into_module(
|
.set_into_module(
|
||||||
&mut module,
|
&mut module,
|
||||||
|
@ -98,25 +100,27 @@ pub fn build_sprite_module(ct_src: Arc<Content>, state_src: Rc<RefCell<UiState>>
|
||||||
|
|
||||||
match ui_state.get_mut_by_name(&name) {
|
match ui_state.get_mut_by_name(&name) {
|
||||||
Some(UiElement::Sprite(x)) => {
|
Some(UiElement::Sprite(x)) => {
|
||||||
let sprite = x.anim.get_sprite();
|
let sprite_handle = x.anim.get_sprite();
|
||||||
|
let sprite = &ct.get_sprite(sprite_handle);
|
||||||
|
|
||||||
let edge =
|
let edge = resolve_edge_as_edge(edge_name.as_str(), duration, |x| {
|
||||||
resolve_edge_as_edge(&sprite.sections, edge_name.as_str(), duration);
|
sprite.get_section_handle_by_name(x)
|
||||||
|
});
|
||||||
let edge = match edge {
|
let edge = match edge {
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
error!(
|
error!(
|
||||||
"called `sprite::jump_to` on an invalid edge `{}` on sprite `{}`",
|
"called `sprite::take_edge` on an invalid edge `{}` on sprite `{}`",
|
||||||
edge_name, sprite.index
|
edge_name, sprite.name
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Ok(s) => s,
|
Ok(s) => s,
|
||||||
};
|
};
|
||||||
|
|
||||||
x.anim.jump_to(&edge);
|
x.anim.jump_to(&ct, edge);
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
error!("called `sprite::jump_to` on an invalid name `{name}`")
|
error!("called `sprite::take_edge` on an invalid name `{name}`")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -4,7 +4,7 @@ use rhai::{FnNamespace, FuncRegistration, ImmutableString, Module};
|
||||||
use std::{cell::RefCell, rc::Rc};
|
use std::{cell::RefCell, rc::Rc};
|
||||||
|
|
||||||
use super::super::{Color, Rect};
|
use super::super::{Color, Rect};
|
||||||
use crate::ui::{elements::UiTextBox, UiElement, UiState};
|
use crate::ui::{elements::TextBox, UiElement, UiState};
|
||||||
|
|
||||||
pub fn build_textbox_module(
|
pub fn build_textbox_module(
|
||||||
font_src: Rc<RefCell<FontSystem>>,
|
font_src: Rc<RefCell<FontSystem>>,
|
||||||
|
@ -32,14 +32,18 @@ pub fn build_textbox_module(
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ui_state.add_element(UiElement::Text(UiTextBox::new(
|
ui_state.names.push(name.clone());
|
||||||
&mut font.borrow_mut(),
|
ui_state.elements.insert(
|
||||||
name.clone(),
|
name.clone(),
|
||||||
font_size,
|
UiElement::Text(TextBox::new(
|
||||||
line_height,
|
&mut font.borrow_mut(),
|
||||||
rect,
|
name.clone(),
|
||||||
color,
|
font_size,
|
||||||
)));
|
line_height,
|
||||||
|
rect,
|
||||||
|
color,
|
||||||
|
)),
|
||||||
|
);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -15,17 +15,5 @@ pub fn build_ui_module(state_src: Rc<RefCell<UiState>>) -> Module {
|
||||||
ui_state.set_scene(scene);
|
ui_state.set_scene(scene);
|
||||||
});
|
});
|
||||||
|
|
||||||
let state = state_src.clone();
|
|
||||||
let _ = FuncRegistration::new("get_camera_zoom")
|
|
||||||
.with_namespace(FnNamespace::Internal)
|
|
||||||
.set_into_module(&mut module, move || state.borrow().camera.get_zoom());
|
|
||||||
|
|
||||||
let state = state_src.clone();
|
|
||||||
let _ = FuncRegistration::new("set_camera_zoom")
|
|
||||||
.with_namespace(FnNamespace::Internal)
|
|
||||||
.set_into_module(&mut module, move |z: f32| {
|
|
||||||
state.borrow_mut().camera.set_zoom(z)
|
|
||||||
});
|
|
||||||
|
|
||||||
return module;
|
return module;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,9 @@
|
||||||
use nalgebra::{Point2, Vector2};
|
use nalgebra::{Point2, Vector2};
|
||||||
use rhai::{CustomType, TypeBuilder};
|
use rhai::{CustomType, TypeBuilder};
|
||||||
use winit::{
|
use winit::{dpi::LogicalSize, window::Window};
|
||||||
dpi::{LogicalSize, PhysicalPosition},
|
|
||||||
window::Window,
|
|
||||||
};
|
|
||||||
|
|
||||||
use super::{anchor::Anchor, vector::UiVector};
|
use super::anchor::Anchor;
|
||||||
use crate::RenderState;
|
use crate::{RenderInput, RenderState};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Rect {
|
pub struct Rect {
|
||||||
|
@ -81,11 +78,7 @@ impl Rect {
|
||||||
|
|
||||||
impl CustomType for Rect {
|
impl CustomType for Rect {
|
||||||
fn build(mut builder: TypeBuilder<Self>) {
|
fn build(mut builder: TypeBuilder<Self>) {
|
||||||
builder
|
builder.with_name("Rect").with_fn("Rect", Self::new);
|
||||||
.with_name("Rect")
|
|
||||||
.with_fn("Rect", Self::new)
|
|
||||||
.with_fn("pos", |s: &mut Self| UiVector::new(s.pos.x, s.pos.y))
|
|
||||||
.with_fn("dim", |s: &mut Self| UiVector::new(s.dim.x, s.dim.y));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,16 +100,17 @@ impl CenteredRect {
|
||||||
return (pt.y < ne.y && pt.y > sw.y) && (pt.x > ne.x && pt.x < sw.x);
|
return (pt.y < ne.y && pt.y > sw.y) && (pt.x > ne.x && pt.x < sw.x);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn contains_mouse(&self, state: &RenderState, mouse_pos: &PhysicalPosition<f32>) -> bool {
|
pub fn contains_mouse(&self, input: &RenderInput, state: &RenderState) -> bool {
|
||||||
let fac = state.window.scale_factor() as f32;
|
let fac = state.window.scale_factor() as f32;
|
||||||
let window_size = Vector2::new(
|
let window_size = Vector2::new(
|
||||||
state.window_size.width as f32 / fac,
|
state.window_size.width as f32 / fac,
|
||||||
state.window_size.height as f32 / fac,
|
state.window_size.height as f32 / fac,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let pos = input.player.input.get_mouse_pos();
|
||||||
let mouse_pos = Point2::new(
|
let mouse_pos = Point2::new(
|
||||||
mouse_pos.x / fac - window_size.x / 2.0,
|
pos.x / fac - window_size.x / 2.0,
|
||||||
window_size.y / 2.0 - mouse_pos.y / fac,
|
window_size.y / 2.0 - pos.y / fac,
|
||||||
);
|
);
|
||||||
|
|
||||||
return self.contains_point(mouse_pos);
|
return self.contains_point(mouse_pos);
|
||||||
|
|
|
@ -1,19 +1,17 @@
|
||||||
mod directive;
|
|
||||||
mod event;
|
mod event;
|
||||||
mod functions;
|
mod functions;
|
||||||
mod helpers;
|
mod helpers;
|
||||||
mod state;
|
mod state;
|
||||||
|
|
||||||
pub use directive::*;
|
|
||||||
pub use event::*;
|
pub use event::*;
|
||||||
|
use glyphon::FontSystem;
|
||||||
pub use helpers::{anchor::*, color::*, rect::*, vector::*};
|
pub use helpers::{anchor::*, color::*, rect::*, vector::*};
|
||||||
|
use log::debug;
|
||||||
pub use state::*;
|
pub use state::*;
|
||||||
|
|
||||||
use super::UiState;
|
use super::UiState;
|
||||||
use galactica_content::Content;
|
use galactica_content::Content;
|
||||||
use galactica_system::PlayerDirective;
|
use rhai::{exported_module, Dynamic, Engine};
|
||||||
use glyphon::FontSystem;
|
|
||||||
use rhai::{exported_module, Engine};
|
|
||||||
use std::{cell::RefCell, rc::Rc, sync::Arc};
|
use std::{cell::RefCell, rc::Rc, sync::Arc};
|
||||||
|
|
||||||
pub fn register_into_engine(
|
pub fn register_into_engine(
|
||||||
|
@ -31,27 +29,21 @@ pub fn register_into_engine(
|
||||||
.build_type::<State>()
|
.build_type::<State>()
|
||||||
.build_type::<ShipState>()
|
.build_type::<ShipState>()
|
||||||
.build_type::<SystemObjectState>()
|
.build_type::<SystemObjectState>()
|
||||||
.build_type::<OutfitState>()
|
|
||||||
// Events
|
// Events
|
||||||
.build_type::<MouseClickEvent>()
|
.build_type::<MouseClickEvent>()
|
||||||
.build_type::<MouseHoverEvent>()
|
.build_type::<MouseHoverEvent>()
|
||||||
.build_type::<PlayerShipStateEvent>()
|
.build_type::<PlayerShipStateEvent>()
|
||||||
.build_type::<KeyboardEvent>()
|
|
||||||
.build_type::<ScrollEvent>()
|
|
||||||
// Bigger modules
|
// Bigger modules
|
||||||
.register_type_with_name::<Anchor>("Anchor")
|
.register_type_with_name::<Anchor>("Anchor")
|
||||||
.register_static_module("Anchor", exported_module!(anchor_mod).into())
|
.register_static_module("Anchor", exported_module!(anchor_mod).into());
|
||||||
.register_type_with_name::<PlayerDirective>("PlayerDirective")
|
|
||||||
.register_static_module(
|
|
||||||
"PlayerDirective",
|
|
||||||
exported_module!(player_directive_module).into(),
|
|
||||||
);
|
|
||||||
|
|
||||||
// Extra functions
|
// Extra functions
|
||||||
engine.register_fn("clamp", |x: f32, l: f32, h: f32| x.clamp(l, h));
|
engine.register_fn("print", move |d: Dynamic| {
|
||||||
|
debug!("{:?}", d);
|
||||||
|
});
|
||||||
|
engine.register_fn("clamp", move |x: f32, l: f32, h: f32| x.clamp(l, h));
|
||||||
|
|
||||||
// Modules
|
// Modules
|
||||||
engine.register_static_module("ui", functions::build_ui_module(state_src.clone()).into());
|
|
||||||
engine.register_static_module(
|
engine.register_static_module(
|
||||||
"sprite",
|
"sprite",
|
||||||
functions::build_sprite_module(ct_src.clone(), state_src.clone()).into(),
|
functions::build_sprite_module(ct_src.clone(), state_src.clone()).into(),
|
||||||
|
@ -64,10 +56,7 @@ pub fn register_into_engine(
|
||||||
"radialbar",
|
"radialbar",
|
||||||
functions::build_radialbar_module(state_src.clone()).into(),
|
functions::build_radialbar_module(state_src.clone()).into(),
|
||||||
);
|
);
|
||||||
engine.register_static_module(
|
engine.register_static_module("ui", functions::build_ui_module(state_src.clone()).into());
|
||||||
"scrollbox",
|
|
||||||
functions::build_scrollbox_module(state_src.clone()).into(),
|
|
||||||
);
|
|
||||||
engine.register_static_module(
|
engine.register_static_module(
|
||||||
"conf",
|
"conf",
|
||||||
functions::build_conf_module(state_src.clone()).into(),
|
functions::build_conf_module(state_src.clone()).into(),
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use galactica_content::{Outfit, Ship, SystemObject};
|
use galactica_content::{Ship, SystemObject, SystemObjectHandle};
|
||||||
use galactica_system::{
|
use galactica_system::{
|
||||||
data::{self},
|
data::{self},
|
||||||
phys::{objects::PhysShip, PhysSimShipHandle},
|
phys::{objects::PhysShip, PhysSimShipHandle},
|
||||||
|
@ -19,15 +19,14 @@ pub struct ShipState {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ShipState {
|
impl ShipState {
|
||||||
// All functions passed to rhai MUST be mut,
|
|
||||||
// even getters.
|
|
||||||
fn get_content(&mut self) -> &Ship {
|
fn get_content(&mut self) -> &Ship {
|
||||||
let ship = self
|
let ship = self
|
||||||
.input
|
.input
|
||||||
.phys_img
|
.phys_img
|
||||||
.get_ship(self.ship.as_ref().unwrap())
|
.get_ship(self.ship.as_ref().unwrap())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
ship.ship.get_data().get_content()
|
let handle = ship.ship.get_data().get_content();
|
||||||
|
self.input.ct.get_ship(handle)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_ship(&mut self) -> &PhysShip {
|
fn get_ship(&mut self) -> &PhysShip {
|
||||||
|
@ -49,13 +48,20 @@ impl ShipState {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn landed_on(&mut self) -> SystemObjectState {
|
fn landed_on(&mut self) -> SystemObjectState {
|
||||||
|
let input = self.input.clone();
|
||||||
match self.get_ship().get_data().get_state() {
|
match self.get_ship().get_data().get_state() {
|
||||||
data::ShipState::Landed { target } => {
|
data::ShipState::Landed { target } => {
|
||||||
return SystemObjectState {
|
return SystemObjectState {
|
||||||
object: Some(target.clone()),
|
input,
|
||||||
|
object: Some(*target),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
return SystemObjectState {
|
||||||
|
input,
|
||||||
|
object: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => return SystemObjectState { object: None },
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -83,15 +89,8 @@ impl CustomType for ShipState {
|
||||||
.with_fn("is_collapsing", |s: &mut Self| {
|
.with_fn("is_collapsing", |s: &mut Self| {
|
||||||
s.get_ship().get_data().get_state().is_collapsing()
|
s.get_ship().get_data().get_state().is_collapsing()
|
||||||
})
|
})
|
||||||
.with_fn("display_name", |s: &mut Self| {
|
.with_fn("name", |s: &mut Self| s.get_content().name.clone())
|
||||||
s.get_content().display_name.clone()
|
.with_fn("thumbnail", |s: &mut Self| s.get_content().thumbnail)
|
||||||
})
|
|
||||||
.with_fn("content_index", |s: &mut Self| {
|
|
||||||
s.get_content().display_name.clone()
|
|
||||||
})
|
|
||||||
.with_fn("thumbnail", |s: &mut Self| {
|
|
||||||
s.get_content().thumbnail.clone()
|
|
||||||
})
|
|
||||||
.with_fn("landed_on", |s: &mut Self| s.landed_on())
|
.with_fn("landed_on", |s: &mut Self| s.landed_on())
|
||||||
.with_fn("get_shields", |s: &mut Self| {
|
.with_fn("get_shields", |s: &mut Self| {
|
||||||
s.get_ship().get_data().get_shields()
|
s.get_ship().get_data().get_shields()
|
||||||
|
@ -104,58 +103,28 @@ impl CustomType for ShipState {
|
||||||
s.get_ship().get_data().get_hull()
|
s.get_ship().get_data().get_hull()
|
||||||
})
|
})
|
||||||
.with_fn("get_size", |s: &mut Self| s.get_content().size)
|
.with_fn("get_size", |s: &mut Self| s.get_content().size)
|
||||||
.with_fn("phys_uid", |s: &mut Self| format!("{}", s.get_ship().uid))
|
.with_fn("get_uid", |s: &mut Self| format!("{}", s.get_ship().uid))
|
||||||
.with_fn("get_pos", |s: &mut Self| {
|
.with_fn("get_pos", |s: &mut Self| {
|
||||||
let t = s.get_body().translation();
|
let t = s.get_body().translation();
|
||||||
UiVector::new(t.x, t.y)
|
UiVector::new(t.x, t.y)
|
||||||
})
|
})
|
||||||
.with_fn("get_faction_color", |s: &mut Self| {
|
.with_fn("get_faction_color", |s: &mut Self| {
|
||||||
let h = s.get_ship().get_data().get_faction();
|
let h = s.get_ship().get_data().get_faction();
|
||||||
let c = h.color;
|
let c = s.input.ct.get_faction(h).color;
|
||||||
Color::new(c[0], c[1], c[2], 1.0)
|
Color::new(c[0], c[1], c[2], 1.0)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct OutfitState {
|
|
||||||
outfit: Arc<Outfit>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl OutfitState {}
|
|
||||||
|
|
||||||
impl CustomType for OutfitState {
|
|
||||||
fn build(mut builder: TypeBuilder<Self>) {
|
|
||||||
builder
|
|
||||||
.with_name("OutfitState")
|
|
||||||
.with_fn("display_name", |s: &mut Self| s.outfit.display_name.clone())
|
|
||||||
.with_fn("index", |s: &mut Self| s.outfit.index.to_string())
|
|
||||||
.with_fn("thumbnail", |s: &mut Self| {
|
|
||||||
s.outfit.thumbnail.index.to_string()
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct SystemObjectState {
|
pub struct SystemObjectState {
|
||||||
object: Option<Arc<SystemObject>>,
|
object: Option<SystemObjectHandle>,
|
||||||
|
input: Arc<RenderInput>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SystemObjectState {
|
impl SystemObjectState {
|
||||||
fn outfitter(&mut self) -> Array {
|
fn get_content(&mut self) -> &SystemObject {
|
||||||
let mut a = Array::new();
|
self.input.ct.get_system_object(self.object.unwrap())
|
||||||
for o in &self
|
|
||||||
.object
|
|
||||||
.as_ref()
|
|
||||||
.unwrap()
|
|
||||||
.landable
|
|
||||||
.as_ref()
|
|
||||||
.unwrap()
|
|
||||||
.outfitter
|
|
||||||
{
|
|
||||||
a.push(Dynamic::from(OutfitState { outfit: o.clone() }));
|
|
||||||
}
|
|
||||||
return a;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -163,70 +132,58 @@ impl CustomType for SystemObjectState {
|
||||||
fn build(mut builder: TypeBuilder<Self>) {
|
fn build(mut builder: TypeBuilder<Self>) {
|
||||||
builder
|
builder
|
||||||
.with_name("SystemObjectState")
|
.with_name("SystemObjectState")
|
||||||
.with_fn("outfitter", Self::outfitter)
|
|
||||||
//
|
//
|
||||||
// Get landable name
|
// Get landable name
|
||||||
.with_fn("display_name", |s: &mut Self| {
|
.with_fn("name", |s: &mut Self| {
|
||||||
s.object
|
s.get_content()
|
||||||
|
.name
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.unwrap()
|
.map(|x| x.to_string())
|
||||||
.landable
|
|
||||||
.as_ref()
|
|
||||||
.map(|x| x.display_name.clone())
|
|
||||||
.unwrap_or_else(|| {
|
.unwrap_or_else(|| {
|
||||||
error!("UI called `name()` on a system object which isn't landable");
|
error!("UI called `name()` on a system object which doesn't provide one");
|
||||||
"".to_string()
|
"".to_string()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
//
|
//
|
||||||
// Get landable description
|
// Get landable description
|
||||||
.with_fn("desc", |s: &mut Self| {
|
.with_fn("desc", |s: &mut Self| {
|
||||||
s.object
|
s.get_content()
|
||||||
|
.desc
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.unwrap()
|
.map(|x| x.to_string())
|
||||||
.landable
|
|
||||||
.as_ref()
|
|
||||||
.map(|x| x.desc.clone())
|
|
||||||
.unwrap_or_else(|| {
|
.unwrap_or_else(|| {
|
||||||
error!("UI called `desc()` on a system object which isn't landable");
|
error!("UI called `name()` on a system object which doesn't provide one");
|
||||||
"".to_string()
|
"".to_string()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
//
|
//
|
||||||
// Get landable landscape image
|
// Get landable landscape image
|
||||||
.with_fn("image", |s: &mut Self| {
|
.with_fn("image", |s: &mut Self| {
|
||||||
s.object
|
let handle = s.get_content().image;
|
||||||
.as_ref()
|
if let Some(handle) = handle {
|
||||||
.unwrap()
|
s.input.ct.get_sprite(handle).name.clone()
|
||||||
.landable
|
} else {
|
||||||
.as_ref()
|
error!("UI called `image()` on a system object which doesn't provide one");
|
||||||
.map(|x| x.image.index.to_string())
|
"".to_string()
|
||||||
.unwrap_or_else(|| {
|
}
|
||||||
error!("UI called `image()` on a system object which isn't landable");
|
|
||||||
"".to_string()
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
.with_fn("is_some", |s: &mut Self| s.object.is_some())
|
.with_fn("is_some", |s: &mut Self| s.object.is_some())
|
||||||
.with_fn("is_landable", |s: &mut Self| {
|
.with_fn("==", |a: &mut Self, b: Self| a.object == b.object)
|
||||||
s.object.as_ref().unwrap().landable.is_some()
|
.with_fn("get_size", |s: &mut Self| s.get_content().size)
|
||||||
})
|
.with_fn("get_label", |s: &mut Self| {
|
||||||
.with_fn("==", |a: &mut Self, b: Self| match (&a.object, &b.object) {
|
ImmutableString::from(&s.get_content().label)
|
||||||
(None, _) => false,
|
|
||||||
(_, None) => false,
|
|
||||||
(Some(a), Some(b)) => a.index == b.index,
|
|
||||||
})
|
|
||||||
.with_fn("get_size", |s: &mut Self| s.object.as_ref().unwrap().size)
|
|
||||||
.with_fn("get_index", |s: &mut Self| {
|
|
||||||
ImmutableString::from(s.object.as_ref().unwrap().index.as_str())
|
|
||||||
})
|
})
|
||||||
.with_fn("get_angle", |s: &mut Self| {
|
.with_fn("get_angle", |s: &mut Self| {
|
||||||
to_degrees(s.object.as_ref().unwrap().angle)
|
to_degrees(s.get_content().angle)
|
||||||
})
|
})
|
||||||
.with_fn("get_pos", |s: &mut Self| {
|
.with_fn("get_pos", |s: &mut Self| {
|
||||||
let t = s.object.as_ref().unwrap().pos;
|
let t = s.get_content().pos;
|
||||||
UiVector::new(t.x, t.y)
|
UiVector::new(t.x, t.y)
|
||||||
})
|
})
|
||||||
.with_fn("get_pos_z", |s: &mut Self| s.object.as_ref().unwrap().pos.z);
|
.with_fn("get_pos_z", |s: &mut Self| {
|
||||||
|
let t = s.get_content().pos;
|
||||||
|
t.z
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -268,9 +225,11 @@ impl State {
|
||||||
|
|
||||||
pub fn objects(&mut self) -> Array {
|
pub fn objects(&mut self) -> Array {
|
||||||
let mut a = Array::new();
|
let mut a = Array::new();
|
||||||
for (_, o) in &self.input.current_system.objects {
|
let s = self.input.current_system;
|
||||||
|
for o in &self.input.ct.get_system(s).objects {
|
||||||
a.push(Dynamic::from(SystemObjectState {
|
a.push(Dynamic::from(SystemObjectState {
|
||||||
object: Some(o.clone()),
|
input: self.input.clone(),
|
||||||
|
object: Some(o.handle),
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
return a;
|
return a;
|
||||||
|
@ -284,6 +243,7 @@ impl CustomType for State {
|
||||||
.with_fn("player_ship", Self::player_ship)
|
.with_fn("player_ship", Self::player_ship)
|
||||||
.with_fn("ships", Self::ships)
|
.with_fn("ships", Self::ships)
|
||||||
.with_fn("objects", Self::objects)
|
.with_fn("objects", Self::objects)
|
||||||
.with_fn("window_aspect", |s: &mut Self| s.window_aspect);
|
.with_fn("window_aspect", |s: &mut Self| s.window_aspect)
|
||||||
|
.with_fn("camera_zoom", |s: &mut Self| s.input.camera_zoom);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,35 +0,0 @@
|
||||||
use nalgebra::Vector2;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub(crate) struct Camera {
|
|
||||||
/// The position of the camera, in game units
|
|
||||||
pos: Vector2<f32>,
|
|
||||||
|
|
||||||
/// The height of the viewport, in game units.
|
|
||||||
zoom: f32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Camera {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
Self {
|
|
||||||
pos: Vector2::new(0.0, 0.0),
|
|
||||||
zoom: 500.0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_pos(&mut self, pos: Vector2<f32>) {
|
|
||||||
self.pos = pos
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_pos(&self) -> Vector2<f32> {
|
|
||||||
self.pos
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_zoom(&mut self, zoom: f32) {
|
|
||||||
self.zoom = zoom
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_zoom(&self) -> f32 {
|
|
||||||
self.zoom
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,13 +1,10 @@
|
||||||
use glyphon::{Attrs, Buffer, Color, Family, FontSystem, Metrics, Shaping, TextBounds};
|
use glyphon::{Attrs, Buffer, Color, Family, FontSystem, Metrics, Shaping, TextArea, TextBounds};
|
||||||
use std::rc::Rc;
|
|
||||||
use winit::window::Window;
|
use winit::window::Window;
|
||||||
|
|
||||||
use crate::{RenderInput, RenderState};
|
use crate::{RenderInput, RenderState};
|
||||||
|
|
||||||
use super::OwnedTextArea;
|
|
||||||
|
|
||||||
pub(crate) struct FpsIndicator {
|
pub(crate) struct FpsIndicator {
|
||||||
buffer: Rc<Buffer>,
|
buffer: Buffer,
|
||||||
update_counter: u32,
|
update_counter: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,7 +21,7 @@ impl FpsIndicator {
|
||||||
);
|
);
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
buffer: Rc::new(buffer),
|
buffer,
|
||||||
update_counter: 0,
|
update_counter: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,31 +29,29 @@ impl FpsIndicator {
|
||||||
|
|
||||||
impl FpsIndicator {
|
impl FpsIndicator {
|
||||||
pub fn step(&mut self, input: &RenderInput, font: &mut FontSystem) {
|
pub fn step(&mut self, input: &RenderInput, font: &mut FontSystem) {
|
||||||
let buffer = Rc::get_mut(&mut self.buffer).unwrap();
|
|
||||||
|
|
||||||
if self.update_counter > 0 {
|
if self.update_counter > 0 {
|
||||||
self.update_counter -= 1;
|
self.update_counter -= 1;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.update_counter = 100;
|
self.update_counter = 100;
|
||||||
|
|
||||||
buffer.set_text(
|
self.buffer.set_text(
|
||||||
font,
|
font,
|
||||||
&input.timing.get_string(),
|
&input.timing.get_string(),
|
||||||
Attrs::new().family(Family::Monospace),
|
Attrs::new().family(Family::Monospace),
|
||||||
Shaping::Basic,
|
Shaping::Basic,
|
||||||
);
|
);
|
||||||
buffer.shape_until_scroll(font);
|
self.buffer.shape_until_scroll(font);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b: 'a> FpsIndicator {
|
impl<'a, 'b: 'a> FpsIndicator {
|
||||||
pub fn get_textarea(&'b self, input: &RenderInput, _window: &Window) -> OwnedTextArea {
|
pub fn get_textarea(&'b self, input: &RenderInput, _window: &Window) -> TextArea<'a> {
|
||||||
OwnedTextArea {
|
TextArea {
|
||||||
buffer: self.buffer.clone(),
|
buffer: &self.buffer,
|
||||||
left: 10.0,
|
left: 10.0,
|
||||||
top: 400.0,
|
top: 400.0,
|
||||||
scale: input.ct.config.ui_scale,
|
scale: input.ct.get_config().ui_scale,
|
||||||
bounds: TextBounds {
|
bounds: TextBounds {
|
||||||
left: 10,
|
left: 10,
|
||||||
top: 400,
|
top: 400,
|
||||||
|
|
|
@ -1,38 +1,9 @@
|
||||||
mod fpsindicator;
|
mod fpsindicator;
|
||||||
mod radialbar;
|
mod radialbar;
|
||||||
mod scrollbox;
|
|
||||||
mod sprite;
|
mod sprite;
|
||||||
mod textbox;
|
mod textbox;
|
||||||
|
|
||||||
pub(super) use fpsindicator::*;
|
pub(super) use fpsindicator::*;
|
||||||
pub(super) use radialbar::*;
|
pub(super) use radialbar::*;
|
||||||
pub(super) use scrollbox::*;
|
|
||||||
pub(super) use sprite::*;
|
pub(super) use sprite::*;
|
||||||
pub(super) use textbox::*;
|
pub(super) use textbox::*;
|
||||||
|
|
||||||
use glyphon::{Buffer, Color, TextArea, TextBounds};
|
|
||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
/// A hack that lets us easily construct TextAreas
|
|
||||||
/// for [`UiTextBox`]es wrapped in Rcs.
|
|
||||||
pub struct OwnedTextArea {
|
|
||||||
pub buffer: Rc<Buffer>,
|
|
||||||
pub left: f32,
|
|
||||||
pub top: f32,
|
|
||||||
pub scale: f32,
|
|
||||||
pub bounds: TextBounds,
|
|
||||||
pub default_color: Color,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl OwnedTextArea {
|
|
||||||
pub fn get_textarea(&self) -> TextArea {
|
|
||||||
TextArea {
|
|
||||||
buffer: &self.buffer,
|
|
||||||
top: self.top,
|
|
||||||
left: self.left,
|
|
||||||
scale: self.scale,
|
|
||||||
bounds: self.bounds,
|
|
||||||
default_color: self.default_color,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -6,7 +6,7 @@ use super::super::api::Rect;
|
||||||
use crate::{ui::api::Color, vertexbuffer::types::RadialBarInstance, RenderInput, RenderState};
|
use crate::{ui::api::Color, vertexbuffer::types::RadialBarInstance, RenderInput, RenderState};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct UiRadialBar {
|
pub struct RadialBar {
|
||||||
pub name: ImmutableString,
|
pub name: ImmutableString,
|
||||||
rect: Rect,
|
rect: Rect,
|
||||||
stroke: f32,
|
stroke: f32,
|
||||||
|
@ -14,7 +14,7 @@ pub struct UiRadialBar {
|
||||||
progress: f32,
|
progress: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UiRadialBar {
|
impl RadialBar {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
name: ImmutableString,
|
name: ImmutableString,
|
||||||
stroke: f32,
|
stroke: f32,
|
||||||
|
@ -38,14 +38,16 @@ impl UiRadialBar {
|
||||||
pub fn push_to_buffer(&self, input: &RenderInput, state: &mut RenderState) {
|
pub fn push_to_buffer(&self, input: &RenderInput, state: &mut RenderState) {
|
||||||
let rect = self
|
let rect = self
|
||||||
.rect
|
.rect
|
||||||
.to_centered(&state.window, input.ct.config.ui_scale);
|
.to_centered(&state.window, input.ct.get_config().ui_scale);
|
||||||
|
|
||||||
state.push_radialbar_buffer(RadialBarInstance {
|
state.push_radialbar_buffer(RadialBarInstance {
|
||||||
position: [rect.pos.x, rect.pos.y],
|
position: [rect.pos.x, rect.pos.y],
|
||||||
diameter: rect.dim.x.min(rect.dim.y),
|
diameter: rect.dim.x.min(rect.dim.y),
|
||||||
stroke: self.stroke * input.ct.config.ui_scale,
|
stroke: self.stroke * input.ct.get_config().ui_scale,
|
||||||
color: self.color.as_array(),
|
color: self.color.as_array(),
|
||||||
angle: self.progress * TAU,
|
angle: self.progress * TAU,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn step(&mut self, _input: &RenderInput, _state: &mut RenderState) {}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,136 +0,0 @@
|
||||||
use nalgebra::Vector2;
|
|
||||||
use rhai::{Dynamic, ImmutableString};
|
|
||||||
use std::{cell::RefCell, collections::HashMap, rc::Rc};
|
|
||||||
use winit::window::Window;
|
|
||||||
|
|
||||||
use super::{super::api::Rect, OwnedTextArea};
|
|
||||||
use crate::{ui::UiElement, InputEvent, RenderInput, RenderState};
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct UiScrollbox {
|
|
||||||
pub name: ImmutableString,
|
|
||||||
pub rect: Rect,
|
|
||||||
pub offset: Vector2<f32>,
|
|
||||||
pub elements: HashMap<ImmutableString, Rc<RefCell<UiElement>>>,
|
|
||||||
|
|
||||||
has_mouse: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl UiScrollbox {
|
|
||||||
pub fn new(name: ImmutableString, rect: Rect) -> Self {
|
|
||||||
Self {
|
|
||||||
name,
|
|
||||||
rect,
|
|
||||||
elements: HashMap::new(),
|
|
||||||
offset: Vector2::new(0.0, 0.0),
|
|
||||||
has_mouse: false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn add_element(&mut self, e: Rc<RefCell<UiElement>>) {
|
|
||||||
let name = e.borrow().get_name().clone();
|
|
||||||
self.elements.insert(name, e);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn remove_element(&mut self, sprite: &ImmutableString) {
|
|
||||||
self.elements.remove(sprite);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn step(&mut self, t: f32) {
|
|
||||||
for (_name, e) in &self.elements {
|
|
||||||
match &mut *e.clone().borrow_mut() {
|
|
||||||
UiElement::Sprite(sprite) => sprite.step(t),
|
|
||||||
UiElement::RadialBar(_) => {}
|
|
||||||
UiElement::Text(..) => {}
|
|
||||||
UiElement::Scrollbox(..) => {}
|
|
||||||
UiElement::SubElement { .. } => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn handle_event(
|
|
||||||
&mut self,
|
|
||||||
input: &RenderInput,
|
|
||||||
state: &mut RenderState,
|
|
||||||
event: &InputEvent,
|
|
||||||
) -> Option<Dynamic> {
|
|
||||||
let r = self
|
|
||||||
.rect
|
|
||||||
.to_centered(&state.window, input.ct.config.ui_scale);
|
|
||||||
|
|
||||||
// TODO: handle only if used in event()
|
|
||||||
// i.e, scrollable sprites shouldn't break scrollboxes
|
|
||||||
// First, check if this event is captured by any sub-elements
|
|
||||||
for (_, e) in &mut self.elements {
|
|
||||||
let arg = match &mut *e.borrow_mut() {
|
|
||||||
UiElement::Sprite(sprite) => sprite.handle_event(&input, state, &event),
|
|
||||||
UiElement::Scrollbox(sbox) => sbox.handle_event(&input, state, &event),
|
|
||||||
|
|
||||||
UiElement::RadialBar(_) | UiElement::Text(..) => None,
|
|
||||||
|
|
||||||
// Subelements are intentionally skipped,
|
|
||||||
// they should be handled by their parent's `handle_event` method.
|
|
||||||
UiElement::SubElement { .. } => None,
|
|
||||||
};
|
|
||||||
|
|
||||||
if arg.is_some() {
|
|
||||||
return arg;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If no inner events were captured, handle self events.
|
|
||||||
match event {
|
|
||||||
InputEvent::MouseMove(pos) => {
|
|
||||||
if r.contains_mouse(state, pos) && !self.has_mouse {
|
|
||||||
self.has_mouse = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if !r.contains_mouse(state, pos) && self.has_mouse {
|
|
||||||
self.has_mouse = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
InputEvent::Scroll(x) => {
|
|
||||||
if self.has_mouse {
|
|
||||||
self.offset.y -= x;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_ => return None,
|
|
||||||
}
|
|
||||||
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl UiScrollbox {
|
|
||||||
pub fn push_to_buffer(&self, input: &RenderInput, state: &mut RenderState) {
|
|
||||||
for (_name, e) in &self.elements {
|
|
||||||
match &*e.clone().borrow() {
|
|
||||||
UiElement::Sprite(sprite) => {
|
|
||||||
sprite.push_to_buffer_with_offset(input, state, self.offset)
|
|
||||||
}
|
|
||||||
UiElement::RadialBar(..) => {}
|
|
||||||
UiElement::Text(..) => {}
|
|
||||||
UiElement::Scrollbox(..) => {}
|
|
||||||
UiElement::SubElement { .. } => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: don't allocate here
|
|
||||||
impl<'a> UiScrollbox {
|
|
||||||
pub fn get_textareas(&'a self, input: &RenderInput, window: &Window) -> Vec<OwnedTextArea> {
|
|
||||||
let mut v = Vec::with_capacity(32);
|
|
||||||
for e in self.elements.values() {
|
|
||||||
match &*e.clone().borrow() {
|
|
||||||
UiElement::Text(x) => {
|
|
||||||
v.push(x.get_textarea_with_offset(input, window, self.offset))
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,18 +1,15 @@
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use super::super::api::Rect;
|
use super::super::api::Rect;
|
||||||
use crate::{
|
use crate::{
|
||||||
ui::api::{Color, MouseClickEvent, MouseHoverEvent},
|
ui::{api::Color, event::Event},
|
||||||
vertexbuffer::types::UiInstance,
|
vertexbuffer::types::UiInstance,
|
||||||
InputEvent, RenderInput, RenderState,
|
RenderInput, RenderState,
|
||||||
};
|
};
|
||||||
use galactica_content::{Sprite, SpriteAutomaton};
|
use galactica_content::{Content, SpriteAutomaton, SpriteHandle};
|
||||||
use galactica_util::to_radians;
|
use galactica_util::to_radians;
|
||||||
use nalgebra::Vector2;
|
use rhai::ImmutableString;
|
||||||
use rhai::{Dynamic, ImmutableString};
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct UiSprite {
|
pub struct Sprite {
|
||||||
pub anim: SpriteAutomaton,
|
pub anim: SpriteAutomaton,
|
||||||
pub name: ImmutableString,
|
pub name: ImmutableString,
|
||||||
|
|
||||||
|
@ -24,29 +21,32 @@ pub struct UiSprite {
|
||||||
preserve_aspect: bool,
|
preserve_aspect: bool,
|
||||||
|
|
||||||
rect: Rect,
|
rect: Rect,
|
||||||
mask: Option<Arc<Sprite>>,
|
mask: Option<SpriteHandle>,
|
||||||
color: Color,
|
color: Color,
|
||||||
|
|
||||||
|
/// If true, ignore mouse events until click is released
|
||||||
|
waiting_for_release: bool,
|
||||||
has_mouse: bool,
|
has_mouse: bool,
|
||||||
has_click: bool,
|
has_click: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UiSprite {
|
impl Sprite {
|
||||||
pub fn new(name: ImmutableString, sprite: Arc<Sprite>, rect: Rect) -> Self {
|
pub fn new(ct: &Content, name: ImmutableString, sprite: SpriteHandle, rect: Rect) -> Self {
|
||||||
Self {
|
Self {
|
||||||
name,
|
name,
|
||||||
anim: SpriteAutomaton::new(sprite),
|
anim: SpriteAutomaton::new(&ct, sprite),
|
||||||
rect,
|
rect,
|
||||||
angle: 0.0,
|
angle: 0.0,
|
||||||
color: Color::new(1.0, 1.0, 1.0, 1.0),
|
color: Color::new(1.0, 1.0, 1.0, 1.0),
|
||||||
mask: None,
|
mask: None,
|
||||||
has_mouse: false,
|
has_mouse: false,
|
||||||
has_click: false,
|
has_click: false,
|
||||||
|
waiting_for_release: false,
|
||||||
preserve_aspect: false,
|
preserve_aspect: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_mask(&mut self, mask: Option<Arc<Sprite>>) {
|
pub fn set_mask(&mut self, mask: Option<SpriteHandle>) {
|
||||||
self.mask = mask;
|
self.mask = mask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,27 +65,15 @@ impl UiSprite {
|
||||||
pub fn set_preserve_aspect(&mut self, preserve_aspect: bool) {
|
pub fn set_preserve_aspect(&mut self, preserve_aspect: bool) {
|
||||||
self.preserve_aspect = preserve_aspect;
|
self.preserve_aspect = preserve_aspect;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl UiSprite {
|
|
||||||
pub fn push_to_buffer(&self, input: &RenderInput, state: &mut RenderState) {
|
pub fn push_to_buffer(&self, input: &RenderInput, state: &mut RenderState) {
|
||||||
self.push_to_buffer_with_offset(input, state, Vector2::new(0.0, 0.0))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn push_to_buffer_with_offset(
|
|
||||||
&self,
|
|
||||||
input: &RenderInput,
|
|
||||||
state: &mut RenderState,
|
|
||||||
offset: Vector2<f32>,
|
|
||||||
) {
|
|
||||||
let mut rect = self
|
let mut rect = self
|
||||||
.rect
|
.rect
|
||||||
.to_centered(&state.window, input.ct.config.ui_scale);
|
.to_centered(&state.window, input.ct.get_config().ui_scale);
|
||||||
rect.pos += offset;
|
|
||||||
|
|
||||||
if self.preserve_aspect {
|
if self.preserve_aspect {
|
||||||
let rect_aspect = rect.dim.x / rect.dim.y;
|
let rect_aspect = rect.dim.x / rect.dim.y;
|
||||||
let sprite_aspect = self.anim.get_sprite().aspect;
|
let sprite_aspect = input.ct.get_sprite(self.anim.get_sprite()).aspect;
|
||||||
|
|
||||||
// "wide rect" case => match height, reduce width
|
// "wide rect" case => match height, reduce width
|
||||||
if rect_aspect > sprite_aspect {
|
if rect_aspect > sprite_aspect {
|
||||||
|
@ -109,74 +97,64 @@ impl UiSprite {
|
||||||
texture_fade: anim_state.fade,
|
texture_fade: anim_state.fade,
|
||||||
mask_index: self
|
mask_index: self
|
||||||
.mask
|
.mask
|
||||||
.as_ref()
|
|
||||||
.map(|x| {
|
.map(|x| {
|
||||||
let texture_b = x.get_first_frame(); // TODO: animate?
|
let sprite = input.ct.get_sprite(x);
|
||||||
|
let texture_b = sprite.get_first_frame(); // TODO: animate?
|
||||||
[1, texture_b]
|
[1, texture_b]
|
||||||
})
|
})
|
||||||
.unwrap_or([0, 0]),
|
.unwrap_or([0, 0]),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_event(
|
pub fn check_events(&mut self, input: &RenderInput, state: &mut RenderState) -> Event {
|
||||||
&mut self,
|
|
||||||
input: &RenderInput,
|
|
||||||
state: &mut RenderState,
|
|
||||||
event: &InputEvent,
|
|
||||||
) -> Option<Dynamic> {
|
|
||||||
let r = self
|
let r = self
|
||||||
.rect
|
.rect
|
||||||
.to_centered(&state.window, input.ct.config.ui_scale);
|
.to_centered(&state.window, input.ct.get_config().ui_scale);
|
||||||
|
|
||||||
|
if self.waiting_for_release && self.has_mouse && !input.player.input.pressed_leftclick() {
|
||||||
|
self.waiting_for_release = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if !self.waiting_for_release
|
||||||
|
&& self.has_mouse
|
||||||
|
&& !self.has_click
|
||||||
|
&& input.player.input.pressed_leftclick()
|
||||||
|
{
|
||||||
|
self.has_click = true;
|
||||||
|
return Event::MouseClick;
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.has_mouse && self.has_click && !input.player.input.pressed_leftclick() {
|
||||||
|
self.has_click = false;
|
||||||
|
return Event::MouseRelease;
|
||||||
|
}
|
||||||
|
|
||||||
// Release mouse when cursor leaves box
|
// Release mouse when cursor leaves box
|
||||||
if self.has_click && !self.has_mouse {
|
if self.has_click && !self.has_mouse {
|
||||||
self.has_click = false;
|
self.has_click = false;
|
||||||
|
return Event::MouseRelease;
|
||||||
}
|
}
|
||||||
|
|
||||||
match event {
|
if r.contains_mouse(input, state) && !self.has_mouse {
|
||||||
InputEvent::MouseMove(pos) => {
|
if input.player.input.pressed_leftclick() {
|
||||||
if r.contains_mouse(state, pos) && !self.has_mouse {
|
// If we're holding click when the cursor enters,
|
||||||
self.has_mouse = true;
|
// don't trigger the `Click` event.
|
||||||
return Some(Dynamic::from(MouseHoverEvent {
|
self.waiting_for_release = true;
|
||||||
enter: true,
|
|
||||||
element: self.name.clone(),
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
if !r.contains_mouse(state, pos) && self.has_mouse {
|
|
||||||
self.has_mouse = false;
|
|
||||||
return Some(Dynamic::from(MouseHoverEvent {
|
|
||||||
enter: false,
|
|
||||||
element: self.name.clone(),
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
self.has_mouse = true;
|
||||||
InputEvent::MouseLeftClick(pressed) => {
|
return Event::MouseHover;
|
||||||
if self.has_mouse && !self.has_click && *pressed {
|
|
||||||
self.has_click = true;
|
|
||||||
return Some(Dynamic::from(MouseClickEvent {
|
|
||||||
down: true,
|
|
||||||
element: self.name.clone(),
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
if self.has_mouse && self.has_click && !*pressed {
|
|
||||||
self.has_click = false;
|
|
||||||
return Some(Dynamic::from(MouseClickEvent {
|
|
||||||
down: false,
|
|
||||||
element: self.name.clone(),
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_ => return None,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return None;
|
if !r.contains_mouse(input, state) && self.has_mouse {
|
||||||
|
self.waiting_for_release = false;
|
||||||
|
self.has_mouse = false;
|
||||||
|
return Event::MouseUnhover;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Event::None;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn step(&mut self, t: f32) {
|
pub fn step(&mut self, input: &RenderInput, _state: &mut RenderState) {
|
||||||
self.anim.step(t);
|
self.anim.step(&input.ct, input.time_since_last_run);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,28 +1,27 @@
|
||||||
use glyphon::{
|
use glyphon::{
|
||||||
cosmic_text::Align, Attrs, AttrsOwned, Buffer, Color, FamilyOwned, FontSystem, Metrics,
|
cosmic_text::Align, Attrs, AttrsOwned, Buffer, Color, FamilyOwned, FontSystem, Metrics,
|
||||||
Shaping, Style, TextBounds, Weight,
|
Shaping, Style, TextArea, TextBounds, Weight,
|
||||||
};
|
};
|
||||||
use nalgebra::Vector2;
|
use nalgebra::Vector2;
|
||||||
use rhai::ImmutableString;
|
use rhai::ImmutableString;
|
||||||
use std::rc::Rc;
|
|
||||||
use winit::window::Window;
|
use winit::window::Window;
|
||||||
|
|
||||||
use super::{super::api::Rect, OwnedTextArea};
|
use super::super::api::Rect;
|
||||||
use crate::{ui::api, RenderInput};
|
use crate::{ui::api, RenderInput};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct UiTextBox {
|
pub struct TextBox {
|
||||||
pub name: ImmutableString,
|
pub name: ImmutableString,
|
||||||
|
|
||||||
text: String,
|
text: String,
|
||||||
justify: Align,
|
justify: Align,
|
||||||
rect: Rect,
|
rect: Rect,
|
||||||
buffer: Rc<Buffer>,
|
buffer: Buffer,
|
||||||
color: api::Color,
|
color: api::Color,
|
||||||
attrs: AttrsOwned,
|
attrs: AttrsOwned,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UiTextBox {
|
impl TextBox {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
font: &mut FontSystem,
|
font: &mut FontSystem,
|
||||||
name: ImmutableString,
|
name: ImmutableString,
|
||||||
|
@ -39,7 +38,7 @@ impl UiTextBox {
|
||||||
Self {
|
Self {
|
||||||
name,
|
name,
|
||||||
rect,
|
rect,
|
||||||
buffer: Rc::new(buffer),
|
buffer,
|
||||||
color,
|
color,
|
||||||
justify: Align::Left,
|
justify: Align::Left,
|
||||||
attrs: AttrsOwned::new(Attrs::new()),
|
attrs: AttrsOwned::new(Attrs::new()),
|
||||||
|
@ -48,14 +47,14 @@ impl UiTextBox {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reflow(&mut self, font: &mut FontSystem) {
|
fn reflow(&mut self, font: &mut FontSystem) {
|
||||||
let buffer = Rc::get_mut(&mut self.buffer).unwrap();
|
self.buffer
|
||||||
buffer.set_text(font, &self.text, self.attrs.as_attrs(), Shaping::Advanced);
|
.set_text(font, &self.text, self.attrs.as_attrs(), Shaping::Advanced);
|
||||||
|
|
||||||
for l in &mut buffer.lines {
|
for l in &mut self.buffer.lines {
|
||||||
l.set_align(Some(self.justify));
|
l.set_align(Some(self.justify));
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer.shape_until_scroll(font);
|
self.buffer.shape_until_scroll(font);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_text(&mut self, font: &mut FontSystem, text: &str) {
|
pub fn set_text(&mut self, font: &mut FontSystem, text: &str) {
|
||||||
|
@ -85,19 +84,11 @@ impl UiTextBox {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b: 'a> UiTextBox {
|
impl<'a, 'b: 'a> TextBox {
|
||||||
pub fn get_textarea(&'b self, input: &RenderInput, window: &Window) -> OwnedTextArea {
|
pub fn get_textarea(&'b self, input: &RenderInput, window: &Window) -> TextArea<'a> {
|
||||||
self.get_textarea_with_offset(input, window, Vector2::new(0.0, 0.0))
|
let rect = self
|
||||||
}
|
.rect
|
||||||
|
.to_centered(window, input.ct.get_config().ui_scale);
|
||||||
pub fn get_textarea_with_offset(
|
|
||||||
&'b self,
|
|
||||||
input: &RenderInput,
|
|
||||||
window: &Window,
|
|
||||||
offset: Vector2<f32>,
|
|
||||||
) -> OwnedTextArea {
|
|
||||||
let mut rect = self.rect.to_centered(window, input.ct.config.ui_scale);
|
|
||||||
rect.pos += offset;
|
|
||||||
|
|
||||||
// Glypon works with physical pixels, so we must do some conversion
|
// Glypon works with physical pixels, so we must do some conversion
|
||||||
let fac = window.scale_factor() as f32;
|
let fac = window.scale_factor() as f32;
|
||||||
|
@ -108,11 +99,11 @@ impl<'a, 'b: 'a> UiTextBox {
|
||||||
let corner_sw = corner_ne + rect.dim * fac;
|
let corner_sw = corner_ne + rect.dim * fac;
|
||||||
let c = self.color.as_array_u8();
|
let c = self.color.as_array_u8();
|
||||||
|
|
||||||
OwnedTextArea {
|
TextArea {
|
||||||
buffer: self.buffer.clone(),
|
buffer: &self.buffer,
|
||||||
top: corner_ne.y,
|
top: corner_ne.y,
|
||||||
left: corner_ne.x,
|
left: corner_ne.x,
|
||||||
scale: input.ct.config.ui_scale,
|
scale: input.ct.get_config().ui_scale,
|
||||||
bounds: TextBounds {
|
bounds: TextBounds {
|
||||||
top: (corner_ne.y) as i32,
|
top: (corner_ne.y) as i32,
|
||||||
bottom: (corner_sw.y) as i32,
|
bottom: (corner_sw.y) as i32,
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
#[derive(Debug, Copy, Clone)]
|
||||||
|
pub enum Event {
|
||||||
|
None,
|
||||||
|
MouseClick,
|
||||||
|
MouseRelease,
|
||||||
|
MouseHover,
|
||||||
|
MouseUnhover,
|
||||||
|
}
|
|
@ -1,17 +1,20 @@
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use galactica_content::Content;
|
use galactica_content::Content;
|
||||||
use galactica_system::{phys::PhysSimShipHandle, PlayerDirective};
|
use galactica_system::phys::PhysSimShipHandle;
|
||||||
use galactica_util::rhai_error_to_anyhow;
|
use galactica_util::rhai_error_to_anyhow;
|
||||||
use log::{debug, error};
|
use log::debug;
|
||||||
use rhai::{Dynamic, Engine, ImmutableString, Scope};
|
use rhai::{
|
||||||
|
packages::{BasicArrayPackage, BasicStringPackage, LogicPackage, MoreStringPackage, Package},
|
||||||
|
Dynamic, Engine, ImmutableString, Scope,
|
||||||
|
};
|
||||||
use std::{cell::RefCell, num::NonZeroU32, rc::Rc, sync::Arc};
|
use std::{cell::RefCell, num::NonZeroU32, rc::Rc, sync::Arc};
|
||||||
use winit::event::VirtualKeyCode;
|
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
api::{self, KeyboardEvent, PlayerShipStateEvent, ScrollEvent},
|
api::{self, MouseClickEvent, MouseHoverEvent, PlayerShipStateEvent},
|
||||||
|
event::Event,
|
||||||
UiConfig, UiElement, UiState,
|
UiConfig, UiElement, UiState,
|
||||||
};
|
};
|
||||||
use crate::{ui::api::State, InputEvent, RenderInput, RenderState};
|
use crate::{ui::api::State, RenderInput, RenderState};
|
||||||
|
|
||||||
pub(crate) struct UiScriptExecutor {
|
pub(crate) struct UiScriptExecutor {
|
||||||
engine: Engine,
|
engine: Engine,
|
||||||
|
@ -28,8 +31,14 @@ impl UiScriptExecutor {
|
||||||
let scope = Scope::new();
|
let scope = Scope::new();
|
||||||
let elements = Rc::new(RefCell::new(UiState::new(ct.clone(), state)));
|
let elements = Rc::new(RefCell::new(UiState::new(ct.clone(), state)));
|
||||||
|
|
||||||
// TODO: document all functions rhai provides
|
let mut engine = Engine::new_raw();
|
||||||
let mut engine = Engine::new();
|
|
||||||
|
// Required for array iteration
|
||||||
|
// We may need to add more packages here later.
|
||||||
|
engine.register_global_module(BasicArrayPackage::new().as_shared_module());
|
||||||
|
engine.register_global_module(LogicPackage::new().as_shared_module());
|
||||||
|
engine.register_global_module(BasicStringPackage::new().as_shared_module());
|
||||||
|
engine.register_global_module(MoreStringPackage::new().as_shared_module());
|
||||||
|
|
||||||
engine.set_max_expr_depths(0, 0);
|
engine.set_max_expr_depths(0, 0);
|
||||||
// Enables custom operators
|
// Enables custom operators
|
||||||
|
@ -55,129 +64,6 @@ impl UiScriptExecutor {
|
||||||
(*self.state).borrow().config.clone()
|
(*self.state).borrow().config.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn process_input(
|
|
||||||
&mut self,
|
|
||||||
state: &mut RenderState,
|
|
||||||
input: Arc<RenderInput>,
|
|
||||||
event: InputEvent,
|
|
||||||
) -> Result<PlayerDirective> {
|
|
||||||
let current_scene = (*self.state).borrow().get_scene().clone();
|
|
||||||
if current_scene.is_none() {
|
|
||||||
return Ok(PlayerDirective::None);
|
|
||||||
}
|
|
||||||
let mut arg: Option<Dynamic> = None;
|
|
||||||
|
|
||||||
// First, check if this event is captured by any ui elements.
|
|
||||||
for (_, e) in &mut self.state.borrow_mut().elements {
|
|
||||||
arg = match e {
|
|
||||||
UiElement::Sprite(sprite) => sprite.handle_event(&input, state, &event),
|
|
||||||
UiElement::Scrollbox(sbox) => sbox.handle_event(&input, state, &event),
|
|
||||||
|
|
||||||
UiElement::RadialBar(_) | UiElement::Text(..) => None,
|
|
||||||
|
|
||||||
// Subelements are intentionally skipped,
|
|
||||||
// they should be handled by their parent's `handle_event` method.
|
|
||||||
UiElement::SubElement { .. } => None,
|
|
||||||
};
|
|
||||||
|
|
||||||
if arg.is_some() {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If nothing was caught, check global events
|
|
||||||
if arg.is_none() {
|
|
||||||
arg = match event {
|
|
||||||
InputEvent::Scroll(val) => Some(Dynamic::from(ScrollEvent { val })),
|
|
||||||
InputEvent::Keyboard { down, key } => {
|
|
||||||
let str = match key {
|
|
||||||
VirtualKeyCode::A => Some("A"),
|
|
||||||
VirtualKeyCode::B => Some("B"),
|
|
||||||
VirtualKeyCode::C => Some("C"),
|
|
||||||
VirtualKeyCode::D => Some("D"),
|
|
||||||
VirtualKeyCode::E => Some("E"),
|
|
||||||
VirtualKeyCode::F => Some("F"),
|
|
||||||
VirtualKeyCode::G => Some("G"),
|
|
||||||
VirtualKeyCode::H => Some("H"),
|
|
||||||
VirtualKeyCode::I => Some("I"),
|
|
||||||
VirtualKeyCode::J => Some("J"),
|
|
||||||
VirtualKeyCode::K => Some("K"),
|
|
||||||
VirtualKeyCode::L => Some("L"),
|
|
||||||
VirtualKeyCode::M => Some("M"),
|
|
||||||
VirtualKeyCode::N => Some("N"),
|
|
||||||
VirtualKeyCode::O => Some("O"),
|
|
||||||
VirtualKeyCode::P => Some("P"),
|
|
||||||
VirtualKeyCode::Q => Some("Q"),
|
|
||||||
VirtualKeyCode::R => Some("R"),
|
|
||||||
VirtualKeyCode::S => Some("S"),
|
|
||||||
VirtualKeyCode::T => Some("T"),
|
|
||||||
VirtualKeyCode::U => Some("U"),
|
|
||||||
VirtualKeyCode::V => Some("V"),
|
|
||||||
VirtualKeyCode::W => Some("W"),
|
|
||||||
VirtualKeyCode::X => Some("X"),
|
|
||||||
VirtualKeyCode::Y => Some("Y"),
|
|
||||||
VirtualKeyCode::Z => Some("Z"),
|
|
||||||
VirtualKeyCode::Up => Some("up"),
|
|
||||||
VirtualKeyCode::Down => Some("down"),
|
|
||||||
VirtualKeyCode::Left => Some("left"),
|
|
||||||
VirtualKeyCode::Right => Some("right"),
|
|
||||||
VirtualKeyCode::Space => Some("space"),
|
|
||||||
_ => None,
|
|
||||||
};
|
|
||||||
if let Some(str) = str {
|
|
||||||
Some(Dynamic::from(KeyboardEvent {
|
|
||||||
down,
|
|
||||||
key: ImmutableString::from(str),
|
|
||||||
}))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => None,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(arg) = arg {
|
|
||||||
self.run_event_callback(state, input, arg)
|
|
||||||
} else {
|
|
||||||
return Ok(PlayerDirective::None);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn run_event_callback(
|
|
||||||
&mut self,
|
|
||||||
state: &mut RenderState,
|
|
||||||
input: Arc<RenderInput>,
|
|
||||||
arg: Dynamic,
|
|
||||||
) -> Result<PlayerDirective> {
|
|
||||||
let current_scene = (*self.state).borrow().get_scene().clone();
|
|
||||||
if current_scene.is_none() {
|
|
||||||
return Ok(PlayerDirective::None);
|
|
||||||
}
|
|
||||||
let current_scene = current_scene.unwrap();
|
|
||||||
let ct = (*self.state).borrow().ct.clone();
|
|
||||||
|
|
||||||
let d: Dynamic = rhai_error_to_anyhow(self.engine.call_fn(
|
|
||||||
&mut self.scope,
|
|
||||||
ct.config.ui_scenes.get(current_scene.as_str()).unwrap(),
|
|
||||||
"event",
|
|
||||||
(State::new(state, input.clone()), arg.clone()),
|
|
||||||
))
|
|
||||||
.with_context(|| format!("while handling event `{:?}`", arg))
|
|
||||||
.with_context(|| format!("in ui scene `{}`", current_scene))?;
|
|
||||||
|
|
||||||
if d.is::<PlayerDirective>() {
|
|
||||||
return Ok(d.cast());
|
|
||||||
} else if !(d.is_unit()) {
|
|
||||||
error!(
|
|
||||||
"`event()` in UI scene `{current_scene}` returned invalid type `{}`",
|
|
||||||
d
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return Ok(PlayerDirective::None);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Change the current scene
|
/// Change the current scene
|
||||||
pub fn set_scene(&mut self, state: &RenderState, input: Arc<RenderInput>) -> Result<()> {
|
pub fn set_scene(&mut self, state: &RenderState, input: Arc<RenderInput>) -> Result<()> {
|
||||||
let current_scene = (*self.state).borrow().get_scene().clone();
|
let current_scene = (*self.state).borrow().get_scene().clone();
|
||||||
|
@ -201,12 +87,12 @@ impl UiScriptExecutor {
|
||||||
let mut elm = self.state.borrow_mut();
|
let mut elm = self.state.borrow_mut();
|
||||||
elm.clear();
|
elm.clear();
|
||||||
drop(elm);
|
drop(elm);
|
||||||
|
|
||||||
let ct = (*self.state).borrow().ct.clone();
|
let ct = (*self.state).borrow().ct.clone();
|
||||||
|
|
||||||
rhai_error_to_anyhow(
|
rhai_error_to_anyhow(
|
||||||
self.engine.call_fn(
|
self.engine.call_fn(
|
||||||
&mut self.scope,
|
&mut self.scope,
|
||||||
ct.config
|
ct.get_config()
|
||||||
.ui_scenes
|
.ui_scenes
|
||||||
.get(current_scene.as_ref().unwrap().as_str())
|
.get(current_scene.as_ref().unwrap().as_str())
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
|
@ -228,7 +114,7 @@ impl UiScriptExecutor {
|
||||||
if (*self.state).borrow().get_scene().is_none() {
|
if (*self.state).borrow().get_scene().is_none() {
|
||||||
(*self.state)
|
(*self.state)
|
||||||
.borrow_mut()
|
.borrow_mut()
|
||||||
.set_scene(ImmutableString::from(&ct.config.start_ui_scene));
|
.set_scene(ImmutableString::from(&ct.get_config().start_ui_scene));
|
||||||
}
|
}
|
||||||
self.set_scene(state, input.clone())?;
|
self.set_scene(state, input.clone())?;
|
||||||
let current_scene = (*self.state).borrow().get_scene().clone();
|
let current_scene = (*self.state).borrow().get_scene().clone();
|
||||||
|
@ -236,8 +122,9 @@ impl UiScriptExecutor {
|
||||||
(*self.state).borrow_mut().step(state, input.clone());
|
(*self.state).borrow_mut().step(state, input.clone());
|
||||||
|
|
||||||
// Run step() (if it is defined)
|
// Run step() (if it is defined)
|
||||||
|
|
||||||
let ast = ct
|
let ast = ct
|
||||||
.config
|
.get_config()
|
||||||
.ui_scenes
|
.ui_scenes
|
||||||
.get(current_scene.as_ref().unwrap().as_str())
|
.get(current_scene.as_ref().unwrap().as_str())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
@ -271,25 +158,79 @@ impl UiScriptExecutor {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
} {
|
} {
|
||||||
self.run_event_callback(state, input.clone(), Dynamic::from(PlayerShipStateEvent {}))?;
|
rhai_error_to_anyhow(
|
||||||
|
self.engine.call_fn(
|
||||||
|
&mut self.scope,
|
||||||
|
ct.get_config()
|
||||||
|
.ui_scenes
|
||||||
|
.get(current_scene.as_ref().unwrap().as_str())
|
||||||
|
.unwrap(),
|
||||||
|
"event",
|
||||||
|
(State::new(state, input.clone()), PlayerShipStateEvent {}),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.with_context(|| format!("while handling player state change event"))
|
||||||
|
.with_context(|| format!("in ui scene `{}`", current_scene.as_ref().unwrap()))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let len = (*self.state).borrow().len();
|
let len = (*self.state).borrow().len();
|
||||||
for i in 0..len {
|
for i in 0..len {
|
||||||
match (*self.state).borrow_mut().get_mut_by_idx(i).unwrap() {
|
let event_arg = match (*self.state).borrow_mut().get_mut_by_idx(i).unwrap() {
|
||||||
UiElement::Sprite(sprite) => {
|
UiElement::Sprite(sprite) => {
|
||||||
|
// Draw and update sprites
|
||||||
|
sprite.step(&input, state);
|
||||||
sprite.push_to_buffer(&input, state);
|
sprite.push_to_buffer(&input, state);
|
||||||
|
let event = sprite.check_events(&input, state);
|
||||||
|
|
||||||
|
match event {
|
||||||
|
Event::None => None,
|
||||||
|
|
||||||
|
Event::MouseClick => Some(Dynamic::from(MouseClickEvent {
|
||||||
|
down: true,
|
||||||
|
element: sprite.name.clone(),
|
||||||
|
})),
|
||||||
|
|
||||||
|
Event::MouseRelease => Some(Dynamic::from(MouseClickEvent {
|
||||||
|
down: false,
|
||||||
|
element: sprite.name.clone(),
|
||||||
|
})),
|
||||||
|
|
||||||
|
Event::MouseHover => Some(Dynamic::from(MouseHoverEvent {
|
||||||
|
enter: true,
|
||||||
|
element: sprite.name.clone(),
|
||||||
|
})),
|
||||||
|
|
||||||
|
Event::MouseUnhover => Some(Dynamic::from(MouseHoverEvent {
|
||||||
|
enter: false,
|
||||||
|
element: sprite.name.clone(),
|
||||||
|
})),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
UiElement::RadialBar(x) => {
|
UiElement::RadialBar(x) => {
|
||||||
|
// Draw and update radialbar
|
||||||
|
x.step(&input, state);
|
||||||
x.push_to_buffer(&input, state);
|
x.push_to_buffer(&input, state);
|
||||||
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
UiElement::Scrollbox(x) => {
|
UiElement::Text(..) => None,
|
||||||
x.push_to_buffer(&input, state);
|
};
|
||||||
}
|
|
||||||
|
|
||||||
UiElement::SubElement { .. } | UiElement::Text(..) => {}
|
if let Some(event_arg) = event_arg {
|
||||||
|
rhai_error_to_anyhow(
|
||||||
|
self.engine.call_fn(
|
||||||
|
&mut self.scope,
|
||||||
|
ct.get_config()
|
||||||
|
.ui_scenes
|
||||||
|
.get(current_scene.as_ref().unwrap().as_str())
|
||||||
|
.unwrap(),
|
||||||
|
"event",
|
||||||
|
(State::new(state, input.clone()), event_arg.clone()),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.with_context(|| format!("while handling event `{:?}`", event_arg))
|
||||||
|
.with_context(|| format!("in ui scene `{}`", current_scene.as_ref().unwrap()))?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
mod api;
|
mod api;
|
||||||
mod camera;
|
mod event;
|
||||||
mod elements;
|
|
||||||
mod executor;
|
mod executor;
|
||||||
mod state;
|
mod state;
|
||||||
|
|
||||||
pub(crate) use camera::*;
|
mod elements;
|
||||||
|
|
||||||
pub(crate) use executor::UiScriptExecutor;
|
pub(crate) use executor::UiScriptExecutor;
|
||||||
pub(crate) use state::*;
|
pub(crate) use state::*;
|
||||||
|
|
|
@ -1,39 +1,19 @@
|
||||||
use galactica_content::Content;
|
use galactica_content::Content;
|
||||||
|
use glyphon::TextArea;
|
||||||
use log::{debug, error};
|
use log::{debug, error};
|
||||||
use rhai::ImmutableString;
|
use rhai::ImmutableString;
|
||||||
use std::{cell::RefCell, collections::HashMap, rc::Rc, sync::Arc, time::Instant};
|
use std::collections::HashMap;
|
||||||
|
use std::sync::Arc;
|
||||||
use winit::window::Window;
|
use winit::window::Window;
|
||||||
|
|
||||||
use super::{
|
use super::elements::{FpsIndicator, RadialBar, Sprite, TextBox};
|
||||||
elements::{FpsIndicator, OwnedTextArea, UiRadialBar, UiScrollbox, UiSprite, UiTextBox},
|
|
||||||
Camera,
|
|
||||||
};
|
|
||||||
use crate::{RenderInput, RenderState};
|
use crate::{RenderInput, RenderState};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum UiElement {
|
pub enum UiElement {
|
||||||
Sprite(UiSprite),
|
Sprite(Sprite),
|
||||||
RadialBar(UiRadialBar),
|
RadialBar(RadialBar),
|
||||||
Text(UiTextBox),
|
Text(TextBox),
|
||||||
Scrollbox(UiScrollbox),
|
|
||||||
|
|
||||||
/// This is a sub-element managed by another element
|
|
||||||
SubElement {
|
|
||||||
parent: ImmutableString,
|
|
||||||
element: Rc<RefCell<UiElement>>,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
impl UiElement {
|
|
||||||
pub fn get_name(&self) -> ImmutableString {
|
|
||||||
match self {
|
|
||||||
Self::Sprite(x) => x.name.clone(),
|
|
||||||
Self::RadialBar(x) => x.name.clone(),
|
|
||||||
Self::Text(x) => x.name.clone(),
|
|
||||||
Self::Scrollbox(x) => x.name.clone(),
|
|
||||||
Self::SubElement { element, .. } => element.borrow().get_name(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
|
@ -51,13 +31,8 @@ pub(crate) struct UiState {
|
||||||
|
|
||||||
show_timings: bool,
|
show_timings: bool,
|
||||||
fps_indicator: FpsIndicator,
|
fps_indicator: FpsIndicator,
|
||||||
last_step: Instant,
|
|
||||||
|
|
||||||
pub config: UiConfig,
|
pub config: UiConfig,
|
||||||
|
|
||||||
/// The player's camera.
|
|
||||||
/// Only used when drawing physics.
|
|
||||||
pub camera: Camera,
|
|
||||||
}
|
}
|
||||||
// TODO: remove this
|
// TODO: remove this
|
||||||
unsafe impl Send for UiState {}
|
unsafe impl Send for UiState {}
|
||||||
|
@ -77,8 +52,6 @@ impl UiState {
|
||||||
show_phys: false,
|
show_phys: false,
|
||||||
show_starfield: false,
|
show_starfield: false,
|
||||||
},
|
},
|
||||||
last_step: Instant::now(),
|
|
||||||
camera: Camera::new(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,7 +95,7 @@ impl UiState {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_scene(&mut self, scene: ImmutableString) {
|
pub fn set_scene(&mut self, scene: ImmutableString) {
|
||||||
if !self.ct.config.ui_scenes.contains_key(scene.as_str()) {
|
if !self.ct.get_config().ui_scenes.contains_key(scene.as_str()) {
|
||||||
error!("tried to switch to ui scene `{scene}`, which doesn't exist");
|
error!("tried to switch to ui scene `{scene}`, which doesn't exist");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -132,57 +105,15 @@ impl UiState {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn step(&mut self, state: &mut RenderState, input: Arc<RenderInput>) {
|
pub fn step(&mut self, state: &mut RenderState, input: Arc<RenderInput>) {
|
||||||
let t = self.last_step.elapsed().as_secs_f32();
|
|
||||||
for (_, e) in &mut self.elements {
|
|
||||||
match e {
|
|
||||||
UiElement::Sprite(sprite) => sprite.step(t),
|
|
||||||
UiElement::Scrollbox(sbox) => sbox.step(t),
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if self.show_timings {
|
if self.show_timings {
|
||||||
self.fps_indicator
|
self.fps_indicator
|
||||||
.step(&input, &mut state.text_font_system.borrow_mut());
|
.step(&input, &mut state.text_font_system.borrow_mut());
|
||||||
}
|
}
|
||||||
self.last_step = Instant::now();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn add_element(&mut self, e: UiElement) {
|
|
||||||
self.names.push(e.get_name().clone());
|
|
||||||
self.elements.insert(e.get_name().clone(), e);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove an element from this sprite.
|
|
||||||
// This does NOT remove subelements from their parent sprites.
|
|
||||||
pub fn remove_element_incomplete(&mut self, name: &ImmutableString) -> Option<UiElement> {
|
|
||||||
let e = self.elements.remove(name);
|
|
||||||
self.names.retain(|x| *x != name);
|
|
||||||
return e;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove an element from this sprite and from all subsprites.
|
|
||||||
pub fn remove_element(&mut self, name: &ImmutableString) {
|
|
||||||
let e = self.elements.remove(name);
|
|
||||||
self.names.retain(|x| *x != name);
|
|
||||||
|
|
||||||
match e {
|
|
||||||
Some(UiElement::SubElement { parent, element }) => {
|
|
||||||
let x = Rc::into_inner(element).unwrap().into_inner();
|
|
||||||
let parent = self.elements.get_mut(&parent).unwrap();
|
|
||||||
match parent {
|
|
||||||
UiElement::Scrollbox(s) => s.remove_element(&x.get_name()),
|
|
||||||
_ => unreachable!("invalid subelement parent"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: don't allocate here, return an iterator
|
|
||||||
impl<'a> UiState {
|
impl<'a> UiState {
|
||||||
pub fn get_textareas(&'a self, input: &RenderInput, window: &Window) -> Vec<OwnedTextArea> {
|
pub fn get_textareas(&'a mut self, input: &RenderInput, window: &Window) -> Vec<TextArea<'a>> {
|
||||||
let mut v = Vec::with_capacity(32);
|
let mut v = Vec::with_capacity(32);
|
||||||
|
|
||||||
if self.current_scene.is_none() {
|
if self.current_scene.is_none() {
|
||||||
|
@ -193,10 +124,9 @@ impl<'a> UiState {
|
||||||
v.push(self.fps_indicator.get_textarea(input, window))
|
v.push(self.fps_indicator.get_textarea(input, window))
|
||||||
}
|
}
|
||||||
|
|
||||||
for e in self.elements.values() {
|
for t in self.elements.values() {
|
||||||
match &e {
|
match &t {
|
||||||
UiElement::Text(t) => v.push(t.get_textarea(input, window)),
|
UiElement::Text(x) => v.push(x.get_textarea(input, window)),
|
||||||
UiElement::Scrollbox(b) => v.extend(b.get_textareas(input, window)),
|
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,4 +25,3 @@ rapier2d = { workspace = true }
|
||||||
nalgebra = { workspace = true }
|
nalgebra = { workspace = true }
|
||||||
crossbeam = { workspace = true }
|
crossbeam = { workspace = true }
|
||||||
rand = { workspace = true }
|
rand = { workspace = true }
|
||||||
log = { workspace = true }
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use std::{collections::HashMap, sync::Arc};
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use galactica_content::{ContentIndex, GunPoint, Outfit, OutfitSpace};
|
use galactica_content::{GunPoint, Outfit, OutfitHandle, OutfitSpace};
|
||||||
|
|
||||||
/// Possible outcomes when adding an outfit
|
/// Possible outcomes when adding an outfit
|
||||||
pub enum OutfitAddResult {
|
pub enum OutfitAddResult {
|
||||||
|
@ -34,7 +34,7 @@ pub enum OutfitRemoveResult {
|
||||||
/// A simple data class, used to keep track of delayed shield generators
|
/// A simple data class, used to keep track of delayed shield generators
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub(crate) struct ShieldGenerator {
|
pub(crate) struct ShieldGenerator {
|
||||||
pub outfit: Arc<Outfit>,
|
pub outfit: OutfitHandle,
|
||||||
pub delay: f32,
|
pub delay: f32,
|
||||||
pub generation: f32,
|
pub generation: f32,
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ pub(crate) struct ShieldGenerator {
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct OutfitSet {
|
pub struct OutfitSet {
|
||||||
/// What outfits does this statsum contain?
|
/// What outfits does this statsum contain?
|
||||||
outfits: HashMap<ContentIndex, (Arc<Outfit>, u32)>,
|
outfits: HashMap<OutfitHandle, u32>,
|
||||||
|
|
||||||
/// Space available in this outfitset.
|
/// Space available in this outfitset.
|
||||||
/// set at creation and never changes.
|
/// set at creation and never changes.
|
||||||
|
@ -59,7 +59,7 @@ pub struct OutfitSet {
|
||||||
/// The gun points available in this ship.
|
/// The gun points available in this ship.
|
||||||
/// If value is None, this point is free.
|
/// If value is None, this point is free.
|
||||||
/// if value is Some, this point is taken.
|
/// if value is Some, this point is taken.
|
||||||
gun_points: HashMap<GunPoint, Option<Arc<Outfit>>>,
|
gun_points: HashMap<GunPoint, Option<OutfitHandle>>,
|
||||||
|
|
||||||
/// Outfit values
|
/// Outfit values
|
||||||
/// This isn't strictly necessary, but we don't want to
|
/// This isn't strictly necessary, but we don't want to
|
||||||
|
@ -89,7 +89,7 @@ impl OutfitSet {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn add(&mut self, o: &Arc<Outfit>) -> OutfitAddResult {
|
pub(super) fn add(&mut self, o: &Outfit) -> OutfitAddResult {
|
||||||
if !(self.total_space - self.used_space).can_contain(&o.space) {
|
if !(self.total_space - self.used_space).can_contain(&o.space) {
|
||||||
return OutfitAddResult::NotEnoughSpace("TODO".to_string());
|
return OutfitAddResult::NotEnoughSpace("TODO".to_string());
|
||||||
}
|
}
|
||||||
|
@ -100,7 +100,7 @@ impl OutfitSet {
|
||||||
let mut added = false;
|
let mut added = false;
|
||||||
for (_, outfit) in &mut self.gun_points {
|
for (_, outfit) in &mut self.gun_points {
|
||||||
if outfit.is_none() {
|
if outfit.is_none() {
|
||||||
*outfit = Some(o.clone());
|
*outfit = Some(o.handle);
|
||||||
added = true;
|
added = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -115,29 +115,29 @@ impl OutfitSet {
|
||||||
self.steer_power += o.steer_power;
|
self.steer_power += o.steer_power;
|
||||||
self.shield_strength += o.shield_strength;
|
self.shield_strength += o.shield_strength;
|
||||||
self.shield_generators.push(ShieldGenerator {
|
self.shield_generators.push(ShieldGenerator {
|
||||||
outfit: o.clone(),
|
outfit: o.handle,
|
||||||
delay: o.shield_delay,
|
delay: o.shield_delay,
|
||||||
generation: o.shield_generation,
|
generation: o.shield_generation,
|
||||||
});
|
});
|
||||||
|
|
||||||
if self.outfits.contains_key(&o.index) {
|
if self.outfits.contains_key(&o.handle) {
|
||||||
self.outfits.get_mut(&o.index).unwrap().1 += 1;
|
*self.outfits.get_mut(&o.handle).unwrap() += 1;
|
||||||
} else {
|
} else {
|
||||||
self.outfits.insert(o.index.clone(), (o.clone(), 1));
|
self.outfits.insert(o.handle, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
return OutfitAddResult::Ok;
|
return OutfitAddResult::Ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn remove(&mut self, o: &Arc<Outfit>) -> OutfitRemoveResult {
|
pub(super) fn remove(&mut self, o: &Outfit) -> OutfitRemoveResult {
|
||||||
if !self.outfits.contains_key(&o.index) {
|
if !self.outfits.contains_key(&o.handle) {
|
||||||
return OutfitRemoveResult::NotExist;
|
return OutfitRemoveResult::NotExist;
|
||||||
} else {
|
} else {
|
||||||
let n = self.outfits.get_mut(&o.index).unwrap();
|
let n = *self.outfits.get(&o.handle).unwrap();
|
||||||
if n.1 == 1u32 {
|
if n == 1u32 {
|
||||||
self.outfits.remove(&o.index);
|
self.outfits.remove(&o.handle);
|
||||||
} else {
|
} else {
|
||||||
self.outfits.get_mut(&o.index).unwrap().1 -= 1;
|
*self.outfits.get_mut(&o.handle).unwrap() -= 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -152,7 +152,7 @@ impl OutfitSet {
|
||||||
let index = self
|
let index = self
|
||||||
.shield_generators
|
.shield_generators
|
||||||
.iter()
|
.iter()
|
||||||
.position(|g| g.outfit.index == o.index)
|
.position(|g| g.outfit == o.handle)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
self.shield_generators.remove(index);
|
self.shield_generators.remove(index);
|
||||||
}
|
}
|
||||||
|
@ -165,16 +165,16 @@ impl OutfitSet {
|
||||||
impl OutfitSet {
|
impl OutfitSet {
|
||||||
/// The number of outfits in this set
|
/// The number of outfits in this set
|
||||||
pub fn len(&self) -> u32 {
|
pub fn len(&self) -> u32 {
|
||||||
self.outfits.iter().map(|(_, (_, x))| x).sum()
|
self.outfits.iter().map(|(_, x)| x).sum()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Iterate over all outfits
|
/// Iterate over all outfits
|
||||||
pub fn iter_outfits(&self) -> impl Iterator<Item = &(Arc<Outfit>, u32)> {
|
pub fn iter_outfits(&self) -> impl Iterator<Item = (&OutfitHandle, &u32)> {
|
||||||
self.outfits.values()
|
self.outfits.iter()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Iterate over all gun points
|
/// Iterate over all gun points
|
||||||
pub fn iter_gun_points(&self) -> impl Iterator<Item = (&GunPoint, &Option<Arc<Outfit>>)> {
|
pub fn iter_gun_points(&self) -> impl Iterator<Item = (&GunPoint, &Option<OutfitHandle>)> {
|
||||||
self.gun_points.iter()
|
self.gun_points.iter()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -190,7 +190,7 @@ impl OutfitSet {
|
||||||
|
|
||||||
/// Get the outfit attached to the given gun point
|
/// Get the outfit attached to the given gun point
|
||||||
/// Will panic if this gunpoint is not in this outfit set.
|
/// Will panic if this gunpoint is not in this outfit set.
|
||||||
pub fn get_gun(&self, point: &GunPoint) -> Option<Arc<Outfit>> {
|
pub fn get_gun(&self, point: &GunPoint) -> Option<OutfitHandle> {
|
||||||
self.gun_points.get(point).unwrap().clone()
|
self.gun_points.get(point).unwrap().clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use galactica_content::{Faction, GunPoint, Outfit, Ship, SystemObject};
|
use galactica_content::{Content, FactionHandle, GunPoint, Outfit, ShipHandle, SystemObjectHandle};
|
||||||
use nalgebra::Isometry2;
|
use nalgebra::Isometry2;
|
||||||
use rand::{rngs::ThreadRng, Rng};
|
use rand::{rngs::ThreadRng, Rng};
|
||||||
use std::{collections::HashMap, sync::Arc, time::Instant};
|
use std::{collections::HashMap, time::Instant};
|
||||||
|
|
||||||
use super::{OutfitSet, ShipAutoPilot, ShipPersonality, ShipState};
|
use super::{OutfitSet, ShipAutoPilot, ShipPersonality, ShipState};
|
||||||
|
|
||||||
|
@ -9,8 +9,8 @@ use super::{OutfitSet, ShipAutoPilot, ShipPersonality, ShipState};
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct ShipData {
|
pub struct ShipData {
|
||||||
// Metadata values
|
// Metadata values
|
||||||
ship: Arc<Ship>,
|
ct_handle: ShipHandle,
|
||||||
faction: Arc<Faction>,
|
faction: FactionHandle,
|
||||||
outfits: OutfitSet,
|
outfits: OutfitSet,
|
||||||
|
|
||||||
personality: ShipPersonality,
|
personality: ShipPersonality,
|
||||||
|
@ -34,14 +34,16 @@ pub struct ShipData {
|
||||||
impl ShipData {
|
impl ShipData {
|
||||||
/// Create a new ShipData
|
/// Create a new ShipData
|
||||||
pub(crate) fn new(
|
pub(crate) fn new(
|
||||||
ship: Arc<Ship>,
|
ct: &Content,
|
||||||
faction: Arc<Faction>,
|
ct_handle: ShipHandle,
|
||||||
|
faction: FactionHandle,
|
||||||
personality: ShipPersonality,
|
personality: ShipPersonality,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
|
let s = ct.get_ship(ct_handle);
|
||||||
ShipData {
|
ShipData {
|
||||||
ship: ship.clone(),
|
ct_handle,
|
||||||
faction,
|
faction,
|
||||||
outfits: OutfitSet::new(ship.space, &ship.guns),
|
outfits: OutfitSet::new(s.space, &s.guns),
|
||||||
personality,
|
personality,
|
||||||
last_hit: Instant::now(),
|
last_hit: Instant::now(),
|
||||||
rng: rand::thread_rng(),
|
rng: rand::thread_rng(),
|
||||||
|
@ -52,9 +54,9 @@ impl ShipData {
|
||||||
},
|
},
|
||||||
|
|
||||||
// Initial stats
|
// Initial stats
|
||||||
hull: ship.hull,
|
hull: s.hull,
|
||||||
shields: 0.0,
|
shields: 0.0,
|
||||||
gun_cooldowns: ship.guns.iter().map(|x| (x.clone(), 0.0)).collect(),
|
gun_cooldowns: s.guns.iter().map(|x| (x.clone(), 0.0)).collect(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,11 +80,11 @@ impl ShipData {
|
||||||
/// That is the simulation's responsiblity.
|
/// That is the simulation's responsiblity.
|
||||||
///
|
///
|
||||||
/// Will panic if we're not flying.
|
/// Will panic if we're not flying.
|
||||||
pub fn start_land_on(&mut self, target: Arc<SystemObject>) {
|
pub fn start_land_on(&mut self, target_handle: SystemObjectHandle) {
|
||||||
match self.state {
|
match self.state {
|
||||||
ShipState::Flying { .. } => {
|
ShipState::Flying { .. } => {
|
||||||
self.state = ShipState::Landing {
|
self.state = ShipState::Landing {
|
||||||
target,
|
target: target_handle,
|
||||||
current_z: 1.0,
|
current_z: 1.0,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -106,11 +108,9 @@ impl ShipData {
|
||||||
/// Finish landing sequence
|
/// Finish landing sequence
|
||||||
/// Will panic if we're not landing
|
/// Will panic if we're not landing
|
||||||
pub fn finish_land_on(&mut self) {
|
pub fn finish_land_on(&mut self) {
|
||||||
match &self.state {
|
match self.state {
|
||||||
ShipState::Landing { target, .. } => {
|
ShipState::Landing { target, .. } => {
|
||||||
self.state = ShipState::Landed {
|
self.state = ShipState::Landed { target };
|
||||||
target: target.clone(),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
unreachable!("Called `finish_land_on` on a ship that isn't landing!")
|
unreachable!("Called `finish_land_on` on a ship that isn't landing!")
|
||||||
|
@ -123,13 +123,14 @@ impl ShipData {
|
||||||
/// That is the simulation's responsiblity.
|
/// That is the simulation's responsiblity.
|
||||||
///
|
///
|
||||||
/// Will panic if we're not flying.
|
/// Will panic if we're not flying.
|
||||||
pub fn start_unland_to(&mut self, to_position: Isometry2<f32>) {
|
pub fn start_unland_to(&mut self, ct: &Content, to_position: Isometry2<f32>) {
|
||||||
match &self.state {
|
match self.state {
|
||||||
ShipState::Landed { target } => {
|
ShipState::Landed { target } => {
|
||||||
|
let obj = ct.get_system_object(target);
|
||||||
self.state = ShipState::UnLanding {
|
self.state = ShipState::UnLanding {
|
||||||
to_position,
|
to_position,
|
||||||
from: target.clone(),
|
from: target,
|
||||||
current_z: target.pos.z,
|
current_z: obj.pos.z,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
|
@ -176,14 +177,14 @@ impl ShipData {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add an outfit to this ship
|
/// Add an outfit to this ship
|
||||||
pub fn add_outfit(&mut self, o: &Arc<Outfit>) -> super::OutfitAddResult {
|
pub fn add_outfit(&mut self, o: &Outfit) -> super::OutfitAddResult {
|
||||||
let r = self.outfits.add(o);
|
let r = self.outfits.add(o);
|
||||||
self.shields = self.outfits.get_total_shields();
|
self.shields = self.outfits.get_total_shields();
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Remove an outfit from this ship
|
/// Remove an outfit from this ship
|
||||||
pub fn remove_outfit(&mut self, o: &Arc<Outfit>) -> super::OutfitRemoveResult {
|
pub fn remove_outfit(&mut self, o: &Outfit) -> super::OutfitRemoveResult {
|
||||||
self.outfits.remove(o)
|
self.outfits.remove(o)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -191,14 +192,16 @@ impl ShipData {
|
||||||
/// Will panic if `which` isn't a point on this ship.
|
/// Will panic if `which` isn't a point on this ship.
|
||||||
/// Returns `true` if this gun was fired,
|
/// Returns `true` if this gun was fired,
|
||||||
/// and `false` if it is on cooldown or empty.
|
/// and `false` if it is on cooldown or empty.
|
||||||
pub(crate) fn fire_gun(&mut self, which: &GunPoint) -> bool {
|
pub(crate) fn fire_gun(&mut self, ct: &Content, which: &GunPoint) -> bool {
|
||||||
let c = self.gun_cooldowns.get_mut(which).unwrap();
|
let c = self.gun_cooldowns.get_mut(which).unwrap();
|
||||||
|
|
||||||
if *c > 0.0 {
|
if *c > 0.0 {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(g) = self.outfits.get_gun(which) {
|
let g = self.outfits.get_gun(which);
|
||||||
|
if g.is_some() {
|
||||||
|
let g = ct.get_outfit(g.unwrap());
|
||||||
let gun = g.gun.as_ref().unwrap();
|
let gun = g.gun.as_ref().unwrap();
|
||||||
*c = 0f32.max(gun.rate + self.rng.gen_range(-gun.rate_rng..=gun.rate_rng));
|
*c = 0f32.max(gun.rate + self.rng.gen_range(-gun.rate_rng..=gun.rate_rng));
|
||||||
return true;
|
return true;
|
||||||
|
@ -285,12 +288,13 @@ impl ShipData {
|
||||||
&self.state
|
&self.state
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get this ship's content
|
/// Get a handle to this ship's content
|
||||||
pub fn get_content(&self) -> &Arc<Ship> {
|
pub fn get_content(&self) -> ShipHandle {
|
||||||
&self.ship
|
self.ct_handle
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get this ship's current hull
|
/// Get this ship's current hull.
|
||||||
|
/// Use content handle to get maximum hull
|
||||||
pub fn get_hull(&self) -> f32 {
|
pub fn get_hull(&self) -> f32 {
|
||||||
self.hull
|
self.hull
|
||||||
}
|
}
|
||||||
|
@ -312,7 +316,12 @@ impl ShipData {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get this ship's faction
|
/// Get this ship's faction
|
||||||
pub fn get_faction(&self) -> &Arc<Faction> {
|
pub fn get_faction(&self) -> FactionHandle {
|
||||||
&self.faction
|
self.faction
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get this ship's content handle
|
||||||
|
pub fn get_ship(&self) -> ShipHandle {
|
||||||
|
self.ct_handle
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use galactica_content::SystemObject;
|
use std::num::NonZeroU32;
|
||||||
|
|
||||||
|
use galactica_content::SystemObjectHandle;
|
||||||
use rapier2d::math::Isometry;
|
use rapier2d::math::Isometry;
|
||||||
use std::{num::NonZeroU32, sync::Arc};
|
|
||||||
|
|
||||||
/// A ship autopilot.
|
/// A ship autopilot.
|
||||||
/// An autopilot is a lightweight ShipController that
|
/// An autopilot is a lightweight ShipController that
|
||||||
|
@ -13,7 +14,7 @@ pub enum ShipAutoPilot {
|
||||||
/// Automatically arrange for landing on the given object
|
/// Automatically arrange for landing on the given object
|
||||||
Landing {
|
Landing {
|
||||||
/// The body we want to land on
|
/// The body we want to land on
|
||||||
target: Arc<SystemObject>,
|
target: SystemObjectHandle,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,14 +40,14 @@ pub enum ShipState {
|
||||||
/// This ship is landed on a planet
|
/// This ship is landed on a planet
|
||||||
Landed {
|
Landed {
|
||||||
/// The planet this ship is landed on
|
/// The planet this ship is landed on
|
||||||
target: Arc<SystemObject>,
|
target: SystemObjectHandle,
|
||||||
},
|
},
|
||||||
|
|
||||||
/// This ship is landing on a planet
|
/// This ship is landing on a planet
|
||||||
/// (playing the animation)
|
/// (playing the animation)
|
||||||
Landing {
|
Landing {
|
||||||
/// The planet we're landing on
|
/// The planet we're landing on
|
||||||
target: Arc<SystemObject>,
|
target: SystemObjectHandle,
|
||||||
|
|
||||||
/// Our current z-coordinate
|
/// Our current z-coordinate
|
||||||
current_z: f32,
|
current_z: f32,
|
||||||
|
@ -59,7 +60,7 @@ pub enum ShipState {
|
||||||
to_position: Isometry<f32>,
|
to_position: Isometry<f32>,
|
||||||
|
|
||||||
/// The planet we're taking off from
|
/// The planet we're taking off from
|
||||||
from: Arc<SystemObject>,
|
from: SystemObjectHandle,
|
||||||
|
|
||||||
/// Our current z-coordinate
|
/// Our current z-coordinate
|
||||||
current_z: f32,
|
current_z: f32,
|
||||||
|
@ -68,9 +69,9 @@ pub enum ShipState {
|
||||||
|
|
||||||
impl ShipState {
|
impl ShipState {
|
||||||
/// What planet is this ship landed on?
|
/// What planet is this ship landed on?
|
||||||
pub fn landed_on(&self) -> Option<Arc<SystemObject>> {
|
pub fn landed_on(&self) -> Option<SystemObjectHandle> {
|
||||||
match self {
|
match self {
|
||||||
Self::Landed { target } => Some(target.clone()),
|
Self::Landed { target } => Some(*target),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,3 @@
|
||||||
|
|
||||||
pub mod data;
|
pub mod data;
|
||||||
pub mod phys;
|
pub mod phys;
|
||||||
mod playerdirective;
|
|
||||||
|
|
||||||
pub use playerdirective::PlayerDirective;
|
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
use galactica_content::{Effect, EffectVelocity, SpriteAutomaton};
|
use galactica_content::{Content, EffectHandle, EffectVelocity, SpriteAutomaton};
|
||||||
use nalgebra::{Point2, Rotation2, Vector2};
|
use nalgebra::{Point2, Rotation2, Vector2};
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use rapier2d::dynamics::{RevoluteJointBuilder, RigidBodyBuilder, RigidBodyHandle, RigidBodyType};
|
use rapier2d::dynamics::{RevoluteJointBuilder, RigidBodyBuilder, RigidBodyHandle, RigidBodyType};
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use crate::phys::{PhysStepResources, PhysWrapper};
|
use crate::phys::{PhysStepResources, PhysWrapper};
|
||||||
|
|
||||||
|
@ -33,13 +32,16 @@ pub struct PhysEffect {
|
||||||
impl PhysEffect {
|
impl PhysEffect {
|
||||||
/// Create a new effect inside `Wrapper`
|
/// Create a new effect inside `Wrapper`
|
||||||
pub fn new(
|
pub fn new(
|
||||||
|
ct: &Content,
|
||||||
wrapper: &mut PhysWrapper,
|
wrapper: &mut PhysWrapper,
|
||||||
effect: Arc<Effect>,
|
effect: EffectHandle,
|
||||||
// Where to spawn the particle, in world space.
|
// Where to spawn the particle, in world space.
|
||||||
pos: Vector2<f32>,
|
pos: Vector2<f32>,
|
||||||
parent: RigidBodyHandle,
|
parent: RigidBodyHandle,
|
||||||
target: Option<RigidBodyHandle>,
|
target: Option<RigidBodyHandle>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
|
let effect = ct.get_effect(effect);
|
||||||
|
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
let parent_body = wrapper.get_rigid_body(parent).unwrap();
|
let parent_body = wrapper.get_rigid_body(parent).unwrap();
|
||||||
let parent_angle = parent_body.rotation().angle();
|
let parent_angle = parent_body.rotation().angle();
|
||||||
|
@ -111,7 +113,7 @@ impl PhysEffect {
|
||||||
};
|
};
|
||||||
|
|
||||||
PhysEffect {
|
PhysEffect {
|
||||||
anim: SpriteAutomaton::new(effect.sprite.clone()),
|
anim: SpriteAutomaton::new(ct, effect.sprite),
|
||||||
rigid_body,
|
rigid_body,
|
||||||
lifetime: 0f32.max(
|
lifetime: 0f32.max(
|
||||||
effect.lifetime + rng.gen_range(-effect.lifetime_rng..=effect.lifetime_rng),
|
effect.lifetime + rng.gen_range(-effect.lifetime_rng..=effect.lifetime_rng),
|
||||||
|
@ -151,7 +153,7 @@ impl PhysEffect {
|
||||||
);
|
);
|
||||||
|
|
||||||
PhysEffect {
|
PhysEffect {
|
||||||
anim: SpriteAutomaton::new(effect.sprite.clone()),
|
anim: SpriteAutomaton::new(ct, effect.sprite),
|
||||||
rigid_body,
|
rigid_body,
|
||||||
lifetime: 0f32.max(
|
lifetime: 0f32.max(
|
||||||
effect.lifetime + rng.gen_range(-effect.lifetime_rng..=effect.lifetime_rng),
|
effect.lifetime + rng.gen_range(-effect.lifetime_rng..=effect.lifetime_rng),
|
||||||
|
@ -172,7 +174,7 @@ impl PhysEffect {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.anim.step(res.t);
|
self.anim.step(&res.ct, res.t);
|
||||||
self.lifetime -= res.t;
|
self.lifetime -= res.t;
|
||||||
|
|
||||||
if self.lifetime <= 0.0 {
|
if self.lifetime <= 0.0 {
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
use std::sync::Arc;
|
use galactica_content::{AnimationState, Content, FactionHandle, Projectile, SpriteAutomaton};
|
||||||
|
|
||||||
use galactica_content::{AnimationState, Faction, Projectile, SpriteAutomaton};
|
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use rapier2d::{dynamics::RigidBodyHandle, geometry::ColliderHandle};
|
use rapier2d::{dynamics::RigidBodyHandle, geometry::ColliderHandle};
|
||||||
|
|
||||||
|
@ -12,7 +10,7 @@ use super::PhysEffect;
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct PhysProjectile {
|
pub struct PhysProjectile {
|
||||||
/// This projectile's game data
|
/// This projectile's game data
|
||||||
pub content: Arc<Projectile>,
|
pub content: Projectile,
|
||||||
|
|
||||||
/// This projectile's sprite animation state
|
/// This projectile's sprite animation state
|
||||||
anim: SpriteAutomaton,
|
anim: SpriteAutomaton,
|
||||||
|
@ -21,7 +19,7 @@ pub struct PhysProjectile {
|
||||||
lifetime: f32,
|
lifetime: f32,
|
||||||
|
|
||||||
/// The faction this projectile belongs to
|
/// The faction this projectile belongs to
|
||||||
pub faction: Arc<Faction>,
|
pub faction: FactionHandle,
|
||||||
|
|
||||||
/// This projectile's rigidbody
|
/// This projectile's rigidbody
|
||||||
pub rigid_body: RigidBodyHandle,
|
pub rigid_body: RigidBodyHandle,
|
||||||
|
@ -40,16 +38,17 @@ pub struct PhysProjectile {
|
||||||
impl PhysProjectile {
|
impl PhysProjectile {
|
||||||
/// Create a new projectile
|
/// Create a new projectile
|
||||||
pub fn new(
|
pub fn new(
|
||||||
content: Arc<Projectile>,
|
ct: &Content,
|
||||||
|
content: Projectile, // TODO: use a handle?
|
||||||
rigid_body: RigidBodyHandle,
|
rigid_body: RigidBodyHandle,
|
||||||
faction: Arc<Faction>,
|
faction: FactionHandle,
|
||||||
collider: ColliderHandle,
|
collider: ColliderHandle,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
let size_rng = content.size_rng;
|
let size_rng = content.size_rng;
|
||||||
let lifetime = content.lifetime;
|
let lifetime = content.lifetime;
|
||||||
PhysProjectile {
|
PhysProjectile {
|
||||||
anim: SpriteAutomaton::new(content.sprite.clone()),
|
anim: SpriteAutomaton::new(ct, content.sprite),
|
||||||
rigid_body,
|
rigid_body,
|
||||||
collider,
|
collider,
|
||||||
content,
|
content,
|
||||||
|
@ -68,24 +67,31 @@ impl PhysProjectile {
|
||||||
wrapper: &mut PhysWrapper,
|
wrapper: &mut PhysWrapper,
|
||||||
) {
|
) {
|
||||||
self.lifetime -= res.t;
|
self.lifetime -= res.t;
|
||||||
self.anim.step(res.t);
|
self.anim.step(&res.ct, res.t);
|
||||||
|
|
||||||
if self.lifetime <= 0.0 {
|
if self.lifetime <= 0.0 {
|
||||||
self.destroy(new, wrapper, true);
|
self.destroy(res, new, wrapper, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Destroy this projectile without creating an expire effect
|
/// Destroy this projectile without creating an expire effect
|
||||||
pub(in crate::phys) fn destroy_silent(
|
pub(in crate::phys) fn destroy_silent(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
res: &PhysStepResources,
|
||||||
new: &mut NewObjects,
|
new: &mut NewObjects,
|
||||||
wrapper: &mut PhysWrapper,
|
wrapper: &mut PhysWrapper,
|
||||||
) {
|
) {
|
||||||
self.destroy(new, wrapper, false);
|
self.destroy(res, new, wrapper, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Destroy this projectile
|
/// Destroy this projectile
|
||||||
fn destroy(&mut self, new: &mut NewObjects, wrapper: &mut PhysWrapper, expire: bool) {
|
fn destroy(
|
||||||
|
&mut self,
|
||||||
|
res: &PhysStepResources,
|
||||||
|
new: &mut NewObjects,
|
||||||
|
wrapper: &mut PhysWrapper,
|
||||||
|
expire: bool,
|
||||||
|
) {
|
||||||
if self.is_destroyed {
|
if self.is_destroyed {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -94,10 +100,11 @@ impl PhysProjectile {
|
||||||
if expire {
|
if expire {
|
||||||
match &self.content.expire_effect {
|
match &self.content.expire_effect {
|
||||||
None => {}
|
None => {}
|
||||||
Some(effect) => {
|
Some(handle) => {
|
||||||
new.effects.push(PhysEffect::new(
|
new.effects.push(PhysEffect::new(
|
||||||
|
&res.ct,
|
||||||
wrapper,
|
wrapper,
|
||||||
effect.clone(),
|
*handle,
|
||||||
*rb.translation(),
|
*rb.translation(),
|
||||||
self.rigid_body,
|
self.rigid_body,
|
||||||
None,
|
None,
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use galactica_content::{CollapseEvent, Ship};
|
use galactica_content::{CollapseEvent, Content, Ship};
|
||||||
use nalgebra::{Point2, Vector2};
|
use nalgebra::{Point2, Vector2};
|
||||||
use rand::{rngs::ThreadRng, Rng};
|
use rand::{rngs::ThreadRng, Rng};
|
||||||
use rapier2d::{
|
use rapier2d::{
|
||||||
|
@ -37,13 +37,14 @@ impl ShipCollapseSequence {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Pick a random points inside a ship's collider
|
/// Pick a random points inside a ship's collider
|
||||||
fn random_in_ship(&mut self, ship: &Ship, collider: &Collider) -> Vector2<f32> {
|
fn random_in_ship(&mut self, ct: &Content, ship: &Ship, collider: &Collider) -> Vector2<f32> {
|
||||||
let mut y = 0.0;
|
let mut y = 0.0;
|
||||||
let mut x = 0.0;
|
let mut x = 0.0;
|
||||||
let mut a = false;
|
let mut a = false;
|
||||||
while !a {
|
while !a {
|
||||||
x = self.rng.gen_range(-1.0..=1.0) * ship.size / 2.0;
|
x = self.rng.gen_range(-1.0..=1.0) * ship.size / 2.0;
|
||||||
y = self.rng.gen_range(-1.0..=1.0) * ship.size * ship.sprite.aspect / 2.0;
|
y = self.rng.gen_range(-1.0..=1.0) * ship.size * ct.get_sprite(ship.sprite).aspect
|
||||||
|
/ 2.0;
|
||||||
a = collider.shape().contains_local_point(&Point2::new(x, y));
|
a = collider.shape().contains_local_point(&Point2::new(x, y));
|
||||||
}
|
}
|
||||||
Vector2::new(x, y)
|
Vector2::new(x, y)
|
||||||
|
@ -61,7 +62,7 @@ impl ShipCollapseSequence {
|
||||||
) {
|
) {
|
||||||
let rigid_body = wrapper.get_rigid_body(rigid_body_handle).unwrap().clone();
|
let rigid_body = wrapper.get_rigid_body(rigid_body_handle).unwrap().clone();
|
||||||
let collider = wrapper.get_collider(collider_handle).unwrap().clone();
|
let collider = wrapper.get_collider(collider_handle).unwrap().clone();
|
||||||
let ship_content = ship_data.get_content();
|
let ship_content = res.ct.get_ship(ship_data.get_content());
|
||||||
let ship_pos = rigid_body.translation();
|
let ship_pos = rigid_body.translation();
|
||||||
let ship_rot = rigid_body.rotation();
|
let ship_rot = rigid_body.rotation();
|
||||||
|
|
||||||
|
@ -83,13 +84,14 @@ impl ShipCollapseSequence {
|
||||||
let pos: Vector2<f32> = if let Some(pos) = spawner.pos {
|
let pos: Vector2<f32> = if let Some(pos) = spawner.pos {
|
||||||
Vector2::new(pos.x, pos.y)
|
Vector2::new(pos.x, pos.y)
|
||||||
} else {
|
} else {
|
||||||
self.random_in_ship(&ship_content, &collider)
|
self.random_in_ship(&res.ct, ship_content, &collider)
|
||||||
};
|
};
|
||||||
let pos = ship_pos + (ship_rot * pos);
|
let pos = ship_pos + (ship_rot * pos);
|
||||||
|
|
||||||
new.effects.push(PhysEffect::new(
|
new.effects.push(PhysEffect::new(
|
||||||
|
&res.ct,
|
||||||
wrapper,
|
wrapper,
|
||||||
spawner.effect.clone(),
|
spawner.effect,
|
||||||
pos,
|
pos,
|
||||||
rigid_body_handle,
|
rigid_body_handle,
|
||||||
None,
|
None,
|
||||||
|
@ -122,14 +124,15 @@ impl ShipCollapseSequence {
|
||||||
let pos = if let Some(pos) = spawner.pos {
|
let pos = if let Some(pos) = spawner.pos {
|
||||||
Vector2::new(pos.x, pos.y)
|
Vector2::new(pos.x, pos.y)
|
||||||
} else {
|
} else {
|
||||||
self.random_in_ship(&ship_content, &collider)
|
self.random_in_ship(&res.ct, ship_content, &collider)
|
||||||
};
|
};
|
||||||
|
|
||||||
// Position, adjusted for ship rotation
|
// Position, adjusted for ship rotation
|
||||||
let pos = ship_pos + (ship_rot * pos);
|
let pos = ship_pos + (ship_rot * pos);
|
||||||
new.effects.push(PhysEffect::new(
|
new.effects.push(PhysEffect::new(
|
||||||
|
&res.ct,
|
||||||
wrapper,
|
wrapper,
|
||||||
spawner.effect.clone(),
|
spawner.effect,
|
||||||
pos,
|
pos,
|
||||||
rigid_body_handle,
|
rigid_body_handle,
|
||||||
None,
|
None,
|
||||||
|
|
|
@ -25,7 +25,7 @@ impl PointShipController {
|
||||||
impl ShipControllerStruct for PointShipController {
|
impl ShipControllerStruct for PointShipController {
|
||||||
fn update_controls(
|
fn update_controls(
|
||||||
&mut self,
|
&mut self,
|
||||||
_res: &PhysStepResources,
|
res: &PhysStepResources,
|
||||||
img: &PhysImage,
|
img: &PhysImage,
|
||||||
this_ship: PhysSimShipHandle,
|
this_ship: PhysSimShipHandle,
|
||||||
) -> Option<ShipControls> {
|
) -> Option<ShipControls> {
|
||||||
|
@ -40,14 +40,14 @@ impl ShipControllerStruct for PointShipController {
|
||||||
let my_position = this_rigidbody.translation();
|
let my_position = this_rigidbody.translation();
|
||||||
let my_rotation = this_rigidbody.rotation();
|
let my_rotation = this_rigidbody.rotation();
|
||||||
let my_angvel = this_rigidbody.angvel();
|
let my_angvel = this_rigidbody.angvel();
|
||||||
let my_faction = my_ship.ship.data.get_faction();
|
let my_faction = res.ct.get_faction(my_ship.ship.data.get_faction());
|
||||||
|
|
||||||
// Iterate all possible targets
|
// Iterate all possible targets
|
||||||
let mut hostile_ships = img.iter_ships().filter(
|
let mut hostile_ships = img.iter_ships().filter(
|
||||||
// Target only flying ships we're hostile towards
|
// Target only flying ships we're hostile towards
|
||||||
|s| match my_faction
|
|s| match my_faction
|
||||||
.relationships
|
.relationships
|
||||||
.get(&s.ship.data.get_faction().index)
|
.get(&s.ship.data.get_faction())
|
||||||
.unwrap()
|
.unwrap()
|
||||||
{
|
{
|
||||||
Relationship::Hostile => match s.ship.data.get_state() {
|
Relationship::Hostile => match s.ship.data.get_state() {
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use galactica_content::{
|
use galactica_content::{
|
||||||
AnimationState, EnginePoint, Faction, GunPoint, Outfit, ProjectileCollider, Ship,
|
AnimationState, Content, EnginePoint, FactionHandle, GunPoint, OutfitHandle,
|
||||||
SpriteAutomaton,
|
ProjectileCollider, ShipHandle, SpriteAutomaton,
|
||||||
};
|
};
|
||||||
use nalgebra::{vector, Point2, Rotation2, Vector2};
|
use nalgebra::{vector, Point2, Rotation2, Vector2};
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
@ -68,22 +66,24 @@ pub struct PhysShip {
|
||||||
impl PhysShip {
|
impl PhysShip {
|
||||||
/// Make a new ship
|
/// Make a new ship
|
||||||
pub(crate) fn new(
|
pub(crate) fn new(
|
||||||
ship: Arc<Ship>,
|
ct: &Content,
|
||||||
|
handle: ShipHandle,
|
||||||
personality: ShipPersonality,
|
personality: ShipPersonality,
|
||||||
faction: Arc<Faction>,
|
faction: FactionHandle,
|
||||||
rigid_body: RigidBodyHandle,
|
rigid_body: RigidBodyHandle,
|
||||||
collider: ColliderHandle,
|
collider: ColliderHandle,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
|
let ship_ct = ct.get_ship(handle);
|
||||||
PhysShip {
|
PhysShip {
|
||||||
uid: get_phys_id(),
|
uid: get_phys_id(),
|
||||||
anim: SpriteAutomaton::new(ship.sprite.clone()),
|
anim: SpriteAutomaton::new(ct, ship_ct.sprite),
|
||||||
rigid_body,
|
rigid_body,
|
||||||
collider,
|
collider,
|
||||||
data: ShipData::new(ship.clone(), faction, personality),
|
data: ShipData::new(ct, handle, faction, personality),
|
||||||
engine_anim: Vec::new(),
|
engine_anim: Vec::new(),
|
||||||
controls: ShipControls::new(),
|
controls: ShipControls::new(),
|
||||||
last_controls: ShipControls::new(),
|
last_controls: ShipControls::new(),
|
||||||
collapse_sequence: Some(ShipCollapseSequence::new(ship.collapse.length)),
|
collapse_sequence: Some(ShipCollapseSequence::new(ship_ct.collapse.length)),
|
||||||
is_destroyed: false,
|
is_destroyed: false,
|
||||||
controller: match personality {
|
controller: match personality {
|
||||||
ShipPersonality::Dummy | ShipPersonality::Player => ShipController::new_null(),
|
ShipPersonality::Dummy | ShipPersonality::Player => ShipController::new_null(),
|
||||||
|
@ -105,28 +105,32 @@ impl PhysShip {
|
||||||
}
|
}
|
||||||
|
|
||||||
self.data.step(res.t);
|
self.data.step(res.t);
|
||||||
self.anim.step(res.t);
|
self.anim.step(&res.ct, res.t);
|
||||||
|
|
||||||
for (_, e) in &mut self.engine_anim {
|
for (_, e) in &mut self.engine_anim {
|
||||||
e.step(res.t);
|
e.step(&res.ct, res.t);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Flare animations
|
// Flare animations
|
||||||
if !self.controls.thrust && self.last_controls.thrust {
|
if !self.controls.thrust && self.last_controls.thrust {
|
||||||
let flare = self.get_flare();
|
let flare = self.get_flare(&res.ct);
|
||||||
if let Some(flare) = flare {
|
if flare.is_some() {
|
||||||
|
let flare_outfit = flare.unwrap();
|
||||||
|
let flare = res.ct.get_outfit(flare_outfit);
|
||||||
if flare.engine_flare_on_stop.is_some() {
|
if flare.engine_flare_on_stop.is_some() {
|
||||||
for (_, e) in &mut self.engine_anim {
|
for (_, e) in &mut self.engine_anim {
|
||||||
e.jump_to(flare.engine_flare_on_stop.as_ref().unwrap());
|
e.jump_to(&res.ct, flare.engine_flare_on_stop.unwrap());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
} else if self.controls.thrust && !self.last_controls.thrust {
|
} else if self.controls.thrust && !self.last_controls.thrust {
|
||||||
let flare = self.get_flare();
|
let flare = self.get_flare(&res.ct);
|
||||||
if let Some(flare) = flare {
|
if flare.is_some() {
|
||||||
|
let flare_outfit = flare.unwrap();
|
||||||
|
let flare = res.ct.get_outfit(flare_outfit);
|
||||||
if flare.engine_flare_on_start.is_some() {
|
if flare.engine_flare_on_start.is_some() {
|
||||||
for (_, e) in &mut self.engine_anim {
|
for (_, e) in &mut self.engine_anim {
|
||||||
e.jump_to(flare.engine_flare_on_start.as_ref().unwrap());
|
e.jump_to(&res.ct, flare.engine_flare_on_start.unwrap());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -158,11 +162,12 @@ impl PhysShip {
|
||||||
// Compute new controls
|
// Compute new controls
|
||||||
let controls = match autopilot {
|
let controls = match autopilot {
|
||||||
ShipAutoPilot::Landing { target } => {
|
ShipAutoPilot::Landing { target } => {
|
||||||
|
let target_obj = res.ct.get_system_object(*target);
|
||||||
let controls = autopilot::auto_landing(
|
let controls = autopilot::auto_landing(
|
||||||
res,
|
res,
|
||||||
img,
|
img,
|
||||||
PhysSimShipHandle(self.collider),
|
PhysSimShipHandle(self.collider),
|
||||||
Vector2::new(target.pos.x, target.pos.y),
|
Vector2::new(target_obj.pos.x, target_obj.pos.y),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Try to land the ship.
|
// Try to land the ship.
|
||||||
|
@ -171,7 +176,7 @@ impl PhysShip {
|
||||||
let landed = 'landed: {
|
let landed = 'landed: {
|
||||||
let r = wrapper.get_rigid_body(self.rigid_body).unwrap();
|
let r = wrapper.get_rigid_body(self.rigid_body).unwrap();
|
||||||
|
|
||||||
let t_pos = Vector2::new(target.pos.x, target.pos.y);
|
let t_pos = Vector2::new(target_obj.pos.x, target_obj.pos.y);
|
||||||
let s_pos = Vector2::new(
|
let s_pos = Vector2::new(
|
||||||
r.position().translation.x,
|
r.position().translation.x,
|
||||||
r.position().translation.y,
|
r.position().translation.y,
|
||||||
|
@ -181,13 +186,13 @@ impl PhysShip {
|
||||||
// Can't just set_active(false), since we still need that collider's mass.
|
// Can't just set_active(false), since we still need that collider's mass.
|
||||||
|
|
||||||
// We're in land range...
|
// We're in land range...
|
||||||
if (t_pos - s_pos).magnitude() > target.size / 2.0 {
|
if (t_pos - s_pos).magnitude() > target_obj.size / 2.0 {
|
||||||
break 'landed false;
|
break 'landed false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// And we'll stay in land range long enough.
|
// And we'll stay in land range long enough.
|
||||||
if (t_pos - (s_pos + r.velocity_at_point(r.center_of_mass()) * 2.0))
|
if (t_pos - (s_pos + r.velocity_at_point(r.center_of_mass()) * 2.0))
|
||||||
.magnitude() > target.size / 2.0
|
.magnitude() > target_obj.size / 2.0
|
||||||
{
|
{
|
||||||
break 'landed false;
|
break 'landed false;
|
||||||
}
|
}
|
||||||
|
@ -197,7 +202,7 @@ impl PhysShip {
|
||||||
Group::GROUP_1,
|
Group::GROUP_1,
|
||||||
Group::empty(),
|
Group::empty(),
|
||||||
));
|
));
|
||||||
self.data.start_land_on(target.clone());
|
self.data.start_land_on(*target);
|
||||||
break 'landed true;
|
break 'landed true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -224,7 +229,7 @@ impl PhysShip {
|
||||||
if self.controls.guns {
|
if self.controls.guns {
|
||||||
// TODO: don't allocate here. This is a hack to satisfy the borrow checker,
|
// TODO: don't allocate here. This is a hack to satisfy the borrow checker,
|
||||||
// convert this to a refcell or do the replace dance.
|
// convert this to a refcell or do the replace dance.
|
||||||
let pairs: Vec<(GunPoint, Option<Arc<Outfit>>)> = self
|
let pairs: Vec<(GunPoint, Option<OutfitHandle>)> = self
|
||||||
.data
|
.data
|
||||||
.get_outfits()
|
.get_outfits()
|
||||||
.iter_gun_points()
|
.iter_gun_points()
|
||||||
|
@ -232,7 +237,7 @@ impl PhysShip {
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
for (gun_point, outfit) in pairs {
|
for (gun_point, outfit) in pairs {
|
||||||
if self.data.fire_gun(&gun_point) {
|
if self.data.fire_gun(&res.ct, &gun_point) {
|
||||||
let outfit = outfit.unwrap();
|
let outfit = outfit.unwrap();
|
||||||
|
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
|
@ -245,17 +250,20 @@ impl PhysShip {
|
||||||
rigid_body.velocity_at_point(rigid_body.center_of_mass());
|
rigid_body.velocity_at_point(rigid_body.center_of_mass());
|
||||||
|
|
||||||
let pos = ship_pos + (ship_rot * gun_point.pos);
|
let pos = ship_pos + (ship_rot * gun_point.pos);
|
||||||
let gun = outfit.gun.as_ref().unwrap();
|
|
||||||
|
|
||||||
let spread =
|
let outfit = res.ct.get_outfit(outfit);
|
||||||
rng.gen_range(-gun.projectile.angle_rng..=gun.projectile.angle_rng);
|
let outfit = outfit.gun.as_ref().unwrap();
|
||||||
|
|
||||||
|
let spread = rng.gen_range(
|
||||||
|
-outfit.projectile.angle_rng..=outfit.projectile.angle_rng,
|
||||||
|
);
|
||||||
let vel = ship_vel
|
let vel = ship_vel
|
||||||
+ (Rotation2::new(ship_ang + spread)
|
+ (Rotation2::new(ship_ang + spread)
|
||||||
* Vector2::new(
|
* Vector2::new(
|
||||||
gun.projectile.speed
|
outfit.projectile.speed
|
||||||
+ rng.gen_range(
|
+ rng.gen_range(
|
||||||
-gun.projectile.speed_rng
|
-outfit.projectile.speed_rng
|
||||||
..=gun.projectile.speed_rng,
|
..=outfit.projectile.speed_rng,
|
||||||
),
|
),
|
||||||
0.0,
|
0.0,
|
||||||
));
|
));
|
||||||
|
@ -266,7 +274,7 @@ impl PhysShip {
|
||||||
.linvel(vel)
|
.linvel(vel)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let mut collider = match &gun.projectile.collider {
|
let mut collider = match &outfit.projectile.collider {
|
||||||
ProjectileCollider::Ball(b) => ColliderBuilder::ball(b.radius)
|
ProjectileCollider::Ball(b) => ColliderBuilder::ball(b.radius)
|
||||||
.sensor(true)
|
.sensor(true)
|
||||||
.active_events(ActiveEvents::COLLISION_EVENTS)
|
.active_events(ActiveEvents::COLLISION_EVENTS)
|
||||||
|
@ -282,9 +290,10 @@ impl PhysShip {
|
||||||
let collider = wrapper.insert_collider(collider, rigid_body);
|
let collider = wrapper.insert_collider(collider, rigid_body);
|
||||||
|
|
||||||
new.projectiles.push(PhysProjectile::new(
|
new.projectiles.push(PhysProjectile::new(
|
||||||
gun.projectile.clone(),
|
&res.ct,
|
||||||
|
outfit.projectile.clone(),
|
||||||
rigid_body,
|
rigid_body,
|
||||||
self.data.get_faction().clone(),
|
self.data.get_faction(),
|
||||||
collider,
|
collider,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
@ -297,6 +306,8 @@ impl PhysShip {
|
||||||
current_z,
|
current_z,
|
||||||
from,
|
from,
|
||||||
} => {
|
} => {
|
||||||
|
let from_obj = res.ct.get_system_object(*from);
|
||||||
|
|
||||||
let controls = autopilot::auto_landing(
|
let controls = autopilot::auto_landing(
|
||||||
&res,
|
&res,
|
||||||
img,
|
img,
|
||||||
|
@ -304,7 +315,7 @@ impl PhysShip {
|
||||||
Vector2::new(to_position.translation.x, to_position.translation.y),
|
Vector2::new(to_position.translation.x, to_position.translation.y),
|
||||||
);
|
);
|
||||||
let r = wrapper.get_rigid_body_mut(self.rigid_body).unwrap();
|
let r = wrapper.get_rigid_body_mut(self.rigid_body).unwrap();
|
||||||
let max_d = (Vector2::new(from.pos.x, from.pos.y)
|
let max_d = (Vector2::new(from_obj.pos.x, from_obj.pos.y)
|
||||||
- Vector2::new(to_position.translation.x, to_position.translation.y))
|
- Vector2::new(to_position.translation.x, to_position.translation.y))
|
||||||
.magnitude();
|
.magnitude();
|
||||||
let now_d = (r.translation()
|
let now_d = (r.translation()
|
||||||
|
@ -313,7 +324,7 @@ impl PhysShip {
|
||||||
let f = now_d / max_d;
|
let f = now_d / max_d;
|
||||||
|
|
||||||
let current_z = *current_z;
|
let current_z = *current_z;
|
||||||
let zdist = 1.0 - from.pos.z;
|
let zdist = 1.0 - from_obj.pos.z;
|
||||||
|
|
||||||
if current_z <= 1.0 {
|
if current_z <= 1.0 {
|
||||||
// Finish unlanding ship
|
// Finish unlanding ship
|
||||||
|
@ -340,17 +351,18 @@ impl PhysShip {
|
||||||
}
|
}
|
||||||
|
|
||||||
ShipState::Landing { target, current_z } => {
|
ShipState::Landing { target, current_z } => {
|
||||||
|
let target_obj = res.ct.get_system_object(*target);
|
||||||
let controls = autopilot::auto_landing(
|
let controls = autopilot::auto_landing(
|
||||||
&res,
|
&res,
|
||||||
img,
|
img,
|
||||||
PhysSimShipHandle(self.collider),
|
PhysSimShipHandle(self.collider),
|
||||||
Vector2::new(target.pos.x, target.pos.y),
|
Vector2::new(target_obj.pos.x, target_obj.pos.y),
|
||||||
);
|
);
|
||||||
|
|
||||||
let current_z = *current_z;
|
let current_z = *current_z;
|
||||||
let zdist = target.pos.z - 1.0;
|
let zdist = target_obj.pos.z - 1.0;
|
||||||
|
|
||||||
if current_z >= target.pos.z {
|
if current_z >= target_obj.pos.z {
|
||||||
// Finish landing ship
|
// Finish landing ship
|
||||||
self.data.finish_land_on();
|
self.data.finish_land_on();
|
||||||
let r = wrapper.get_rigid_body_mut(self.rigid_body).unwrap();
|
let r = wrapper.get_rigid_body_mut(self.rigid_body).unwrap();
|
||||||
|
@ -420,7 +432,7 @@ impl PhysShip {
|
||||||
let rigid_body = wrapper.get_rigid_body(self.rigid_body).unwrap().clone();
|
let rigid_body = wrapper.get_rigid_body(self.rigid_body).unwrap().clone();
|
||||||
let collider = wrapper.get_collider(self.collider).unwrap().clone();
|
let collider = wrapper.get_collider(self.collider).unwrap().clone();
|
||||||
|
|
||||||
let ship_content = self.data.get_content();
|
let ship_content = res.ct.get_ship(self.data.get_content());
|
||||||
let ship_pos = rigid_body.translation();
|
let ship_pos = rigid_body.translation();
|
||||||
let ship_rot = rigid_body.rotation();
|
let ship_rot = rigid_body.rotation();
|
||||||
let ship_ang = ship_rot.angle();
|
let ship_ang = ship_rot.angle();
|
||||||
|
@ -439,7 +451,7 @@ impl PhysShip {
|
||||||
while !a {
|
while !a {
|
||||||
x = rng.gen_range(-1.0..=1.0) * ship_content.size / 2.0;
|
x = rng.gen_range(-1.0..=1.0) * ship_content.size / 2.0;
|
||||||
y = rng.gen_range(-1.0..=1.0)
|
y = rng.gen_range(-1.0..=1.0)
|
||||||
* ship_content.size * ship_content.sprite.aspect
|
* ship_content.size * res.ct.get_sprite(ship_content.sprite).aspect
|
||||||
/ 2.0;
|
/ 2.0;
|
||||||
a = collider.shape().contains_local_point(&Point2::new(x, y));
|
a = collider.shape().contains_local_point(&Point2::new(x, y));
|
||||||
}
|
}
|
||||||
|
@ -449,8 +461,9 @@ impl PhysShip {
|
||||||
let pos = ship_pos + (Rotation2::new(ship_ang) * pos);
|
let pos = ship_pos + (Rotation2::new(ship_ang) * pos);
|
||||||
|
|
||||||
new.effects.push(PhysEffect::new(
|
new.effects.push(PhysEffect::new(
|
||||||
|
&res.ct,
|
||||||
wrapper,
|
wrapper,
|
||||||
e.effect.clone(),
|
e.effect,
|
||||||
pos,
|
pos,
|
||||||
self.rigid_body,
|
self.rigid_body,
|
||||||
None,
|
None,
|
||||||
|
@ -463,11 +476,12 @@ impl PhysShip {
|
||||||
|
|
||||||
/// Public mutable
|
/// Public mutable
|
||||||
impl PhysShip {
|
impl PhysShip {
|
||||||
fn get_flare(&mut self) -> Option<Arc<Outfit>> {
|
fn get_flare(&mut self, ct: &Content) -> Option<OutfitHandle> {
|
||||||
// TODO: better way to pick flare sprite
|
// TODO: better way to pick flare sprite
|
||||||
for (h, _) in self.data.get_outfits().iter_outfits() {
|
for (h, _) in self.data.get_outfits().iter_outfits() {
|
||||||
if h.engine_flare_sprite.is_some() {
|
let c = ct.get_outfit(*h);
|
||||||
return Some(h.clone());
|
if c.engine_flare_sprite.is_some() {
|
||||||
|
return Some(*h);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return None;
|
return None;
|
||||||
|
@ -475,47 +489,37 @@ impl PhysShip {
|
||||||
|
|
||||||
/// Re-create this ship's engine flare animations
|
/// Re-create this ship's engine flare animations
|
||||||
/// Should be called whenever we change outfits
|
/// Should be called whenever we change outfits
|
||||||
fn update_flares(&mut self) {
|
fn update_flares(&mut self, ct: &Content) {
|
||||||
let flare = self.get_flare();
|
let flare_outfit = self.get_flare(ct);
|
||||||
if flare.is_none() {
|
if flare_outfit.is_none() {
|
||||||
self.engine_anim = Vec::new();
|
self.engine_anim = Vec::new();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
let flare = ct
|
||||||
|
.get_outfit(flare_outfit.unwrap())
|
||||||
|
.engine_flare_sprite
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
self.engine_anim = self
|
self.engine_anim = ct
|
||||||
.data
|
.get_ship(self.data.get_content())
|
||||||
.get_content()
|
|
||||||
.engines
|
.engines
|
||||||
.iter()
|
.iter()
|
||||||
.map(|e| {
|
.map(|e| (e.clone(), SpriteAutomaton::new(ct, flare)))
|
||||||
(
|
|
||||||
e.clone(),
|
|
||||||
SpriteAutomaton::new(
|
|
||||||
flare
|
|
||||||
.as_ref()
|
|
||||||
.unwrap()
|
|
||||||
.engine_flare_sprite
|
|
||||||
.as_ref()
|
|
||||||
.unwrap()
|
|
||||||
.clone(),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.collect();
|
.collect();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add one outfit to this ship
|
/// Add one outfit to this ship
|
||||||
pub fn add_outfit(&mut self, o: Arc<Outfit>) {
|
pub fn add_outfit(&mut self, ct: &Content, o: OutfitHandle) {
|
||||||
self.data.add_outfit(&o);
|
self.data.add_outfit(ct.get_outfit(o));
|
||||||
self.update_flares();
|
self.update_flares(ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add many outfits to this ship
|
/// Add many outfits to this ship
|
||||||
pub fn add_outfits(&mut self, outfits: impl IntoIterator<Item = Arc<Outfit>>) {
|
pub fn add_outfits(&mut self, ct: &Content, outfits: impl IntoIterator<Item = OutfitHandle>) {
|
||||||
for o in outfits {
|
for o in outfits {
|
||||||
self.data.add_outfit(&o);
|
self.data.add_outfit(ct.get_outfit(o));
|
||||||
}
|
}
|
||||||
self.update_flares();
|
self.update_flares(ct);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
use galactica_content::Faction;
|
|
||||||
use galactica_content::Relationship;
|
use galactica_content::Relationship;
|
||||||
use galactica_content::Ship;
|
use galactica_content::{Content, FactionHandle, ShipHandle, SystemHandle};
|
||||||
use galactica_playeragent::PlayerAgent;
|
use galactica_playeragent::PlayerAgent;
|
||||||
use log::error;
|
|
||||||
use nalgebra::{Isometry2, Point2, Rotation2, Vector2};
|
use nalgebra::{Isometry2, Point2, Rotation2, Vector2};
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use rapier2d::{
|
use rapier2d::{
|
||||||
|
@ -10,7 +8,6 @@ use rapier2d::{
|
||||||
geometry::{ColliderHandle, Group, InteractionGroups},
|
geometry::{ColliderHandle, Group, InteractionGroups},
|
||||||
};
|
};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use super::PhysEffectImage;
|
use super::PhysEffectImage;
|
||||||
use super::{
|
use super::{
|
||||||
|
@ -18,7 +15,6 @@ use super::{
|
||||||
PhysImage, PhysProjectileImage, PhysShipImage, PhysStepResources, PhysWrapper,
|
PhysImage, PhysProjectileImage, PhysShipImage, PhysStepResources, PhysWrapper,
|
||||||
};
|
};
|
||||||
use crate::data::{ShipAutoPilot, ShipPersonality, ShipState};
|
use crate::data::{ShipAutoPilot, ShipPersonality, ShipState};
|
||||||
use crate::PlayerDirective;
|
|
||||||
|
|
||||||
// TODO: replace with a more generic handle
|
// TODO: replace with a more generic handle
|
||||||
/// A handle for a ship in this simulation
|
/// A handle for a ship in this simulation
|
||||||
|
@ -53,8 +49,13 @@ impl NewObjects {
|
||||||
|
|
||||||
/// Manages the physics state of one system
|
/// Manages the physics state of one system
|
||||||
pub struct PhysSim {
|
pub struct PhysSim {
|
||||||
|
/// The system this sim is attached to
|
||||||
|
_system: SystemHandle,
|
||||||
|
|
||||||
wrapper: PhysWrapper,
|
wrapper: PhysWrapper,
|
||||||
|
|
||||||
new: NewObjects,
|
new: NewObjects,
|
||||||
|
|
||||||
effects: Vec<PhysEffect>,
|
effects: Vec<PhysEffect>,
|
||||||
projectiles: HashMap<ColliderHandle, PhysProjectile>,
|
projectiles: HashMap<ColliderHandle, PhysProjectile>,
|
||||||
ships: HashMap<ColliderHandle, PhysShip>,
|
ships: HashMap<ColliderHandle, PhysShip>,
|
||||||
|
@ -62,9 +63,10 @@ pub struct PhysSim {
|
||||||
|
|
||||||
// Private methods
|
// Private methods
|
||||||
impl PhysSim {
|
impl PhysSim {
|
||||||
pub(super) fn start_unland_ship(&mut self, collider: ColliderHandle) {
|
pub(super) fn start_unland_ship(&mut self, ct: &Content, collider: ColliderHandle) {
|
||||||
let ship = self.ships.get_mut(&collider).unwrap();
|
let ship = self.ships.get_mut(&collider).unwrap();
|
||||||
let obj = ship.data.get_state().landed_on().unwrap();
|
let obj = ship.data.get_state().landed_on().unwrap();
|
||||||
|
let obj = ct.get_system_object(obj);
|
||||||
|
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
let radius = rng.gen_range(500.0..=1500.0);
|
let radius = rng.gen_range(500.0..=1500.0);
|
||||||
|
@ -73,7 +75,7 @@ impl PhysSim {
|
||||||
let target_trans = Vector2::new(obj.pos.x, obj.pos.y) + target_offset;
|
let target_trans = Vector2::new(obj.pos.x, obj.pos.y) + target_offset;
|
||||||
let target_pos = Isometry2::new(target_trans, angle);
|
let target_pos = Isometry2::new(target_trans, angle);
|
||||||
|
|
||||||
ship.data.start_unland_to(target_pos);
|
ship.data.start_unland_to(ct, target_pos);
|
||||||
|
|
||||||
let r = self.wrapper.get_rigid_body_mut(ship.rigid_body).unwrap();
|
let r = self.wrapper.get_rigid_body_mut(ship.rigid_body).unwrap();
|
||||||
r.set_enabled(true);
|
r.set_enabled(true);
|
||||||
|
@ -85,6 +87,7 @@ impl PhysSim {
|
||||||
|
|
||||||
pub(super) fn collide_projectile_ship(
|
pub(super) fn collide_projectile_ship(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
res: &mut PhysStepResources,
|
||||||
projectile_h: ColliderHandle,
|
projectile_h: ColliderHandle,
|
||||||
ship_h: ColliderHandle,
|
ship_h: ColliderHandle,
|
||||||
) {
|
) {
|
||||||
|
@ -96,11 +99,8 @@ impl PhysSim {
|
||||||
let projectile = projectile.unwrap();
|
let projectile = projectile.unwrap();
|
||||||
let ship = ship.unwrap();
|
let ship = ship.unwrap();
|
||||||
|
|
||||||
let r = projectile
|
let f = res.ct.get_faction(projectile.faction);
|
||||||
.faction
|
let r = f.relationships.get(&ship.data.get_faction()).unwrap();
|
||||||
.relationships
|
|
||||||
.get(&ship.data.get_faction().index)
|
|
||||||
.unwrap();
|
|
||||||
let destory_projectile = match r {
|
let destory_projectile = match r {
|
||||||
Relationship::Hostile => match ship.data.get_state() {
|
Relationship::Hostile => match ship.data.get_state() {
|
||||||
ShipState::Flying { .. } => {
|
ShipState::Flying { .. } => {
|
||||||
|
@ -131,8 +131,9 @@ impl PhysSim {
|
||||||
None => {}
|
None => {}
|
||||||
Some(x) => {
|
Some(x) => {
|
||||||
self.effects.push(PhysEffect::new(
|
self.effects.push(PhysEffect::new(
|
||||||
|
&res.ct,
|
||||||
&mut self.wrapper,
|
&mut self.wrapper,
|
||||||
x.clone(),
|
*x,
|
||||||
pos,
|
pos,
|
||||||
projectile.rigid_body,
|
projectile.rigid_body,
|
||||||
Some(ship.rigid_body),
|
Some(ship.rigid_body),
|
||||||
|
@ -140,7 +141,7 @@ impl PhysSim {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
projectile.destroy_silent(&mut self.new, &mut self.wrapper);
|
projectile.destroy_silent(res, &mut self.new, &mut self.wrapper);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -148,9 +149,11 @@ impl PhysSim {
|
||||||
// Public methods
|
// Public methods
|
||||||
impl PhysSim {
|
impl PhysSim {
|
||||||
/// Create a new physics system
|
/// Create a new physics system
|
||||||
pub fn new() -> Self {
|
pub fn new(_ct: &Content, system: SystemHandle) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
_system: system,
|
||||||
wrapper: PhysWrapper::new(),
|
wrapper: PhysWrapper::new(),
|
||||||
|
|
||||||
new: NewObjects::new(),
|
new: NewObjects::new(),
|
||||||
effects: Vec::new(),
|
effects: Vec::new(),
|
||||||
projectiles: HashMap::new(),
|
projectiles: HashMap::new(),
|
||||||
|
@ -161,17 +164,19 @@ impl PhysSim {
|
||||||
/// Add a ship to this physics system
|
/// Add a ship to this physics system
|
||||||
pub fn add_ship(
|
pub fn add_ship(
|
||||||
&mut self,
|
&mut self,
|
||||||
ship: Arc<Ship>,
|
ct: &Content,
|
||||||
faction: Arc<Faction>,
|
handle: ShipHandle,
|
||||||
|
faction: FactionHandle,
|
||||||
personality: ShipPersonality,
|
personality: ShipPersonality,
|
||||||
position: Point2<f32>,
|
position: Point2<f32>,
|
||||||
) -> PhysSimShipHandle {
|
) -> PhysSimShipHandle {
|
||||||
let mut cl = ship.collider.0.clone();
|
let ship_content = ct.get_ship(handle);
|
||||||
|
let mut cl = ship_content.collider.0.clone();
|
||||||
// TODO: additonal ship mass from outfits and cargo
|
// TODO: additonal ship mass from outfits and cargo
|
||||||
|
|
||||||
let rb = RigidBodyBuilder::dynamic()
|
let rb = RigidBodyBuilder::dynamic()
|
||||||
.angular_damping(ship.angular_drag)
|
.angular_damping(ship_content.angular_drag)
|
||||||
.linear_damping(ship.linear_drag)
|
.linear_damping(ship_content.linear_drag)
|
||||||
.translation(Vector2::new(position.x, position.y))
|
.translation(Vector2::new(position.x, position.y))
|
||||||
.can_sleep(false);
|
.can_sleep(false);
|
||||||
|
|
||||||
|
@ -185,14 +190,14 @@ impl PhysSim {
|
||||||
|
|
||||||
self.ships.insert(
|
self.ships.insert(
|
||||||
collider,
|
collider,
|
||||||
PhysShip::new(ship, personality, faction, ridid_body, collider),
|
PhysShip::new(ct, handle, personality, faction, ridid_body, collider),
|
||||||
);
|
);
|
||||||
|
|
||||||
return PhysSimShipHandle(collider);
|
return PhysSimShipHandle(collider);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Update a player ship's controls
|
/// Update a player ship's controls
|
||||||
pub fn apply_directive(&mut self, directive: PlayerDirective, player: &PlayerAgent) {
|
pub fn update_player_controls(&mut self, ct: &Content, player: &PlayerAgent) {
|
||||||
if player.ship.is_none() {
|
if player.ship.is_none() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -206,49 +211,35 @@ impl PhysSim {
|
||||||
|
|
||||||
ShipState::Flying {
|
ShipState::Flying {
|
||||||
autopilot: ShipAutoPilot::None,
|
autopilot: ShipAutoPilot::None,
|
||||||
} => match directive {
|
} => {
|
||||||
PlayerDirective::None => {}
|
ship_object.controls.guns = player.input.pressed_guns();
|
||||||
PlayerDirective::Engine(x) => ship_object.controls.thrust = x,
|
ship_object.controls.left = player.input.pressed_left();
|
||||||
PlayerDirective::TurnLeft(x) => ship_object.controls.left = x,
|
ship_object.controls.right = player.input.pressed_right();
|
||||||
PlayerDirective::TurnRight(x) => ship_object.controls.right = x,
|
ship_object.controls.thrust = player.input.pressed_thrust();
|
||||||
PlayerDirective::Guns(x) => ship_object.controls.guns = x,
|
|
||||||
PlayerDirective::Land => {
|
|
||||||
if let Some(target) = player.selection.get_planet() {
|
|
||||||
ship_object.data.set_autopilot(ShipAutoPilot::Landing {
|
|
||||||
target: target.clone(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
error!("Got an invalid directive {directive:?} in shipstate `Flying`");
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
ShipState::Flying { .. } => match directive {
|
if player.input.pressed_land() {
|
||||||
PlayerDirective::None => {}
|
ship_object.data.set_autopilot(ShipAutoPilot::Landing {
|
||||||
PlayerDirective::Engine(_)
|
target: player.selection.get_planet().unwrap(),
|
||||||
| PlayerDirective::TurnLeft(_)
|
})
|
||||||
| PlayerDirective::TurnRight(_)
|
}
|
||||||
| PlayerDirective::Land
|
}
|
||||||
| PlayerDirective::Guns(_) => {
|
|
||||||
// Disable autopilot and apply action
|
ShipState::Flying { .. } => {
|
||||||
|
// Any input automatically releases autopilot
|
||||||
|
if player.input.pressed_left()
|
||||||
|
|| player.input.pressed_right()
|
||||||
|
|| player.input.pressed_thrust()
|
||||||
|
|| player.input.pressed_guns()
|
||||||
|
{
|
||||||
ship_object.data.set_autopilot(ShipAutoPilot::None);
|
ship_object.data.set_autopilot(ShipAutoPilot::None);
|
||||||
self.apply_directive(directive, player);
|
|
||||||
}
|
}
|
||||||
_ => {
|
}
|
||||||
error!("Got an invalid directive {directive:?} in shipstate `Flying`");
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
ShipState::Landed { .. } => match directive {
|
ShipState::Landed { .. } => {
|
||||||
PlayerDirective::None => {}
|
if player.input.pressed_land() {
|
||||||
PlayerDirective::UnLand => {
|
self.start_unland_ship(ct, player.ship.unwrap());
|
||||||
self.start_unland_ship(player.ship.unwrap());
|
|
||||||
}
|
}
|
||||||
_ => {
|
}
|
||||||
error!("Got an invalid directive {directive:?} in shipstate `Landed`");
|
|
||||||
}
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -280,7 +271,7 @@ impl PhysSim {
|
||||||
if p.is_none() || s.is_none() {
|
if p.is_none() || s.is_none() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
self.collide_projectile_ship(a, b);
|
self.collide_projectile_ship(&mut res, a, b);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
res.timing.mark_physics_step();
|
res.timing.mark_physics_step();
|
||||||
|
|
|
@ -1,24 +0,0 @@
|
||||||
/// An action the player wants to take in the game.
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub enum PlayerDirective {
|
|
||||||
/// Do nothing
|
|
||||||
None,
|
|
||||||
|
|
||||||
/// Set main engine state
|
|
||||||
Engine(bool),
|
|
||||||
|
|
||||||
/// Set left turn thruster state
|
|
||||||
TurnLeft(bool),
|
|
||||||
|
|
||||||
/// Set right turn thruster state
|
|
||||||
TurnRight(bool),
|
|
||||||
|
|
||||||
/// Set main gun state
|
|
||||||
Guns(bool),
|
|
||||||
|
|
||||||
/// Land on the currently selected planet
|
|
||||||
Land,
|
|
||||||
|
|
||||||
/// Take off from the planet we're landed on
|
|
||||||
UnLand,
|
|
||||||
}
|
|
Loading…
Reference in New Issue