Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stat and attack update #57

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ai/simple_ai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ fn attack_action(
attack_writer.send(AttackEvent {
grid_positions,
cell_type: CellType::Player(*actor),
damage: 69
});

attack_indicator.hidden = true;
Expand Down
4 changes: 3 additions & 1 deletion src/attack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub struct AttackEvent {
pub grid_positions: Vec<IVec2>,
/// target cell type to hit
pub cell_type: CellType,
pub damage: u32
}

pub struct AttackPlugin;
Expand All @@ -75,6 +76,7 @@ fn process_attack(
for AttackEvent {
grid_positions,
cell_type,
damage
} in events.iter()
{
for grid_position in grid_positions.iter() {
Expand All @@ -86,7 +88,7 @@ fn process_attack(

match cell_entity {
CellType::Enemy(entity) => {
writer.send(DamageEnemyEvent { entity: *entity });
writer.send(DamageEnemyEvent { entity: *entity, damage: *damage});
},
CellType::Player(entity) => {
info!("player hit!");
Expand Down
37 changes: 29 additions & 8 deletions src/buffs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::time::Duration;

use bevy::prelude::*;
use bevy_bobs::component::health::Health;
use serde::Deserialize;

//Types of effects
//Overtime ticking effect ex. Poison
Expand All @@ -24,34 +25,54 @@ impl Modifier{
}
#[derive(Clone,Copy)]
pub struct ModifiedStats{
health: Health,
armor: i32,
speed: i32,
damage: i32,
crit: f32
pub health: Health,
pub armor: i32,
pub speed: i32,
pub damage: u32,
pub crit: f32
}
impl ModifiedStats {
}
#[derive(Deserialize,Copy,Clone)]
pub struct StatsPrefab {
max_health: u32,
armor: i32,
speed: i32,
damage: u32,
crit: f32,
}


#[derive(Component)]
pub struct Stats {
health: Health,
armor: i32,
speed: i32,
damage: i32,
damage: u32,
crit: f32,
modifiers: Vec<Modifier>
}
impl Stats{
pub fn apply_modifiers(self)->ModifiedStats{
pub fn new(prefab:StatsPrefab)->Self{
Self{
health: Health::new(prefab.max_health),
armor: prefab.armor,
speed: prefab.speed,
damage: prefab.damage,
crit: prefab.crit,
modifiers: Vec::new()
}

}
pub fn apply_modifiers(&self)->ModifiedStats{
let mut m_stats = ModifiedStats{
health: self.health,
armor: self.armor,
speed: self.speed,
damage: self.damage,
crit: self.crit
};
for modifier in self.modifiers{
for modifier in self.modifiers.iter(){
modifier.apply(m_stats);
}
return m_stats;
Expand Down
11 changes: 7 additions & 4 deletions src/enemy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use crate::{
spritesheet_constants::SpriteIndex,
ui::{attack_animation::SpawnAttackAnimEvent, attack_indicator::AttackIndicator},
utils::{some_or_continue, Dir},
weapon::CurrentWeapon,
weapon::{CurrentWeapon, WeaponPrefab},
};

#[derive(Component)]
Expand All @@ -48,6 +48,7 @@ pub struct SpawnEnemyEvent {

pub struct DamageEnemyEvent {
pub entity: Entity,
pub damage: u32
}

pub struct EnemyPlugin;
Expand All @@ -69,6 +70,7 @@ fn spawn(
mut events: EventReader<SpawnEnemyEvent>,
asset_sheet: Res<SpriteSheet>,
prefab_lib: Res<PrefabLib<EnemyPrefab>>,
weapon_lib: Res<PrefabLib<WeaponPrefab>>
) {
for SpawnEnemyEvent {
spawn_pos,
Expand Down Expand Up @@ -96,7 +98,8 @@ fn spawn(
.insert(GridEntity::new(*spawn_pos, CellType::Enemy(id)))
.insert(Movement::new())
.insert(AttackIndicator::default())
.insert(CurrentWeapon(prefab.weapon_id.to_owned()))
.insert(CurrentWeapon(weapon_lib.get(&prefab.weapon_id.to_owned()).unwrap().clone()))

.insert(Enemy)
.insert(Health::new(prefab.health))
.insert(DropTable::new(prefab.drops.clone()));
Expand Down Expand Up @@ -136,10 +139,10 @@ fn take_damage(
mut query: Query<(&mut Health, Option<&DropTable>, &GridEntity)>,
mut writer: EventWriter<SpawnDroppedItemEvent>,
) {
for DamageEnemyEvent { entity } in events.iter() {
for DamageEnemyEvent { entity, damage } in events.iter() {
let (mut health, droptable, grid_entity) = query.get_mut(*entity).unwrap();

health.take(1);
health.take(*damage);
if health.is_zero() {
cmd.entity(*entity).despawn_recursive();

Expand Down
7 changes: 5 additions & 2 deletions src/enviro/arrow_trap.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bevy::prelude::*;
use bevy_bobs::prefab::PrefabLib;
use bevy_ecs_ldtk::{prelude::FieldValue, EntityInstance};
use iyes_loopless::prelude::*;

Expand All @@ -11,7 +12,7 @@ use crate::{
map::ldtk_to_bevy,
ui::{attack_indicator::AttackIndicator, projectile::SpawnProjectileEvent},
utils::{to_rotation, Dir},
weapon::CurrentWeapon,
weapon::{CurrentWeapon, WeaponPrefab},
};

// how many turns between each attack
Expand Down Expand Up @@ -56,6 +57,7 @@ fn ai(
writer.send(AttackEvent {
grid_positions,
cell_type: CellType::Player(entity),
damage: 69
});

projectile_writer.send(SpawnProjectileEvent {
Expand All @@ -80,6 +82,7 @@ fn spawn_from_ldtk(
mut cmd: Commands,
query: Query<(Entity, &EntityInstance), Added<EntityInstance>>,
asset_sheet: Res<SpriteSheet>,
weapon_lib: Res<PrefabLib<WeaponPrefab>>
) {
for (entity, entity_instance) in query.iter().filter(|(_, t)| t.identifier == "ArrowTrap") {
// TODO this code is sorta cringe
Expand Down Expand Up @@ -117,7 +120,7 @@ fn spawn_from_ldtk(
})
.insert(ArrowTrap::new(dir))
.insert(AttackIndicator { dir, ..default() })
.insert(CurrentWeapon("arrow_trap".into()))
.insert(CurrentWeapon(weapon_lib.get("arrow_trap").unwrap().clone()))
.insert(GridEntity {
pos: grid_coords,
value: CellType::Wall,
Expand Down
2 changes: 1 addition & 1 deletion src/player/inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub struct Inventory {
pub armor: PrefabId,
pub ability: PrefabId,
pub slots: Vec<PrefabId>,
capactiy: u32,
capacity: u32,
}

impl Inventory {}
26 changes: 20 additions & 6 deletions src/player/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ use crate::{
floating_text::FloatingText, move_indicator::MoveIndicator,
},
utils::{cardinal_dirs, ok_or_return, some_or_continue, Dir},
weapon::CurrentWeapon,
weapon::{CurrentWeapon, WeaponPrefab, Damage},
buffs::Stats

};

#[derive(Component)]
Expand Down Expand Up @@ -96,7 +98,8 @@ fn spawn(
mut cmd: Commands,
mut events: EventReader<SpawnPlayerEvent>,
asset_sheet: Res<SpriteSheet>,
prefab_lib: Res<PrefabLib<PlayerPrefab>>,
player_lib: Res<PrefabLib<PlayerPrefab>>,
weapon_lib: Res<PrefabLib<WeaponPrefab>>
) {
for SpawnPlayerEvent {
spawn_pos,
Expand All @@ -117,7 +120,8 @@ fn spawn(
(KeyCode::E, PlayerAction::Interact),
]);

let prefab = some_or_continue!(prefab_lib.get(prefab_id));
let prefab = some_or_continue!(player_lib.get(prefab_id));
let weapon_prefab = weapon_lib.get(&prefab.default_weapon.to_owned());

let id = cmd.spawn().id();

Expand All @@ -136,13 +140,13 @@ fn spawn(
})
.insert(Player)
.insert(GridEntity::new(*spawn_pos, CellType::Player(id)))
.insert(Health::new(prefab.health))
.insert(Stats::new(prefab.base_stats))
.insert_bundle(InputManagerBundle::<PlayerAction> {
action_state: ActionState::default(),
input_map,
})
.insert(CameraFollow)
.insert(CurrentWeapon(prefab.default_weapon.to_owned()))
.insert(CurrentWeapon(weapon_lib.get(&prefab.default_weapon.to_owned()).unwrap().clone()))
.insert(Movement::new());

// ui related
Expand All @@ -154,6 +158,7 @@ fn spawn(
// offset: Vec2::new(0., 8.),
// ..default()
// });

}
}

Expand Down Expand Up @@ -250,14 +255,16 @@ fn attack_controller(
&mut AttackIndicator,
&GridEntity,
&ActionState<PlayerAction>,
&Stats,
&CurrentWeapon
),
With<Player>,
>,
mut writer: EventWriter<AttackEvent>,
mut anim_writer: EventWriter<SpawnAttackAnimEvent>,
mut player_moved: EventWriter<PlayerMovedEvent>,
) {
let (entity, mut attack_indicator, grid_entity, action_state) =
let (entity, mut attack_indicator, grid_entity, action_state, stats, weapon) =
ok_or_return!(query.get_single_mut());

if action_state.just_pressed(PlayerAction::Up) {
Expand Down Expand Up @@ -293,10 +300,17 @@ fn attack_controller(
spawn_pos: *pos,
});
}

//calculate damage




writer.send(AttackEvent {
grid_positions,
cell_type: CellType::Enemy(entity),
damage: stats.apply_modifiers().damage+weapon.damage.value()

});

player_moved.send(PlayerMovedEvent);
Expand Down
11 changes: 9 additions & 2 deletions src/player/prefab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use bevy_bobs::prefab::{PrefabId, PrefabLib};
use serde::Deserialize;

use crate::spritesheet_constants::SpriteIndex;
use crate::buffs::StatsPrefab;

#[derive(Deserialize, Component)]
pub enum Class {
Expand All @@ -13,12 +14,12 @@ pub enum Class {

#[derive(Deserialize)]
pub struct PlayerPrefab {
pub health: u32,
pub class: Class,
pub sprite_index: SpriteIndex,
pub default_weapon: PrefabId,
pub default_ability: PrefabId,
pub default_armor: PrefabId,
pub base_stats: StatsPrefab
}

pub struct PrefabPlugin;
Expand All @@ -32,12 +33,18 @@ impl Plugin for PrefabPlugin {
const RON_STRING: &str = r#"
{
"samurai": (
health: 10,
class: Samurai,
sprite_index: Player,
default_weapon: "hammer",
default_ability: "",
default_armor: "",
base_stats: (
max_health: 10,
armor: 2,
speed: 1,
damage: 2,
crit: 0.5
)
)
}
"#;
21 changes: 5 additions & 16 deletions src/ui/attack_indicator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,10 @@ pub struct AttackIndicatorPlugin;
impl Plugin for AttackIndicatorPlugin {
fn build(&self, app: &mut App) {
app.add_system(spawn)
.add_system(sync_attack_pattern)
.add_system(render);
.add_system(sync_attack_pattern);
}
}

fn render(query: Query<(&AttackIndicator, &GridEntity)>) {
for (attack_indicator, grid_position) in &query {
let pos: Vec<IVec2> = attack_indicator
.get_pattern()
.iter()
.map(|v| *v + grid_position.pos)
.collect();
}
}

fn spawn(
mut cmd: Commands,
Expand All @@ -69,7 +59,7 @@ fn spawn(
>,
child_query: Query<&AttackIndicatorRoot>,
) {
for (entity, attack_indictor, children) in &query {
for (entity, attack_indicator, children) in &query {
// despawn existing
if let Some(children) = children {
for child in children.iter() {
Expand All @@ -82,7 +72,7 @@ fn spawn(
}
}

if attack_indictor.hidden {
if attack_indicator.hidden {
continue;
}

Expand All @@ -97,7 +87,7 @@ fn spawn(
cmd.entity(entity).push_children(&[root]);

// spawn children
for offset in attack_indictor
for offset in attack_indicator
.get_pattern()
.iter()
.map(|v| v.as_vec2().extend(0.) * (TILE_SIZE as f32))
Expand Down Expand Up @@ -126,7 +116,6 @@ fn sync_attack_pattern(
prefabs: Res<PrefabLib<WeaponPrefab>>,
) {
for (cur_weapon, mut attack_indicator) in &mut query {
let prefab = prefabs.get(&cur_weapon).unwrap();
attack_indicator.pattern = prefab.attack_pattern;
attack_indicator.pattern = cur_weapon.attack_pattern;
}
}
Loading