npc layers support

This commit is contained in:
Alula 2021-04-28 00:39:31 +02:00
parent f0780479c2
commit 753e7304a3
No known key found for this signature in database
GPG Key ID: 3E00485503A1D8BA
4 changed files with 47 additions and 20 deletions

View File

@ -4,7 +4,7 @@ use crate::caret::CaretType;
use crate::common::{Direction, Rect}; use crate::common::{Direction, Rect};
use crate::framework::error::GameResult; use crate::framework::error::GameResult;
use crate::npc::list::NPCList; use crate::npc::list::NPCList;
use crate::npc::NPC; use crate::npc::{NPC, NPCLayer};
use crate::player::Player; use crate::player::Player;
use crate::rng::RNG; use crate::rng::RNG;
use crate::shared_game_state::SharedGameState; use crate::shared_game_state::SharedGameState;
@ -495,6 +495,7 @@ impl NPC {
pub(crate) fn tick_n041_busted_door(&mut self, state: &mut SharedGameState) -> GameResult { pub(crate) fn tick_n041_busted_door(&mut self, state: &mut SharedGameState) -> GameResult {
if self.action_num == 0 { if self.action_num == 0 {
self.action_num = 1; self.action_num = 1;
self.layer = NPCLayer::Background;
self.anim_rect = state.constants.npc.n041_busted_door; self.anim_rect = state.constants.npc.n041_busted_door;
self.y -= 16 * 0x200; self.y -= 16 * 0x200;
} }

View File

@ -49,6 +49,14 @@ bitfield! {
pub show_damage, set_show_damage: 15; // 0x8000 pub show_damage, set_show_damage: 15; // 0x8000
} }
#[derive(Debug, Copy, Clone, Eq, PartialOrd, PartialEq)]
#[repr(u8)]
pub enum NPCLayer {
Background = 0,
Middleground = 1,
Foreground = 2,
}
/// Represents an NPC object. /// Represents an NPC object.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
#[repr(C)] #[repr(C)]
@ -72,6 +80,7 @@ pub struct NPC {
/// Previous Y position, used by frame interpolator /// Previous Y position, used by frame interpolator
pub prev_y: i32, pub prev_y: i32,
pub exp: u16, pub exp: u16,
pub layer: NPCLayer,
pub size: u8, pub size: u8,
pub shock: u16, pub shock: u16,
pub life: u16, pub life: u16,
@ -115,6 +124,7 @@ impl NPC {
prev_x: 0, prev_x: 0,
prev_y: 0, prev_y: 0,
exp: 0, exp: 0,
layer: NPCLayer::Middleground,
size: 0, size: 0,
shock: 0, shock: 0,
life: 0, life: 0,
@ -140,6 +150,14 @@ impl NPC {
rng: Xoroshiro32PlusPlus::new(0), rng: Xoroshiro32PlusPlus::new(0),
} }
} }
pub fn draw_if_layer(&self, state: &mut SharedGameState, ctx: &mut Context, frame: &Frame, layer: NPCLayer) -> GameResult {
if self.layer == layer {
self.draw(state, ctx, frame)?
}
Ok(())
}
} }
impl GameEntity<([&mut Player; 2], &NPCList, &mut Stage, &BulletManager)> for NPC { impl GameEntity<([&mut Player; 2], &NPCList, &mut Stage, &BulletManager)> for NPC {

View File

@ -6,7 +6,7 @@ use crate::weapon::bullet::Bullet;
use crate::caret::CaretType; use crate::caret::CaretType;
use crate::common::{Condition, Direction, Flag, Rect}; use crate::common::{Condition, Direction, Flag, Rect};
use crate::map::NPCData; use crate::map::NPCData;
use crate::npc::{NPC, NPCFlag, NPCTable}; use crate::npc::{NPC, NPCFlag, NPCTable, NPCLayer};
use crate::npc::list::NPCList; use crate::npc::list::NPCList;
use crate::player::Player; use crate::player::Player;
use crate::rng::{RNG, Xoroshiro32PlusPlus}; use crate::rng::{RNG, Xoroshiro32PlusPlus};
@ -61,6 +61,7 @@ impl NPC {
event_num: 0, event_num: 0,
shock: 0, shock: 0,
exp, exp,
layer: NPCLayer::Middleground,
size, size,
life, life,
damage, damage,

View File

@ -22,7 +22,7 @@ use crate::input::touch_controls::TouchControlType;
use crate::inventory::{Inventory, TakeExperienceResult}; use crate::inventory::{Inventory, TakeExperienceResult};
use crate::npc::boss::BossNPC; use crate::npc::boss::BossNPC;
use crate::npc::list::NPCList; use crate::npc::list::NPCList;
use crate::npc::NPC; use crate::npc::{NPC, NPCLayer};
use crate::physics::PhysicalEntity; use crate::physics::PhysicalEntity;
use crate::player::{Player, TargetPlayer}; use crate::player::{Player, TargetPlayer};
use crate::rng::XorShift; use crate::rng::XorShift;
@ -212,6 +212,28 @@ impl GameScene {
Ok(()) Ok(())
} }
fn draw_npc_layer(&self, state: &mut SharedGameState, ctx: &mut Context, layer: NPCLayer) -> GameResult {
for npc in self.npc_list.iter_alive() {
if npc.layer != layer || npc.x < (self.frame.x - 128 * 0x200 - npc.display_bounds.width() as i32 * 0x200)
|| npc.x
> (self.frame.x
+ 128 * 0x200
+ (state.canvas_size.0 as i32 + npc.display_bounds.width() as i32) * 0x200)
&& npc.y < (self.frame.y - 128 * 0x200 - npc.display_bounds.height() as i32 * 0x200)
|| npc.y
> (self.frame.y
+ 128 * 0x200
+ (state.canvas_size.1 as i32 + npc.display_bounds.height() as i32) * 0x200)
{
continue;
}
npc.draw(state, ctx, &self.frame)?;
}
Ok(())
}
fn draw_bullets(&self, state: &mut SharedGameState, ctx: &mut Context) -> GameResult { fn draw_bullets(&self, state: &mut SharedGameState, ctx: &mut Context) -> GameResult {
let batch = state.texture_set.get_or_load_batch(ctx, &state.constants, "Bullet")?; let batch = state.texture_set.get_or_load_batch(ctx, &state.constants, "Bullet")?;
let mut x: i32; let mut x: i32;
@ -1552,6 +1574,7 @@ impl Scene for GameScene {
//graphics::set_canvas(ctx, Some(&state.game_canvas)); //graphics::set_canvas(ctx, Some(&state.game_canvas));
self.draw_background(state, ctx)?; self.draw_background(state, ctx)?;
self.draw_tiles(state, ctx, TileLayer::Background)?; self.draw_tiles(state, ctx, TileLayer::Background)?;
self.draw_npc_layer(state, ctx, NPCLayer::Background)?;
if state.settings.shader_effects if state.settings.shader_effects
&& self.stage.data.background_type != BackgroundType::Black && self.stage.data.background_type != BackgroundType::Black
&& self.stage.data.background_type != BackgroundType::Outside && self.stage.data.background_type != BackgroundType::Outside
@ -1562,23 +1585,7 @@ impl Scene for GameScene {
} }
self.boss.draw(state, ctx, &self.frame)?; self.boss.draw(state, ctx, &self.frame)?;
for npc in self.npc_list.iter_alive() { self.draw_npc_layer(state, ctx, NPCLayer::Middleground)?;
if npc.x < (self.frame.x - 128 * 0x200 - npc.display_bounds.width() as i32 * 0x200)
|| npc.x
> (self.frame.x
+ 128 * 0x200
+ (state.canvas_size.0 as i32 + npc.display_bounds.width() as i32) * 0x200)
&& npc.y < (self.frame.y - 128 * 0x200 - npc.display_bounds.height() as i32 * 0x200)
|| npc.y
> (self.frame.y
+ 128 * 0x200
+ (state.canvas_size.1 as i32 + npc.display_bounds.height() as i32) * 0x200)
{
continue;
}
npc.draw(state, ctx, &self.frame)?;
}
self.draw_bullets(state, ctx)?; self.draw_bullets(state, ctx)?;
self.player2.draw(state, ctx, &self.frame)?; self.player2.draw(state, ctx, &self.frame)?;
self.player1.draw(state, ctx, &self.frame)?; self.player1.draw(state, ctx, &self.frame)?;