From 293c0b17330490b5ed8630c27a82046f04c7d952 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Wed, 6 Dec 2023 18:17:14 +0100 Subject: [PATCH] minor cleanup --- src/buildings.rs | 4 ++-- src/random.rs | 4 ++-- src/turret.rs | 32 +++++++++++++++++--------------- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/buildings.rs b/src/buildings.rs index cc7c7e8..841ab1f 100644 --- a/src/buildings.rs +++ b/src/buildings.rs @@ -152,14 +152,14 @@ pub struct CreateItemDefVisual { impl EntityCommand for CreateItemDefVisual { fn apply(self, id: Entity, world: &mut World) { let assets = world.get_resource::().unwrap(); - let visual = (MaterialMesh2dBundle { + let visual = MaterialMesh2dBundle { mesh: assets.mesh_def[&self.item_type.0].clone(), transform: Transform::default().with_scale(Vec3::splat( ITEM_VISUAL_SIZE * assets.size_def[&self.item_type.1], )), material: assets.color_def[&self.item_type.2].clone(), ..default() - }); + }; world.entity_mut(id).insert(visual); } } diff --git a/src/random.rs b/src/random.rs index da74d2e..fbb37e9 100644 --- a/src/random.rs +++ b/src/random.rs @@ -1,7 +1,7 @@ use bevy::prelude::*; use rand::prelude::*; use rand_chacha::ChaCha20Rng; -use std::marker::PhantomData; + #[derive(Component)] pub struct RandomDeterministic { @@ -12,7 +12,7 @@ pub struct RandomDeterministic { impl Default for RandomDeterministic { fn default() -> Self { let seed = 0; //thread_rng().gen::(); - return Self::new_from_seed(seed); + Self::new_from_seed(seed) } } diff --git a/src/turret.rs b/src/turret.rs index c92dddb..85be249 100644 --- a/src/turret.rs +++ b/src/turret.rs @@ -1,11 +1,10 @@ use std::{f32::consts::FRAC_PI_2, time::Duration}; use crate::{ - buildings::{self, ItemType}, + buildings::{self}, bullet::SpawnBullet, enemy::Enemy, - grid::{HexCell, HexGrid}, - inventory::{self, Inventory}, + grid::{HexGrid}, primitives::{ target::{SourceWithTargetAccessor, Target}, view::{ @@ -16,7 +15,7 @@ use crate::{ GameState, }; use bevy::{ - ecs::system::{CommandQueue, EntityCommand, SystemState}, + ecs::system::{EntityCommand, SystemState}, math::Vec3, prelude::*, sprite::SpriteBundle, @@ -80,32 +79,35 @@ impl EntityCommand for SpawnTurret { let mut new_item = || { let (mut q_inventory, q_items) = state.get_mut(world); - let (mut rng, mut inventory) = q_inventory.single_mut(); + let (_rng, mut inventory) = q_inventory.single_mut(); - let Some(first_item) = inventory.items.front() else { + let Some(first_item) = inventory.items.front().cloned() else { return None; }; - let Ok(item_to_build) = q_items.get(*first_item) else { + let Ok(_item_to_build) = q_items.get(first_item) else { return None; }; // TODO: check if we can build item_to_build (cooldown, space available, currency, ...) // TODO: send an event if not possible. // TODO: pay "price" ? inventory.items.pop_front(); - drop(inventory); + + /* let new_item = buildings::get_random_item(&mut rng); - let mut queue = CommandQueue::default(); - let new_item = world.spawn(new_item).id(); - Some(new_item) + let new_item = world.spawn(new_item).id();*/ + Some((first_item, first_item)) }; - let Some(new_item) = new_item() else { + let Some((item_built, _new_item)) = new_item() else { return; }; - let (mut q_inventory, q_items) = state.get_mut(world); - let (rng, mut inventory) = q_inventory.single_mut(); + // TODO: reuse that entity to merge it with turret entity ? + world.despawn(item_built); + let (mut q_inventory, _q_items) = state.get_mut(world); + let (_rng, _inventory) = q_inventory.single_mut(); + + //inventory.items.push_back(new_item); - inventory.items.push_back(new_item); let texture = world.resource_scope(|_, asset_server: Mut| { asset_server.load("textures/DifferentTurrets/Turret01.png") });