Skip to content

Commit

Permalink
equipment
Browse files Browse the repository at this point in the history
  • Loading branch information
aratama committed Nov 16, 2024
1 parent c8b9d0f commit b050a95
Show file tree
Hide file tree
Showing 16 changed files with 317 additions and 203 deletions.
2 changes: 2 additions & 0 deletions src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub const MAX_ITEMS_IN_INVENTORY_COLUMN: usize = 8;
pub const MAX_ITEMS_IN_INVENTORY: usize =
MAX_ITEMS_IN_INVENTORY_ROW * MAX_ITEMS_IN_INVENTORY_COLUMN;

pub const MAX_ITEMS_IN_EQUIPMENT: usize = 8;

/// level.aseprite のスライスの最大値 - 1
pub const LEVELS: i32 = 4;

Expand Down
21 changes: 11 additions & 10 deletions src/controller/player.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::asset::GameAssets;
use crate::command::GameCommand;
use crate::constant::MAX_WANDS;
use crate::constant::{MAX_ITEMS_IN_EQUIPMENT, MAX_WANDS};
use crate::controller::remote::RemoteMessage;
use crate::entity::actor::{Actor, ActorFireState};
use crate::entity::gold::{spawn_gold, Gold};
use crate::equipment::Equipment;
use crate::input::{get_direction, get_fire_trigger, MyGamepad};
use crate::inventory::Inventory;
use crate::states::{GameMenuState, GameState};
Expand All @@ -26,6 +27,7 @@ pub struct Player {
pub last_idle_life: i32,
pub last_idle_max_life: i32,
pub inventory: Inventory,
pub equipments: [Option<Equipment>; MAX_ITEMS_IN_EQUIPMENT],
}

/// プレイヤーの移動
Expand All @@ -49,14 +51,13 @@ fn move_player(
}
}

