Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

Commit

Permalink
Adding white cat with a cart to the mall scene (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
porkbrain authored May 23, 2024
1 parent 03ace25 commit 670b599
Show file tree
Hide file tree
Showing 19 changed files with 3,323 additions and 97 deletions.
32 changes: 21 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ common_visuals = { path = "common/visuals" }
main_game_lib = { path = "main_game_lib" }

graphviz-rust = "0.9"
itertools = "0.12"
itertools = "0.13"
lazy_static = "1.4"
logos = "0.14"
pathfinding = "4.8"
Expand Down
12 changes: 12 additions & 0 deletions bevy_grid_squared/src/direction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ impl Square {
.map(move |direction| self.neighbor(direction))
}

/// These are the only 4 diagonal neighbors of a square with manhattan
/// distance 2: ↖, ↗, ↙, ↘
#[inline]
pub fn neighbors_only_diagonal(self) -> impl Iterator<Item = Self> {
use GridDirection::*;

[TopLeft, TopRight, BottomLeft, BottomRight]
.iter()
.copied()
.map(move |direction| self.neighbor(direction))
}

/// Given a square, returns the direction to the other square.
/// They don't have to be neighbors, works at arbitrary distance.
/// If they are the same square then returns `None`.
Expand Down
2 changes: 2 additions & 0 deletions common/assets/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ pub mod portraits {
pub const CAPY: &str = "characters/portraits/capy1.png";
pub const CAT: &str = "characters/portraits/cat1.png";
pub const GINGER_CAT: &str = "characters/portraits/gingercat1.png";
pub const WHITE_CAT: &str = "characters/portraits/whitecat1.png";
pub const EMIL: &str = "characters/portraits/emil1.png";
pub const MASTER: &str = "characters/portraits/master1.png";
pub const POOPER: &str = "characters/portraits/pooper1.png";
Expand All @@ -98,6 +99,7 @@ pub mod character_atlases {
pub const MARIE: &str = "characters/atlases/marie1.png";
pub const SAMIZDAT: &str = "characters/atlases/samizdat1.png";
pub const BOLT: &str = "characters/atlases/bolt1.png";
pub const WHITE_CAT: &str = "characters/atlases/whitecat1.png";
}

pub mod misc {
Expand Down
1 change: 1 addition & 0 deletions common/story/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ devtools = ["bevy-inspector-egui"]


[dependencies]
bevy_grid_squared.workspace = true
bevy-inspector-egui = { workspace = true, optional = true }
bevy.workspace = true
common_action.workspace = true
Expand Down
110 changes: 91 additions & 19 deletions common/story/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod emoji;
use std::time::Duration;

use bevy::prelude::*;
use bevy_grid_squared::GridDirection;
use common_assets::store::AssetList;
use serde::{Deserialize, Serialize};
use strum::{
Expand Down Expand Up @@ -60,8 +61,10 @@ pub enum Character {
Capy,
/// A character.
Cat,
/// A character.
/// Sits in the mall at the Good Water stand.
GingerCat,
/// Walks around the mall with a pushcart.
WhiteCat,
/// A character.
Emil,
/// A character.
Expand Down Expand Up @@ -111,22 +114,6 @@ impl AssetList for StoryAssets {
}

impl Character {
/// How long does it take to move one square.
pub fn default_step_time(self) -> Duration {
match self {
Character::Winnie => Duration::from_millis(35),
_ => Duration::from_millis(50),
}
}

/// How long does it take to move one square if walking slowly.
pub fn slow_step_time(self) -> Duration {
match self {
Character::Winnie => Duration::from_millis(100),
_ => Duration::from_millis(120),
}
}

/// Static str name of the character.
pub fn name(self) -> &'static str {
match self {
Expand All @@ -139,6 +126,7 @@ impl Character {
Character::Capy => "Capy",
Character::Cat => "Cat",
Character::GingerCat => "Rolo",
Character::WhiteCat => "Fluffy",
Character::Emil => "Emil",
Character::Pooper => "Pooper",
Character::Samizdat => "Samizdat",
Expand All @@ -162,6 +150,7 @@ impl Character {
Character::Winnie => WINNIE,
Character::Marie => MARIE,
Character::Samizdat => SAMIZDAT,
Character::WhiteCat => WHITE_CAT,
Character::Bolt => BOLT,
_ => unimplemented!(),
}
Expand Down Expand Up @@ -189,6 +178,7 @@ impl Character {
Character::Capy => CAPY,
Character::Cat => CAT,
Character::GingerCat => GINGER_CAT,
Character::WhiteCat => WHITE_CAT,
Character::Emil => EMIL,
Character::Master => MASTER,
Character::Pooper => POOPER,
Expand All @@ -198,16 +188,24 @@ impl Character {
}
}

/// Returns arguments to [`TextureAtlasLayout::from_grid`].
/// Returns arguments to [`TextureAtlasLayout::from_grid`]:
///
/// * `tile_size` - Each layout grid cell size
/// * `columns` - Grid column count
/// * `rows` - Grid row count
/// * `padding` - Optional padding between cells
#[inline]
fn sprite_atlas(self) -> Option<(Vec2, usize, usize, Vec2)> {
const STANDARD_SIZE: Vec2 = Vec2::new(25.0, 46.0);

match self {
Character::Winnie => Some((STANDARD_SIZE, 12, 1, default())),
Character::Bolt => Some((STANDARD_SIZE, 12, 1, default())),
Character::Marie => Some((STANDARD_SIZE, 15, 1, default())),
Character::Samizdat => Some((STANDARD_SIZE, 15, 1, default())),
Character::Bolt => Some((STANDARD_SIZE, 12, 1, default())),
Character::WhiteCat => {
Some((Vec2::new(48.0, 46.0), 6, 1, default()))
}
_ => None,
}
}
Expand All @@ -231,4 +229,78 @@ impl Character {

texture_atlases.insert(self.sprite_atlas_layout_handle(), atlas);
}

/// How long does it take to move one square.
pub fn default_step_time(self) -> Duration {
match self {
Character::Winnie => Duration::from_millis(35),
Character::WhiteCat => Duration::from_millis(120),
_ => Duration::from_millis(50),
}
}

/// How long does it take to move one square if walking slowly.
pub fn slow_step_time(self) -> Duration {
match self {
Character::Winnie => Duration::from_millis(100),
_ => Duration::from_millis(120),
}
}

/// Based on the current character and direction they are facing, what's the
/// current sprite index in the atlas if they are standing and not moving.
pub fn standing_sprite_atlas_index(
self,
direction: GridDirection,
) -> usize {
use GridDirection::*;

match (self, direction) {
(Self::WhiteCat, Bottom | Left | TopLeft | BottomLeft) => 0,
(Self::WhiteCat, Top | Right | TopRight | BottomRight) => 3,

(_, Bottom) => 0,
(_, Top) => 1,
(_, Right | TopRight | BottomRight) => 6,
(_, Left | TopLeft | BottomLeft) => 9,
}
}

/// Based on the current character, direction they are walking,
/// how much time has elapsed since the beginning and how fast they
/// walk, returns the index of the sprite in the atlas.
#[inline]
pub fn walking_sprite_atlas_index(
self,
direction: GridDirection,
time: &Time,
step_time: Duration,
) -> usize {
use GridDirection::*;

// How often we change walking frame based on how fast we're walking
// from square to square.
let step_secs = step_time.as_secs_f32();
let animation_step_time = match direction {
Top | Bottom => step_secs * 5.0,
_ => step_secs * 3.5,
}
.clamp(0.2, 0.5);

// right now, each sprite has 3 walking frames
let extra = (time.elapsed_seconds_wrapped() / animation_step_time)
.floor() as usize
% 2;

match (self, direction) {
(Self::WhiteCat, Bottom | Left | TopLeft | BottomLeft) => 1 + extra,
(Self::WhiteCat, Top | Right | TopRight | BottomRight) => 4 + extra,

// defaults
(_, Top) => 2 + extra,
(_, Bottom) => 4 + extra,
(_, Right | TopRight | BottomRight) => 7 + extra,
(_, Left | TopLeft | BottomLeft) => 10 + extra,
}
}
}
Binary file removed main_game/assets/characters/atlases/white_cat1.png
Binary file not shown.
Binary file added main_game/assets/characters/atlases/whitecat1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 670b599

Please sign in to comment.