Skip to content

Commit

Permalink
Merge pull request #26 from hyoi/sprite_animation
Browse files Browse the repository at this point in the history
v0.10.2のリリース
  • Loading branch information
hyoi authored Dec 9, 2023
2 parents 10ed9d4 + 3d0c9f7 commit 9e0cd1c
Show file tree
Hide file tree
Showing 31 changed files with 894 additions and 597 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
[package]
name = "tigtag"
version = "0.10.1"
version = "0.10.2"
edition = "2021"

[dependencies]
#bevy = { git = "https://github.com/bevyengine/bevy" } #Master branch
bevy = "0.12"
counted-array = "0.1"
once_cell = "1"
rand = "0.8"
regex = "1"
Expand Down
Binary file added assets/sprites/chasers/sheet_chaser_blue_east.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sprites/chasers/sheet_chaser_blue_north.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sprites/chasers/sheet_chaser_blue_south.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sprites/chasers/sheet_chaser_blue_west.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sprites/chasers/sheet_chaser_green_east.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sprites/chasers/sheet_chaser_green_north.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sprites/chasers/sheet_chaser_green_south.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sprites/chasers/sheet_chaser_green_west.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sprites/chasers/sheet_chaser_pink_east.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sprites/chasers/sheet_chaser_pink_north.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sprites/chasers/sheet_chaser_pink_south.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sprites/chasers/sheet_chaser_pink_west.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sprites/chasers/sheet_chaser_red_east.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sprites/chasers/sheet_chaser_red_north.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sprites/chasers/sheet_chaser_red_south.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sprites/chasers/sheet_chaser_red_west.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sprites/player/player.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sprites/player/sheet_player_east.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sprites/player/sheet_player_north.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sprites/player/sheet_player_south.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sprites/player/sheet_player_west.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 2 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@ use bevy::
asset::{ LoadState, LoadedUntypedAsset },
diagnostic::{ DiagnosticsStore, FrameTimeDiagnosticsPlugin },
sprite::{ Anchor, MaterialMesh2dBundle },
utils::HashSet,
utils::{ HashSet, HashMap },
audio::{ Volume, VolumeLevel },
};
use once_cell::sync::Lazy;
use counted_array::counted_array;
use rand::prelude::*;
use regex::Regex;

//standard library
use std::ops::{ Range, Add, AddAssign };
use std::cmp::Ordering;
use std::collections::VecDeque;
use std::f32::consts::TAU;
use std::f32::consts::{ PI, TAU };

//internal submodules
mod public;
Expand Down
126 changes: 102 additions & 24 deletions src/play_game/chasers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ use super::*;
////////////////////////////////////////////////////////////////////////////////

