Commit 3947622b authored by neosam's avatar neosam
Browse files

Merge branch 'rahix/sprite-drop-points' into 'master'

Use sprites to draw bottle drop points

See merge request !31
parents 1651cea3 c12ef4fe
...@@ -2,6 +2,7 @@ use crate::colliders; ...@@ -2,6 +2,7 @@ use crate::colliders;
use crate::components; use crate::components;
use crate::entities; use crate::entities;
use crate::resources; use crate::resources;
use crate::sprites;
use crate::svg_loader; use crate::svg_loader;
pub struct BottleAngelShift { pub struct BottleAngelShift {
...@@ -66,8 +67,9 @@ impl BottleAngelState { ...@@ -66,8 +67,9 @@ impl BottleAngelState {
#[legion::system] #[legion::system]
#[read_component(colliders::Collider)] #[read_component(colliders::Collider)]
#[read_component(components::Matebottledrop)] #[read_component(components::Matebottledrop)]
#[write_component(components::Sprite)]
pub fn collect_bottledrops( pub fn collect_bottledrops(
world: &legion::world::SubWorld, world: &mut legion::world::SubWorld,
cmd: &mut legion::systems::CommandBuffer, cmd: &mut legion::systems::CommandBuffer,
#[resource] player: &resources::Player, #[resource] player: &resources::Player,
#[resource] collision_world: &colliders::CollisionWorld, #[resource] collision_world: &colliders::CollisionWorld,
...@@ -79,7 +81,7 @@ pub fn collect_bottledrops( ...@@ -79,7 +81,7 @@ pub fn collect_bottledrops(
.get(world, player.0) .get(world, player.0)
.unwrap(); .unwrap();
let mut bottledrops = <&components::Matebottledrop>::query(); let mut bottledrops = <(&components::Matebottledrop, &mut components::Sprite)>::query();
for pair in collision_world for pair in collision_world
.world .world
.proximities_with(collider.handle.unwrap(), false) .proximities_with(collider.handle.unwrap(), false)
...@@ -90,8 +92,9 @@ pub fn collect_bottledrops( ...@@ -90,8 +92,9 @@ pub fn collect_bottledrops(
} }
let entity = *collision_world.world.objects.get(pair.1).unwrap().data(); let entity = *collision_world.world.objects.get(pair.1).unwrap().data();
if bottledrops.get(world, entity).is_ok() { if let Ok((_, sprite)) = bottledrops.get_mut(world, entity) {
cmd.remove(entity); *sprite = components::Sprite::new(sprites::Sprite::BottleDropPointEmpty);
cmd.remove_component::<components::Matebottledrop>(entity);
bottle_angel_state.collected_drops += 1; bottle_angel_state.collected_drops += 1;
} }
} }
......
use crate::resources; use crate::resources;
use nalgebra;
pub struct Sprite { pub struct Sprite {
pub image_handle: resources::ImageHandle, pub image_handle: resources::ImageHandle,
pub center: nalgebra::Vector2<f32>, pub offset: nalgebra::Vector2<f32>,
} }
impl Sprite { impl Sprite {
pub fn new(image_handle: resources::ImageHandle, center: nalgebra::Vector2<f32>) -> Self { pub fn new<S: Into<resources::ImageHandle>>(handle: S) -> Self {
Self::with_offset(handle, nalgebra::Vector2::new(0.0, 0.0))
}
pub fn with_offset<S: Into<resources::ImageHandle>>(
handle: S,
offset: nalgebra::Vector2<f32>,
) -> Self {
Sprite { Sprite {
image_handle, image_handle: handle.into(),
center, offset,
} }
} }
} }
use crate::colliders; use crate::colliders;
use crate::components; use crate::components;
use crate::sprites;
use crate::svg_loader; use crate::svg_loader;
use rand::seq::SliceRandom; use rand::seq::SliceRandom;
...@@ -17,6 +18,7 @@ pub fn create_drop_points(world: &mut legion::World, level: &svg_loader::SvgLeve ...@@ -17,6 +18,7 @@ pub fn create_drop_points(world: &mut legion::World, level: &svg_loader::SvgLeve
components::Matebottledrop, components::Matebottledrop,
components::Position::new(droppoint.x, droppoint.y), components::Position::new(droppoint.x, droppoint.y),
colliders::Collider::new_sensor_circle(50.0), colliders::Collider::new_sensor_circle(50.0),
components::Sprite::new(sprites::Sprite::BottleDropPointFull),
)); ));
} }
} }
use crate::colliders; use crate::colliders;
use crate::components; use crate::components;
use crate::resources;
use crate::sprites; use crate::sprites;
use crate::svg_loader; use crate::svg_loader;
...@@ -8,7 +7,6 @@ use rand::seq::SliceRandom; ...@@ -8,7 +7,6 @@ use rand::seq::SliceRandom;
pub fn create_player( pub fn create_player(
world: &mut legion::World, world: &mut legion::World,
resources: &legion::Resources,
level: &svg_loader::SvgLevel, level: &svg_loader::SvgLevel,
player: components::Player, player: components::Player,
) -> legion::Entity { ) -> legion::Entity {
...@@ -17,15 +15,12 @@ pub fn create_player( ...@@ -17,15 +15,12 @@ pub fn create_player(
.get("player") .get("player")
.expect("no player spawns in this map"); .expect("no player spawns in this map");
let spawn = spawn_locations.choose(&mut rand::thread_rng()).unwrap(); let spawn = spawn_locations.choose(&mut rand::thread_rng()).unwrap();
let image: resources::ImageHandle = sprites::Sprite::Player.into();
let rendering = resources.get::<resources::Rendering>().unwrap();
let center = rendering.get_image_size(&image).unwrap() / 2.0;
world.push(( world.push((
player, player,
components::Position::new(spawn.x, spawn.y), components::Position::new(spawn.x, spawn.y),
components::Movable::new(), components::Movable::new(),
colliders::Collider::new_player(50.0), colliders::Collider::new_player(50.0),
components::Sprite::new(image, center), components::Sprite::new(sprites::Sprite::Player),
)) ))
} }
...@@ -4,12 +4,16 @@ use crate::svg_loader; ...@@ -4,12 +4,16 @@ use crate::svg_loader;
#[derive(Copy, Clone, Debug, Eq, PartialEq)] #[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Sprite { pub enum Sprite {
Player, Player,
BottleDropPointFull,
BottleDropPointEmpty,
} }
impl Sprite { impl Sprite {
pub fn filename(&self) -> &'static str { pub fn filename(&self) -> &'static str {
match *self { match *self {
Sprite::Player => "player.svg", Sprite::Player => "player.svg",
Sprite::BottleDropPointFull => "droppoint-full.svg",
Sprite::BottleDropPointEmpty => "droppoint-empty.svg",
} }
} }
...@@ -19,18 +23,26 @@ impl Sprite { ...@@ -19,18 +23,26 @@ impl Sprite {
return; return;
} }
let filename = self.filename(); let filename = self.filename();
let image_url = format!("resources/{}", filename); let image_url = format!("resources/sprites/{}", filename);
let image = svg_loader::load_svg_as_image(&image_url).await; let image = svg_loader::load_svg_as_image(&image_url).await;
renderer renderer
.register_image_on_handle(image, &image_handle) .register_image_on_handle(image, &image_handle)
.expect("ImageHandle was registered twice - this should not happen"); .expect("ImageHandle was registered twice - this should not happen");
} }
pub async fn load_and_register_all(renderer: &mut resources::Rendering) {
for sprite in &[
Sprite::Player,
Sprite::BottleDropPointFull,
Sprite::BottleDropPointEmpty,
] {
sprite.load_and_register(renderer).await;
}
}
} }
impl Into<resources::ImageHandle> for Sprite { impl Into<resources::ImageHandle> for Sprite {
fn into(self) -> resources::ImageHandle { fn into(self) -> resources::ImageHandle {
resources::ImageHandle(match self { resources::ImageHandle(self as usize)
Sprite::Player => 1,
})
} }
} }
...@@ -38,7 +38,7 @@ impl InGameState { ...@@ -38,7 +38,7 @@ impl InGameState {
resources.insert(resources::Camera::new(1920.0, 1080.0)); resources.insert(resources::Camera::new(1920.0, 1080.0));
resources.insert(colliders::CollisionWorld::new()); resources.insert(colliders::CollisionWorld::new());
let player = entities::create_player(&mut world, &resources, &level, player); let player = entities::create_player(&mut world, &level, player);
let obstacles = entities::create_stationary_obstacles(&mut world, &level); let obstacles = entities::create_stationary_obstacles(&mut world, &level);
resources.insert(resources::Player(player)); resources.insert(resources::Player(player));
...@@ -65,7 +65,6 @@ impl InGameState { ...@@ -65,7 +65,6 @@ impl InGameState {
.add_thread_local(systems::draw_level_layer_system(background)) .add_thread_local(systems::draw_level_layer_system(background))
.add_thread_local(systems::draw_sprites_system()) .add_thread_local(systems::draw_sprites_system())
.add_thread_local(systems::draw_level_layer_system(foreground)) .add_thread_local(systems::draw_level_layer_system(foreground))
.add_thread_local(systems::draw_tmp_matebottledrop_system())
.add_thread_local(systems::draw_tmp_stationary_obstacles_system()) .add_thread_local(systems::draw_tmp_stationary_obstacles_system())
// .add_thread_local(systems::draw_debug_colliders_system()) // .add_thread_local(systems::draw_debug_colliders_system())
; ;
......
...@@ -60,9 +60,7 @@ impl gamestate::State for LevelLoadingState { ...@@ -60,9 +60,7 @@ impl gamestate::State for LevelLoadingState {
.await .await
.unwrap(); .unwrap();
sprites::Sprite::Player sprites::Sprite::load_and_register_all(&mut rendering).await;
.load_and_register(&mut rendering)
.await;
handle.do_transition(gamestate::Transition::replace(states::InGameState::new( handle.do_transition(gamestate::Transition::replace(states::InGameState::new(
level, level,
......
...@@ -4,7 +4,6 @@ mod level; ...@@ -4,7 +4,6 @@ mod level;
mod moving; mod moving;
mod player; mod player;
mod sprite; mod sprite;
mod tmp_matebottledrop;
mod tmp_stationary_obstacles; mod tmp_stationary_obstacles;
pub use camera::camera_system; pub use camera::camera_system;
...@@ -13,5 +12,4 @@ pub use level::draw_level_layer_system; ...@@ -13,5 +12,4 @@ pub use level::draw_level_layer_system;
pub use moving::{move_camera_to_player_system, move_movable_system}; pub use moving::{move_camera_to_player_system, move_movable_system};
pub use player::{sanity_goes_up_and_down_system, update_sanity_bar_system}; pub use player::{sanity_goes_up_and_down_system, update_sanity_bar_system};
pub use sprite::draw_sprites_system; pub use sprite::draw_sprites_system;
pub use tmp_matebottledrop::draw_tmp_matebottledrop_system;
pub use tmp_stationary_obstacles::draw_tmp_stationary_obstacles_system; pub use tmp_stationary_obstacles::draw_tmp_stationary_obstacles_system;
...@@ -9,7 +9,8 @@ pub fn draw_sprites( ...@@ -9,7 +9,8 @@ pub fn draw_sprites(
position: &components::Position, position: &components::Position,
sprite: &components::Sprite, sprite: &components::Sprite,
) { ) {
let upper_left = position.0 - sprite.center; let spritesize = rendering.get_image_size(&sprite.image_handle).unwrap();
let upper_left = position.0 - spritesize / 2.0 + sprite.offset;
rendering rendering
.draw_image( .draw_image(
&sprite.image_handle, &sprite.image_handle,
......
use crate::colors;
use crate::components;
use crate::resources;
#[legion::system(for_each)]
pub fn draw_tmp_matebottledrop(
_player: &components::Matebottledrop,
pos: &components::Position,
#[resource] rendering: &mut resources::Rendering,
) {
rendering.begin_path();
rendering.set_fill_style(&colors::PRIMARY3_SHADE4);
rendering.arc(
pos.0.x as f64,
pos.0.y as f64,
50.0,
0.0,
std::f64::consts::TAU,
);
rendering.fill();
}
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="100"
height="100"
viewBox="0 0 100 100"
version="1.1"
id="svg8"
inkscape:version="1.0.1 (3bc2e813f5, 2020-09-07)"
sodipodi:docname="droppoint-empty.svg">
<defs
id="defs2" />
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="7.9195959"
inkscape:cx="56.580318"
inkscape:cy="48.350229"
inkscape:document-units="mm"
inkscape:current-layer="layer1"
inkscape:document-rotation="0"
showgrid="true"
units="px"
inkscape:window-width="2560"
inkscape:window-height="1381"
inkscape:window-x="1920"
inkscape:window-y="0"
inkscape:window-maximized="1">
<inkscape:grid
type="xygrid"
id="grid833"
spacingx="5"
spacingy="5" />
</sodipodi:namedview>
<metadata
id="metadata5">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1">
<path
style="fill:#05b9ec;fill-opacity:0.567125;stroke:#05b9ec;stroke-width:5;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
d="m 5,60 5,35 h 80 l 5,-35 z"
id="path837"
sodipodi:nodetypes="ccccc" />
</g>
</svg>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="100"
height="100"
viewBox="0 0 100 100"
version="1.1"
id="svg8"
inkscape:version="1.0.1 (3bc2e813f5, 2020-09-07)"
sodipodi:docname="droppoint-full.svg">
<defs
id="defs2" />
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="7.9195959"
inkscape:cx="56.580318"
inkscape:cy="48.350229"
inkscape:document-units="mm"
inkscape:current-layer="layer1"
inkscape:document-rotation="0"
showgrid="true"
units="px"
inkscape:window-width="2560"
inkscape:window-height="1381"
inkscape:window-x="1920"
inkscape:window-y="0"
inkscape:window-maximized="1">
<inkscape:grid
type="xygrid"
id="grid833"
spacingx="5"
spacingy="5" />
</sodipodi:namedview>
<metadata
id="metadata5">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1">
<path
style="fill:#67029f;stroke:#b239ff;stroke-width:2.5;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
d="M 17.564745,63.618788 31.71766,68.588193 40,45 l -3.061171,-6.374107 3.312937,-9.435278 -4.71764,-1.656466 -3.312935,9.435276 -6.374108,3.061171 z"
id="path847" />
<path
style="fill:#440069;stroke:#b239ff;stroke-width:2.5;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
d="M 65.300491,62.012624 80,65 l 4.978959,-24.499183 -3.904045,-5.895628 1.991585,-9.799675 -4.899838,-0.995789 -1.991583,9.799672 -5.89563,3.904045 z"
id="path849" />
<path
style="fill:#41008b;stroke:#6800e7;stroke-width:2.5;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
d="M 71.276857,57.202736 80,45 59.662105,30.461429 l -6.975292,1.159864 -8.13516,-5.815429 -2.907712,4.067581 8.135158,5.815427 1.159864,6.975295 z"
id="path851" />
<path
style="fill:#41008b;stroke:#6800e7;stroke-width:2.5;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
d="M 61.047367,63.427098 70.223254,75.293146 90,60 l 0.89672,-7.013978 7.910699,-6.117259 -3.05863,-3.955349 -7.910698,6.117258 -7.013979,-0.89672 z"
id="path843" />
<path
style="fill:#67029f;stroke:#b239ff;stroke-width:2.5;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
d="M 60.782065,76.089672 73.905108,68.824292 61.796141,46.952554 55,45 l -4.843587,-8.748695 -4.374348,2.421794 4.843587,8.748695 -1.952554,6.796141 z"
id="path841" />
<path
style="fill:#440069;stroke:#b239ff;stroke-width:2.5;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
d="M 20,75 33.123043,67.73462 21.014076,45.862882 14.217935,43.910328 9.374348,35.161633 5,37.583427 9.843587,46.332122 7.891033,53.128263 Z"
id="path839" />
<path
style="fill:#2a005e;stroke:#6800e7;stroke-width:2.5;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
d="m 23.209276,64.396426 9.175887,11.866048 19.776746,-15.293146 0.89672,-7.013978 7.910699,-6.117259 -3.05863,-3.955349 L 50,50 42.986021,49.10328 Z"
id="path845" />
<path
style="fill:#05b9ec;fill-opacity:0.567125;stroke:#05b9ec;stroke-width:5;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
d="m 5,60 5,35 h 80 l 5,-35 z"
id="path837"
sodipodi:nodetypes="ccccc" />
</g>
</svg>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment