Skip to content

Commit

Permalink
random dropped spells
Browse files Browse the repository at this point in the history
  • Loading branch information
aratama committed Nov 19, 2024
1 parent ef672d3 commit 5f6f0d0
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 36 deletions.
12 changes: 2 additions & 10 deletions src/entity/dropped_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use bevy::core::FrameCount;
use bevy::prelude::*;
use bevy_aseprite_ultra::prelude::*;
use bevy_rapier2d::prelude::*;
use rand::random;

#[derive(Component)]
pub struct DroppedItemEntity {
Expand All @@ -27,12 +26,9 @@ struct InteractionMarker;
pub fn spawn_dropped_item(
commands: &mut Commands,
assets: &Res<GameAssets>,
x: f32,
y: f32,
position: Vec2,
item_type: InventoryItem,
) {
let tx = x;
let ty = y;
let icon = match item_type {
InventoryItem::Spell(spell) => {
let props = spell_to_props(spell);
Expand Down Expand Up @@ -85,11 +81,7 @@ pub fn spawn_dropped_item(
},
EntityDepth,
InheritedVisibility::default(),
Transform::from_translation(Vec3::new(
tx + (random::<f32>() - 0.5) * 16.0,
ty + (random::<f32>() - 0.5) * 16.0,
0.0,
)),
Transform::from_translation(Vec3::new(position.x, position.y, 0.0)),
GlobalTransform::default(),
LockedAxes::ROTATION_LOCKED,
RigidBody::Dynamic,
Expand Down
9 changes: 3 additions & 6 deletions src/inventory_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,23 @@ pub fn spawn_inventory_item(
spawn_dropped_item(
&mut commands,
&assets,
position.x,
position.y,
position,
InventoryItem::Spell(spell),
);
}
InventoryItem::Wand(wand_type) => {
spawn_dropped_item(
&mut commands,
&assets,
position.x,
position.y,
position,
InventoryItem::Wand(wand_type),
);
}
InventoryItem::Equipment(equipment_type) => {
spawn_dropped_item(
&mut commands,
&assets,
position.x,
position.y,
position,
InventoryItem::Equipment(equipment_type),
);
}
Expand Down
30 changes: 22 additions & 8 deletions src/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ use crate::level::map::LevelTileMap;
use crate::level::tile::*;
use crate::player_state::PlayerState;
use crate::random::random_select;
use crate::random::random_select_mut;
use crate::spell::SpellType;
use crate::spell::SPELL_TYPES;
use crate::states::GameState;
use crate::wand::WandType;
use bevy::asset::*;
Expand Down Expand Up @@ -91,7 +93,7 @@ fn setup_level(
level,
);

let entry_point = random_select(&mut chunk.entry_points);
let entry_point = random_select_mut(&mut chunk.entry_points);

let player_x = TILE_SIZE * entry_point.x as f32 + TILE_HALF;
let player_y = -TILE_SIZE * entry_point.y as f32 - TILE_HALF;
Expand Down Expand Up @@ -208,9 +210,9 @@ fn spawn_level(

spawn_entities(&mut commands, &assets, &chunk);

if 20 < empties.len() {
if 30 < empties.len() {
for _ in 0..10 {
let (x, y) = random_select(&mut empties);
let (x, y) = random_select_mut(&mut empties);
spawn_slime(
&mut commands,
&assets,
Expand All @@ -223,7 +225,7 @@ fn spawn_level(
}

for _ in 0..10 {
let (x, y) = random_select(&mut empties);
let (x, y) = random_select_mut(&mut empties);
spawn_eyeball(
&mut commands,
&assets,
Expand All @@ -234,6 +236,20 @@ fn spawn_level(
&life_bar_res,
);
}

let mut spells = Vec::from(SPELL_TYPES);
for _ in 0..3 {
let (x, y) = random_select_mut(&mut empties);
spawn_dropped_item(
&mut commands,
&assets,
Vec2::new(
TILE_SIZE * x as f32 + TILE_HALF,
TILE_SIZE * -y as f32 - TILE_HALF,
),
InventoryItem::Spell(*random_select(&mut spells)),
);
}
}

return chunk;
Expand Down Expand Up @@ -404,17 +420,15 @@ fn spawn_entities(mut commands: &mut Commands, assets: &Res<GameAssets>, chunk:
spawn_dropped_item(
&mut commands,
&assets,
tx + TILE_HALF,
ty - TILE_HALF,
Vec2::new(tx + TILE_HALF, ty - TILE_HALF),
InventoryItem::Spell(SpellType::MagicBolt),
);
}
GameEntity::Wand => {
spawn_dropped_item(
&mut commands,
&assets,
tx + TILE_HALF,
ty - TILE_HALF,
Vec2::new(tx + TILE_HALF, ty - TILE_HALF),
InventoryItem::Wand(WandType::CypressWand),
);
}
Expand Down
7 changes: 6 additions & 1 deletion src/random.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
pub fn random_select<T: Copy>(xs: &mut Vec<T>) -> T {
pub fn random_select_mut<T: Copy>(xs: &mut Vec<T>) -> T {
xs.remove((rand::random::<usize>() % xs.len()) as usize)
}

pub fn random_select<T: Copy>(xs: &Vec<T>) -> &T {
xs.get((rand::random::<usize>() % xs.len()) as usize)
.unwrap()
}
12 changes: 12 additions & 0 deletions src/spell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,15 @@ pub enum SpellType {
TripleCast,
Homing,
}

pub const SPELL_TYPES: [SpellType; 9] = [
SpellType::MagicBolt,
SpellType::PurpleBolt,
SpellType::SlimeCharge,
SpellType::Heal,
SpellType::BulletSpeedUp,
SpellType::BulletSpeedDoown,
SpellType::DualCast,
SpellType::TripleCast,
SpellType::Homing,
];
6 changes: 3 additions & 3 deletions src/spell_props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,10 @@ const BULLET_SPEED_DOWN: SpellProps = SpellProps {

const DUAL_CAST: SpellProps = SpellProps {
name: Dict {
ja: "二重呪文",
ja: "並列詠唱",
en: "Dual Cast",
},
description: Dict { ja: "ふたつの投射物呪文を同時に詠唱します。",
description: Dict { ja: "ふたつの投射物呪文を同時に詠唱します。詠唱遅延は大きいほうに揃えられます。",
en: "Casts two projectile spells at the same time." },
mana_drain: 20,
cast_delay: 0,
Expand All @@ -177,7 +177,7 @@ const DUAL_CAST: SpellProps = SpellProps {

const TRIPLE_CAST: SpellProps = SpellProps {
name: Dict {
ja: "三重呪文",
ja: "三並列詠唱",
en: "Triple Cast",
},
description: Dict { ja: "みっつの投射物呪文を同時に詠唱します。",
Expand Down
3 changes: 1 addition & 2 deletions src/ui/inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,7 @@ fn interaction(
spawn_dropped_item(
&mut commands,
&assets,
player_position.translation.x,
player_position.translation.y,
player_position.translation.truncate(),
InventoryItem::Spell(spell),
);
}
Expand Down
9 changes: 3 additions & 6 deletions src/ui/wand_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,15 @@ fn drop_item(
spawn_dropped_item(
&mut commands,
&assets,
dest.x,
dest.y,
dest,
InventoryItem::Spell(spell),
);
}
}
spawn_dropped_item(
&mut commands,
&assets,
dest.x,
dest.y,
dest,
InventoryItem::Wand(wand.wand_type),
);
actor.wands[index] = None;
Expand All @@ -196,8 +194,7 @@ fn drop_item(
spawn_dropped_item(
&mut commands,
&assets,
pointer_in_world.x,
pointer_in_world.y,
pointer_in_world,
InventoryItem::Spell(spell),
);
wand.slots[spell_index] = None;
Expand Down

0 comments on commit 5f6f0d0

Please sign in to comment.