//チェイサーの色と移動方向の決定関数
const COLOR_SPRITE_CHASERS: [ ( Color, Option<FnChasing> ); 4 ] =
[ ( Color::RED, Some ( choice_way_red ) ),
( Color::GREEN, Some ( choice_way_green ) ),
( Color::PINK, Some ( choice_way_pink ) ),
( Color::BLUE, Some ( choice_way_blue ) ),
const COLOR_SPRITE_CHASERS: &[ ( Color, Option<FnChasing>, AnimeSpriteColorTag ) ] =
&[ ( Color::RED, Some ( choice_way_red ), AnimeSpriteColorTag::Red ),
( Color::GREEN, Some ( choice_way_green ), AnimeSpriteColorTag::Green ),
( Color::PINK, Some ( choice_way_pink ), AnimeSpriteColorTag::Pink ),
( Color::BLUE, Some ( choice_way_blue ), AnimeSpriteColorTag::Blue ),
];

#[derive( Component, Clone, Copy, PartialEq )]
pub enum AnimeSpriteColorTag { Red, Blue, Green, Pink }

//進む方向を決める(赤)
fn choice_way_red( chaser: &mut Chaser, player: &Player, sides: &[ News ] ) -> News
{ if sides.contains( &News::West ) && player.next_grid.x < chaser.grid.x { return News::West }
Expand Down Expand Up @@ -52,47 +55,107 @@ fn choice_way_pink( chaser: &mut Chaser, player: &Player, sides: &[ News ] ) ->

////////////////////////////////////////////////////////////////////////////////

//スプライトシートを読み込んでResourceに登録する
pub fn load_sprite_sheet
( mut cmds: Commands,
asset_svr: Res<AssetServer>,
mut texture_atlases: ResMut<Assets<TextureAtlas>>,
)
{ //登録するResourceの準備
let mut animation_sprites = AnimationSpriteChasers
{ cols: ANIME_CHASER_COLS,
wait: ANIME_CHASER_TIMER,
..default()
};

//スプライトアニメーション用データを作成
for chaser_assets in ANIME_CHASERS_ASSETS
{ let mut hash_hdls = HashMap::with_capacity( 4 ); //四方

//四方のアニメassetからテクスチャアトラスのハンドルを作ってハッシュに登録する
for ( news, asset ) in chaser_assets.iter()
{ let texture_atlas = asset_svr.gen_player_texture_atlas( asset );
let texture_atlas_hdl = texture_atlases.add( texture_atlas );

hash_hdls.insert( *news, texture_atlas_hdl );
}
animation_sprites.hdls.push( hash_hdls );
}

//Resourceに登録する
cmds.insert_resource( animation_sprites );
}

////////////////////////////////////////////////////////////////////////////////

//チェイサーをspawnする
pub fn spawn_sprite
( qry_player: Query<Entity, With<Chaser>>,
( qry_chaser: Query<Entity, With<Chaser>>,
opt_anime_sprite_chasers: Option<Res<AnimationSpriteChasers>>,
opt_record: Option<Res<Record>>,
mut cmds: Commands,
)
{ let Some ( record ) = opt_record else { return };

//スプライトがあれば削除する
qry_player.for_each( | id | cmds.entity( id ).despawn_recursive() );
qry_chaser.for_each( | id | cmds.entity( id ).despawn_recursive() );

//チェイサーのスプライトを配置する
let custom_size = Some ( SIZE_GRID * MAGNIFY_SPRITE_CHASER );

( 0.. ).zip( CHASER_START_POSITION ).for_each
( | ( i, chaser_grid ) |
{ let chaser_vec2 = chaser_grid.to_vec2_on_map();
let index = ( ( i + record.stage() - 1 ) % 4 ) as usize;
let ( color, opt_fn_chasing ) = COLOR_SPRITE_CHASERS[ index ];
let chaser = Chaser
{ grid : chaser_grid,
next_grid: chaser_grid,
let ( color, opt_fn_chasing, color_tag ) = COLOR_SPRITE_CHASERS[ index ];
let mut chaser = Chaser
{ grid : *chaser_grid,
next_grid: *chaser_grid,
dx_start : chaser_vec2,
dx_end : chaser_vec2,
color,
opt_fn_chasing,
..default()
};
let translation = chaser_vec2.extend( DEPTH_SPRITE_CHASER );

//アニメーションするスプライトをspawnする
if let Some ( ref anime_sprites ) = opt_anime_sprite_chasers
{ let hdls = &anime_sprites.hdls;
chaser.hdls = hdls[ index ].clone();

let texture_atlas_hdl = hdls[ index ].get( &chaser.direction ).unwrap();
let cols = anime_sprites.cols;
let wait = anime_sprites.wait;

cmds
.spawn( ( SpriteBundle::default(), chaser ) )
.insert( Sprite { color, custom_size, ..default() } )
.insert( Transform::from_translation( chaser_vec2.extend( DEPTH_SPRITE_CHASER ) ) )
;
let custom_size = Some( SIZE_GRID );
let texture_atlas_sprite = TextureAtlasSprite { custom_size, ..default() };
let anime_params = AnimationParams
{ timer: Timer::from_seconds( wait, TimerMode::Repeating ),
frame_count: cols,
};

cmds.spawn( ( SpriteSheetBundle::default(), chaser, anime_params ) )
.insert( texture_atlas_hdl.clone() )
.insert( color_tag )
.insert( texture_atlas_sprite )
.insert( Transform::from_translation( translation ) )
;
}
else
{ //正方形のメッシュ
let custom_size = Some ( SIZE_GRID * MAGNIFY_SPRITE_CHASER );
cmds
.spawn( ( SpriteBundle::default(), chaser ) )
.insert( Sprite { color, custom_size, ..default() } )
.insert( Transform::from_translation( translation ) )
;
}
}
);
}

////////////////////////////////////////////////////////////////////////////////

//チェイサーのスプライトを回転させる
//チェイサー(正方形のメッシュの場合)のスプライトを回転させる
pub fn rotate
( mut qry_chaser: Query<&mut Transform, With<Chaser>>,
time: Res<Time>,
Expand All @@ -110,6 +173,7 @@ pub fn rotate
//チェイサーを移動させる
pub fn move_sprite
( mut qry_chaser: Query<(&mut Chaser, &mut Transform)>,
mut qry_texture_atlas_hdl: Query<( &mut Handle<TextureAtlas>, &AnimeSpriteColorTag )>,
opt_map: Option<Res<Map>>,
qry_player: Query<&Player>,
mut evt_clear : EventReader<EventClear>,
Expand All @@ -118,7 +182,7 @@ pub fn move_sprite
)
{ let Ok ( player ) = qry_player.get_single() else { return };
let Some ( map ) = opt_map else { return };

//直前の判定でクリア/オーバーしていたらスプライトを移動させない
if evt_clear.read().next().is_some() { return }
if evt_over .read().next().is_some() { return }
Expand All @@ -139,15 +203,14 @@ pub fn move_sprite
chaser.dx_end = chaser.next_grid.to_vec2_on_map();
transform.translation = chaser.dx_end.extend( DEPTH_SPRITE_CHASER );
}

//四方の脇道を取得する
let mut sides = map.get_side_spaces_list( chaser.next_grid ); //脇道のリスト
sides.retain( | side | chaser.next_grid + side != chaser.grid ); //戻り路を取り除く

//チェイサーが次に進む方向を決める(プレーヤーのキー入力に相当)
chaser.is_stop = false;
chaser.direction =
match sides.len().cmp( &1 ) //sides要素数は1以上(このゲームのマップに行き止まりが無いので)
let new_side = match sides.len().cmp( &1 ) //sides要素数は1以上(マップに行き止まりが無いので)
{ Ordering::Equal => //一本道 ⇒ 道なりに進む
sides[ 0 ],
Ordering::Greater => //三叉路または十字路
Expand All @@ -169,11 +232,26 @@ pub fn move_sprite
},
};

//チェイサーの向きが変わったらスプライトアニメのテクスチャハンドルを差し替える
if chaser.direction != new_side && ! chaser.hdls.is_empty()
{ let new_hdl = chaser.hdls.get( &new_side ).unwrap().clone();
for ( mut hdl, tag ) in qry_texture_atlas_hdl.iter_mut()
{ match tag
{ AnimeSpriteColorTag::Red if chaser.color == Color::RED => { *hdl = new_hdl; break },
AnimeSpriteColorTag::Blue if chaser.color == Color::BLUE => { *hdl = new_hdl; break },
AnimeSpriteColorTag::Green if chaser.color == Color::GREEN => { *hdl = new_hdl; break },
AnimeSpriteColorTag::Pink if chaser.color == Color::PINK => { *hdl = new_hdl; break },
_ => (),
}
}
}
chaser.direction = new_side;

//現在の位置と次の位置を更新する
chaser.grid = chaser.next_grid;
if ! chaser.is_stop
{ let side = chaser.direction; //chaser.direction += chaser.next_grid すると、
chaser.next_grid += side; //error[E0502]: cannot borrow `chaser` as
chaser.next_grid += side; //error[E0502]: cannot borrow `chaser` as
} //immutable because it is also borrowed as mutable

//タイマーをリセットする
Expand Down
Loading

0 comments on commit 9e0cd1c

Please sign in to comment.