fn switch_intensity(
keys: Res<ButtonInput<KeyCode>>,
mut player_query: Query<&mut Actor, (With<Player>, Without<Camera2d>)>,
) {
if keys.just_pressed(KeyCode::KeyQ) {
if let Ok(mut actor) = player_query.get_single_mut() {
actor.intensity = if actor.intensity == 0.0 { 3.0 } else { 0.0 };
}
fn apply_intensity_by_lantern(mut player_query: Query<(&Player, &mut Actor)>) {
if let Ok((player, mut actor)) = player_query.get_single_mut() {
let equiped_lantern = player.equipments.iter().any(|e| match e {
Some(Equipment::Lantern) => true,
_ => false,
});
actor.intensity = if equiped_lantern { 3.0 } else { 0.0 };
}
}

Expand Down Expand Up @@ -182,7 +183,7 @@ impl Plugin for PlayerPlugin {
trigger_bullet,
pick_gold,
die_player,
switch_intensity,
apply_intensity_by_lantern,
switch_wand,
)
.run_if(in_state(GameState::InGame))
Expand Down
15 changes: 11 additions & 4 deletions src/entity/dropped_item.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::entity::EntityDepth;
use crate::equipment::equipment_to_props;
use crate::inventory_item::InventoryItem;
use crate::spell_props::spell_to_props;
use crate::wand_props::wand_to_props;
Expand Down Expand Up @@ -41,7 +42,10 @@ pub fn spawn_dropped_item(
let props = wand_to_props(wand);
props.icon
}
InventoryItem::Lantern => "lantern",
InventoryItem::Equipment(equipment) => {
let props = equipment_to_props(equipment);
props.icon
}
};
let name = match item_type {
InventoryItem::Spell(spell) => {
Expand All @@ -52,12 +56,15 @@ pub fn spawn_dropped_item(
let props = wand_to_props(wand);
props.name.en
}
InventoryItem::Lantern => "lantern",
InventoryItem::Equipment(equipment) => {
let props = equipment_to_props(equipment);
props.name.en
}
};
let frame_slice = match item_type {
InventoryItem::Wand(_) => "empty", //"wand_frame",
InventoryItem::Spell(_) => "spell_frame",
InventoryItem::Lantern => "empty",
InventoryItem::Equipment(_) => "empty",
};
let collider_width = match item_type {
InventoryItem::Wand(_) => 16.0,
Expand All @@ -66,7 +73,7 @@ pub fn spawn_dropped_item(
let swing = match item_type {
InventoryItem::Spell(_) => 2.0,
InventoryItem::Wand(_) => 0.0,
InventoryItem::Lantern => 0.0,
InventoryItem::Equipment(_) => 0.0,
};
commands
.spawn((
Expand Down
28 changes: 28 additions & 0 deletions src/equipment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use crate::language::Dict;

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Equipment {
Lantern,
}

pub struct EquipmentProps {
pub icon: &'static str,
pub name: Dict,
pub description: Dict,
}

pub fn equipment_to_props(equipment: Equipment) -> EquipmentProps {
match equipment {
Equipment::Lantern => EquipmentProps {
icon: "lantern",
name: Dict {
en: "Lantern",
ja: "ランタン",
},
description: Dict {
ja: "暗闇を照らすランタン",
en: "A lantern that illuminates the darkness",
},
},
}
}
24 changes: 18 additions & 6 deletions src/inventory.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use bevy::log::info;

use crate::{
constant::{MAX_ITEMS_IN_INVENTORY, MAX_ITEMS_IN_INVENTORY_COLUMN, MAX_ITEMS_IN_INVENTORY_ROW},
inventory_item::InventoryItem,
Expand All @@ -24,6 +22,14 @@ impl Inventory {
inventory[index] = item;
}

pub fn try_set(&mut self, index: usize, item: InventoryItem) -> bool {
if self.is_settable(index, item) {
self.set(index, Some(item));
return true;
}
return false;
}

pub fn is_settable(&self, index: usize, item: InventoryItem) -> bool {
let x = index % MAX_ITEMS_IN_INVENTORY_ROW;
let y = index / MAX_ITEMS_IN_INVENTORY_ROW;
Expand All @@ -50,10 +56,16 @@ impl Inventory {

pub fn insert(&mut self, item: InventoryItem) -> bool {
let Inventory(ref mut inventory) = *self;
for i in 0..MAX_ITEMS_IN_INVENTORY {
if inventory[i].is_none() {
inventory[i] = Some(item);
return true;
let mut i = 0;
while i < MAX_ITEMS_IN_INVENTORY {
match inventory[i] {
None => {
inventory[i] = Some(item);
return true;
}
Some(item) => {
i += item.get_width();
}
}
}
return false;
Expand Down
36 changes: 21 additions & 15 deletions src/inventory_item.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
asset::GameAssets,
entity::dropped_item::spawn_dropped_item,
equipment::{equipment_to_props, Equipment},
language::{Dict, Languages},
spell::SpellType,
spell_props::{get_spell_appendix, spell_to_props},
Expand All @@ -13,15 +14,23 @@ use bevy::prelude::*;
pub enum InventoryItem {
Wand(WandType),
Spell(SpellType),
Lantern,
Equipment(Equipment),
}

impl InventoryItem {
pub fn get_width(&self) -> usize {
match self {
InventoryItem::Spell(_) => 1,
InventoryItem::Wand(_) => 2,
InventoryItem::Lantern => 1,
InventoryItem::Equipment(_) => 1,
}
}

pub fn get_icon(&self) -> &'static str {
match self {
InventoryItem::Spell(spell) => spell_to_props(*spell).icon,
InventoryItem::Wand(wand) => wand_to_props(*wand).icon,
InventoryItem::Equipment(equipment) => equipment_to_props(*equipment).icon,
}
}
}
Expand Down Expand Up @@ -51,13 +60,13 @@ pub fn spawn_inventory_item(
InventoryItem::Wand(wand_type),
);
}
InventoryItem::Lantern => {
InventoryItem::Equipment(equipment_type) => {
spawn_dropped_item(
&mut commands,
&assets,
position.x,
position.y,
InventoryItem::Lantern,
InventoryItem::Equipment(equipment_type),
);
}
}
Expand Down Expand Up @@ -87,17 +96,14 @@ pub fn inventory_item_to_props(item: InventoryItem) -> InventoryItemProps {
description: props.description,
}
}
InventoryItem::Lantern => InventoryItemProps {
icon: "lantern",
name: Dict {
ja: "ランタン",
en: "Lantern",
},
description: Dict {
ja: "暗闇を照らすランタン",
en: "A lantern that illuminates the darkness",
},
},
InventoryItem::Equipment(equipment) => {
let props = equipment_to_props(equipment);
InventoryItemProps {
icon: props.icon,
name: props.name,
description: props.description,
}
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod constant;
mod controller;
mod enemy;
mod entity;
mod equipment;
mod game;
mod hud;
mod input;
Expand Down
75 changes: 72 additions & 3 deletions src/ui/equipment_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ use bevy::{
};
use bevy_aseprite_ultra::prelude::*;

use crate::{asset::GameAssets, constant::MAX_SPELLS_IN_WAND};
use crate::{
asset::GameAssets, constant::MAX_SPELLS_IN_WAND, controller::player::Player,
equipment::equipment_to_props, inventory_item::InventoryItem, states::GameState,
};

use super::wand_list::slot_color;
use super::{
floating::{Floating, FloatingContent},
wand_list::slot_color,
};

#[derive(Component)]
pub struct EquipmentContainer;
Expand Down Expand Up @@ -63,8 +69,71 @@ fn spawn_equipment_slot(parent: &mut ChildBuilder, assets: &Res<GameAssets>, ind
));
}

fn update_equipment_sprite(
mut sprite_query: Query<(&EquipmentSprite, &mut AsepriteSlice)>,
player_query: Query<&Player>,
floating_query: Query<&mut Floating>,
) {
if let Ok(player) = player_query.get_single() {
for (sprite, mut slice) in sprite_query.iter_mut() {
let float = floating_query.single();
let picked = match float.0 {
Some(FloatingContent::Equipment(index)) => index == sprite.index,
_ => false,
};
if picked {
*slice = AsepriteSlice::new("empty");
} else if let Some(equipment) = player.equipments[sprite.index] {
let props = equipment_to_props(equipment);
*slice = AsepriteSlice::new(props.icon);
} else {
*slice = AsepriteSlice::new("empty");
}
}
}
}

fn interact(
sprite_query: Query<(&EquipmentSprite, &Interaction), Changed<Interaction>>,
mut player_query: Query<&mut Player>,
mut floating_query: Query<&mut Floating>,
) {
if let Ok(mut player) = player_query.get_single_mut() {
for (sprite, interaction) in sprite_query.iter() {
let mut floating = floating_query.single_mut();
match interaction {
Interaction::Pressed => match floating.0 {
None => {
*floating = Floating(Some(FloatingContent::Equipment(sprite.index)));
}
Some(FloatingContent::Inventory(index)) => match player.inventory.get(index) {
Some(InventoryItem::Equipment(equipment)) => {
player.equipments[sprite.index] = Some(equipment);
player.inventory.set(index, None);
*floating = Floating(None);
}
_ => {}
},
Some(FloatingContent::Wand(index)) => {}
Some(FloatingContent::WandSpell { .. }) => {}
Some(FloatingContent::Equipment(index)) => {
player.equipments.swap(index, sprite.index);
*floating = Floating(None);
}
},
_ => {}
}
}
}
}

pub struct EquipmentListPlugin;

impl Plugin for EquipmentListPlugin {
fn build(&self, _app: &mut App) {}
fn build(&self, app: &mut App) {
app.add_systems(
Update,
(update_equipment_sprite, interact).run_if(in_state(GameState::InGame)),
);
}
}
Loading

0 comments on commit b050a95

Please sign in to comment.