2020-08-19 13:11:34 +00:00
|
|
|
use log::info;
|
2020-08-18 16:46:07 +00:00
|
|
|
|
|
|
|
use crate::common::Rect;
|
|
|
|
use crate::entity::GameEntity;
|
2020-08-19 13:11:34 +00:00
|
|
|
use crate::frame::Frame;
|
2020-08-19 19:11:32 +00:00
|
|
|
use crate::ggez::{Context, GameResult, timer};
|
|
|
|
use crate::ggez::nalgebra::clamp;
|
2020-08-19 13:11:34 +00:00
|
|
|
use crate::live_debugger::LiveDebugger;
|
|
|
|
use crate::player::Player;
|
2020-08-18 16:46:07 +00:00
|
|
|
use crate::scene::Scene;
|
2020-08-19 13:11:34 +00:00
|
|
|
use crate::SharedGameState;
|
|
|
|
use crate::stage::{BackgroundType, Stage};
|
2020-08-18 16:46:07 +00:00
|
|
|
use crate::str;
|
|
|
|
|
|
|
|
pub struct GameScene {
|
2020-08-19 13:11:34 +00:00
|
|
|
pub tick: usize,
|
|
|
|
pub stage: Stage,
|
|
|
|
pub frame: Frame,
|
|
|
|
pub player: Player,
|
2020-08-18 16:46:07 +00:00
|
|
|
tex_tileset_name: String,
|
|
|
|
tex_background_name: String,
|
|
|
|
tex_hud_name: String,
|
|
|
|
life_bar: usize,
|
|
|
|
life_bar_count: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, EnumIter, PartialEq, Eq, Hash, Copy, Clone)]
|
|
|
|
pub enum TileLayer {
|
|
|
|
All,
|
|
|
|
Background,
|
|
|
|
Foreground,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, EnumIter, PartialEq, Eq, Hash, Copy, Clone)]
|
|
|
|
pub enum Alignment {
|
|
|
|
Left,
|
|
|
|
Right,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GameScene {
|
2020-08-19 13:11:34 +00:00
|
|
|
pub fn new(state: &mut SharedGameState, ctx: &mut Context, id: usize) -> GameResult<Self> {
|
|
|
|
let stage = Stage::load(ctx, &state.base_path, &state.stages[id])?;
|
|
|
|
info!("Loaded stage: {}", stage.data.name);
|
|
|
|
info!("Map size: {}x{}", stage.map.width, stage.map.height);
|
2020-08-18 16:46:07 +00:00
|
|
|
|
2020-08-19 13:11:34 +00:00
|
|
|
let tex_tileset_name = ["Stage/", &stage.data.tileset.filename()].join("");
|
|
|
|
let tex_background_name = stage.data.background.filename();
|
|
|
|
let tex_hud_name = str!("TextBox");
|
2020-08-18 16:46:07 +00:00
|
|
|
|
2020-08-19 00:55:21 +00:00
|
|
|
Ok(Self {
|
2020-08-19 13:11:34 +00:00
|
|
|
tick: 0,
|
|
|
|
stage,
|
|
|
|
player: Player::new(state, ctx)?,
|
|
|
|
frame: Frame {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
wait: 16,
|
|
|
|
},
|
2020-08-18 16:46:07 +00:00
|
|
|
tex_tileset_name,
|
|
|
|
tex_background_name,
|
|
|
|
tex_hud_name,
|
|
|
|
life_bar: 3,
|
|
|
|
life_bar_count: 0,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-19 13:11:34 +00:00
|
|
|
fn draw_number(&self, x: f32, y: f32, val: usize, align: Alignment, state: &mut SharedGameState, ctx: &mut Context) -> GameResult {
|
|
|
|
let batch = state.texture_set.get_or_load_batch(ctx, &state.constants, &self.tex_hud_name)?;
|
2020-08-18 16:46:07 +00:00
|
|
|
let n = val.to_string();
|
|
|
|
let align_offset = if align == Alignment::Right { n.len() as f32 * 8.0 } else { 0.0 };
|
|
|
|
|
|
|
|
for (offset, chr) in n.chars().enumerate() {
|
|
|
|
let idx = chr as usize - '0' as usize;
|
|
|
|
batch.add_rect(x - align_offset + offset as f32 * 8.0, y, &Rect::<usize>::new_size(idx * 8, 56, 8, 8));
|
|
|
|
}
|
|
|
|
|
|
|
|
batch.draw(ctx)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-08-19 13:11:34 +00:00
|
|
|
fn draw_hud(&self, state: &mut SharedGameState, ctx: &mut Context) -> GameResult {
|
|
|
|
let batch = state.texture_set.get_or_load_batch(ctx, &state.constants, &self.tex_hud_name)?;
|
2020-08-18 16:46:07 +00:00
|
|
|
// todo: max ammo display
|
|
|
|
|
|
|
|
// none
|
|
|
|
batch.add_rect(16.0 + 48.0, 16.0,
|
|
|
|
&Rect::<usize>::new_size(80, 48, 16, 8));
|
|
|
|
batch.add_rect(16.0 + 48.0, 24.0,
|
|
|
|
&Rect::<usize>::new_size(80, 48, 16, 8));
|
|
|
|
|
|
|
|
// per
|
|
|
|
batch.add_rect(16.0 + 32.0, 24.0,
|
|
|
|
&Rect::<usize>::new_size(72, 48, 8, 8));
|
|
|
|
// lv
|
|
|
|
batch.add_rect(16.0, 32.0,
|
|
|
|
&Rect::<usize>::new_size(80, 80, 16, 8));
|
|
|
|
// life box
|
|
|
|
batch.add_rect(16.0, 40.0,
|
|
|
|
&Rect::<usize>::new_size(0, 40, 64, 8));
|
|
|
|
// bar
|
|
|
|
batch.add_rect(40.0, 40.0,
|
2020-08-19 13:11:34 +00:00
|
|
|
&Rect::<usize>::new_size(0, 32, ((self.life_bar as usize * 40) / self.player.max_life as usize) - 1, 8));
|
2020-08-18 16:46:07 +00:00
|
|
|
// life
|
|
|
|
batch.add_rect(40.0, 40.0,
|
2020-08-19 13:11:34 +00:00
|
|
|
&Rect::<usize>::new_size(0, 24, ((self.player.life as usize * 40) / self.player.max_life as usize) - 1, 8));
|
2020-08-18 16:46:07 +00:00
|
|
|
|
|
|
|
batch.draw(ctx)?;
|
|
|
|
|
2020-08-19 13:11:34 +00:00
|
|
|
self.draw_number(40.0, 40.0, self.life_bar as usize, Alignment::Right, state, ctx)?;
|
2020-08-18 16:46:07 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-08-19 13:11:34 +00:00
|
|
|
fn draw_background(&self, state: &mut SharedGameState, ctx: &mut Context) -> GameResult {
|
|
|
|
let batch = state.texture_set.get_or_load_batch(ctx, &state.constants, &self.tex_background_name)?;
|
2020-08-18 16:46:07 +00:00
|
|
|
|
2020-08-19 13:11:34 +00:00
|
|
|
match self.stage.data.background_type {
|
2020-08-18 16:46:07 +00:00
|
|
|
BackgroundType::Stationary => {
|
2020-08-19 13:11:34 +00:00
|
|
|
let count_x = state.canvas_size.0 as usize / batch.width() + 1;
|
|
|
|
let count_y = state.canvas_size.1 as usize / batch.height() + 1;
|
2020-08-18 16:46:07 +00:00
|
|
|
|
|
|
|
for y in 0..count_y {
|
|
|
|
for x in 0..count_x {
|
|
|
|
batch.add((x * batch.width()) as f32, (y * batch.height()) as f32);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BackgroundType::MoveDistant => {
|
2020-08-19 13:11:34 +00:00
|
|
|
let off_x = self.frame.x as usize / 2 % (batch.width() * 0x200);
|
|
|
|
let off_y = self.frame.y as usize / 2 % (batch.height() * 0x200);
|
2020-08-18 16:46:07 +00:00
|
|
|
|
2020-08-19 13:11:34 +00:00
|
|
|
let count_x = state.canvas_size.0 as usize / batch.width() + 2;
|
|
|
|
let count_y = state.canvas_size.1 as usize / batch.height() + 2;
|
2020-08-18 16:46:07 +00:00
|
|
|
|
|
|
|
for y in 0..count_y {
|
|
|
|
for x in 0..count_x {
|
|
|
|
batch.add((x * batch.width()) as f32 - (off_x / 0x200) as f32,
|
|
|
|
(y * batch.height()) as f32 - (off_y / 0x200) as f32);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BackgroundType::MoveNear => {}
|
|
|
|
BackgroundType::Water => {}
|
|
|
|
BackgroundType::Black => {}
|
|
|
|
BackgroundType::Autoscroll => {}
|
|
|
|
BackgroundType::OutsideWind | BackgroundType::Outside => {
|
2020-08-19 13:11:34 +00:00
|
|
|
let offset = (self.tick % 640) as isize;
|
2020-08-18 16:46:07 +00:00
|
|
|
|
2020-08-19 13:11:34 +00:00
|
|
|
batch.add_rect(((state.canvas_size.0 - 320.0) / 2.0).floor(), 0.0,
|
2020-08-18 16:46:07 +00:00
|
|
|
&Rect::<usize>::new_size(0, 0, 320, 88));
|
|
|
|
|
2020-08-19 13:11:34 +00:00
|
|
|
for x in ((-offset / 2)..(state.canvas_size.0 as isize)).step_by(320) {
|
2020-08-18 16:46:07 +00:00
|
|
|
batch.add_rect(x as f32, 88.0,
|
|
|
|
&Rect::<usize>::new_size(0, 88, 320, 35));
|
|
|
|
}
|
|
|
|
|
2020-08-19 13:11:34 +00:00
|
|
|
for x in ((-offset % 320)..(state.canvas_size.0 as isize)).step_by(320) {
|
2020-08-18 16:46:07 +00:00
|
|
|
batch.add_rect(x as f32, 123.0,
|
|
|
|
&Rect::<usize>::new_size(0, 123, 320, 23));
|
|
|
|
}
|
|
|
|
|
2020-08-19 13:11:34 +00:00
|
|
|
for x in ((-offset * 2)..(state.canvas_size.0 as isize)).step_by(320) {
|
2020-08-18 16:46:07 +00:00
|
|
|
batch.add_rect(x as f32, 146.0,
|
|
|
|
&Rect::<usize>::new_size(0, 146, 320, 30));
|
|
|
|
}
|
|
|
|
|
2020-08-19 13:11:34 +00:00
|
|
|
for x in ((-offset * 4)..(state.canvas_size.0 as isize)).step_by(320) {
|
2020-08-18 16:46:07 +00:00
|
|
|
batch.add_rect(x as f32, 176.0,
|
|
|
|
&Rect::<usize>::new_size(0, 176, 320, 64));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
batch.draw(ctx)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-08-19 13:11:34 +00:00
|
|
|
fn draw_tiles(&self, state: &mut SharedGameState, ctx: &mut Context, layer: TileLayer) -> GameResult {
|
|
|
|
let batch = state.texture_set.get_or_load_batch(ctx, &state.constants, &self.tex_tileset_name)?;
|
|
|
|
let mut rect = Rect::<usize>::new(0, 0, 16, 16);
|
|
|
|
|
|
|
|
let tile_start_x = clamp(self.frame.x / 0x200 / 16, 0, self.stage.map.width as isize) as usize;
|
|
|
|
let tile_start_y = clamp(self.frame.y / 0x200 / 16, 0, self.stage.map.height as isize) as usize;
|
|
|
|
let tile_end_x = clamp((self.frame.x / 0x200 + 8 + state.canvas_size.0 as isize) / 16 + 1, 0, self.stage.map.width as isize) as usize;
|
|
|
|
let tile_end_y = clamp((self.frame.y / 0x200 + 8 + state.canvas_size.1 as isize) / 16 + 1, 0, self.stage.map.height as isize) as usize;
|
|
|
|
|
|
|
|
for y in tile_start_y..tile_end_y {
|
|
|
|
for x in tile_start_x..tile_end_x {
|
|
|
|
let tile = *self.stage.map.tiles
|
|
|
|
.get((y * self.stage.map.width) + x)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
match layer {
|
|
|
|
TileLayer::Background => {
|
|
|
|
if self.stage.map.attrib[tile as usize] >= 0x20 {
|
|
|
|
continue;
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|
2020-08-19 13:11:34 +00:00
|
|
|
}
|
|
|
|
TileLayer::Foreground => {
|
|
|
|
let attr = self.stage.map.attrib[tile as usize];
|
|
|
|
if attr < 0x40 || attr >= 0x80 {
|
|
|
|
continue;
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-19 13:11:34 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
2020-08-18 16:46:07 +00:00
|
|
|
|
2020-08-19 13:11:34 +00:00
|
|
|
rect.left = (tile as usize % 16) * 16;
|
|
|
|
rect.top = (tile as usize / 16) * 16;
|
|
|
|
rect.right = rect.left + 16;
|
|
|
|
rect.bottom = rect.top + 16;
|
2020-08-18 16:46:07 +00:00
|
|
|
|
2020-08-19 13:11:34 +00:00
|
|
|
batch.add_rect((x as f32 * 16.0 - 8.0) - (self.frame.x / 0x200) as f32, (y as f32 * 16.0 - 8.0) - (self.frame.y / 0x200) as f32, &rect);
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|
2020-08-19 00:55:21 +00:00
|
|
|
}
|
2020-08-19 13:11:34 +00:00
|
|
|
|
|
|
|
batch.draw(ctx)?;
|
|
|
|
|
2020-08-18 16:46:07 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Scene for GameScene {
|
2020-08-19 13:11:34 +00:00
|
|
|
fn init(&mut self, state: &mut SharedGameState, ctx: &mut Context) -> GameResult {
|
|
|
|
state.sound_manager.play_song(ctx)?;
|
|
|
|
//self.player.x = 700 * 0x200;
|
|
|
|
//self.player.y = 1000 * 0x200;
|
|
|
|
//self.player.equip.set_booster_2_0(true);
|
|
|
|
state.flags.set_flag_x01(true);
|
|
|
|
state.flags.set_control_enabled(true);
|
2020-08-18 16:46:07 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-08-19 13:11:34 +00:00
|
|
|
fn tick(&mut self, state: &mut SharedGameState, ctx: &mut Context) -> GameResult {
|
2020-08-18 16:46:07 +00:00
|
|
|
state.update_key_trigger();
|
|
|
|
|
2020-08-19 13:11:34 +00:00
|
|
|
if state.flags.flag_x01() {
|
|
|
|
self.player.tick(state, ctx)?;
|
2020-08-18 16:46:07 +00:00
|
|
|
|
2020-08-19 13:11:34 +00:00
|
|
|
self.player.flags.0 = 0;
|
|
|
|
self.player.tick_map_collisions(state, &self.stage);
|
|
|
|
|
|
|
|
self.frame.update(state, &self.player, &self.stage);
|
|
|
|
}
|
2020-08-18 16:46:07 +00:00
|
|
|
|
|
|
|
if state.flags.control_enabled() {
|
|
|
|
// update health bar
|
2020-08-19 13:11:34 +00:00
|
|
|
if self.life_bar < self.player.life as usize {
|
|
|
|
self.life_bar = self.player.life as usize;
|
2020-08-18 16:46:07 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 13:11:34 +00:00
|
|
|
if self.life_bar > self.player.life as usize {
|
2020-08-18 16:46:07 +00:00
|
|
|
self.life_bar_count += 1;
|
|
|
|
if self.life_bar_count > 30 {
|
|
|
|
self.life_bar -= 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.life_bar_count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-19 13:11:34 +00:00
|
|
|
self.tick = self.tick.wrapping_add(1);
|
2020-08-18 16:46:07 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-08-19 13:11:34 +00:00
|
|
|
fn draw(&self, state: &mut SharedGameState, ctx: &mut Context) -> GameResult {
|
|
|
|
self.draw_background(state, ctx)?;
|
|
|
|
self.draw_tiles(state, ctx, TileLayer::Background)?;
|
|
|
|
self.player.draw(state, ctx, &self.frame)?;
|
|
|
|
self.draw_tiles(state, ctx, TileLayer::Foreground)?;
|
|
|
|
|
|
|
|
self.draw_hud(state, ctx)?;
|
2020-08-19 19:11:32 +00:00
|
|
|
self.draw_number(state.canvas_size.0 - 8.0, 8.0, timer::fps(ctx) as usize, Alignment::Right, state, ctx)?;
|
2020-08-18 16:46:07 +00:00
|
|
|
|
2020-08-19 11:21:40 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-08-18 16:46:07 +00:00
|
|
|
|
2020-08-19 13:11:34 +00:00
|
|
|
fn debug_overlay_draw(&mut self, dbg: &mut LiveDebugger, state: &mut SharedGameState, ctx: &mut Context, ui: &mut imgui::Ui) -> GameResult {
|
|
|
|
dbg.run_ingame(self, state, ctx, ui)?;
|
2020-08-18 16:46:07 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|