Skip to content

Commit

Permalink
Partial adjustment
Browse files Browse the repository at this point in the history
  • Loading branch information
Cupnfish committed Jan 28, 2021
1 parent ebf37cf commit 7062b1d
Show file tree
Hide file tree
Showing 11 changed files with 448 additions and 249 deletions.
62 changes: 51 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 @@ -25,7 +25,7 @@ web = [
]

[dependencies]
rand = "*"
rand = "0.8"
anyhow = "1"
bevy = {version="0.4", default-features=false}
bevy_webgl2 = {version="0.4", optional=true}
Expand Down
34 changes: 3 additions & 31 deletions src/bomb.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use bevy::prelude::*;
use bevy_rapier2d::{
physics::{ColliderHandleComponent, RigidBodyHandleComponent},
rapier::{dynamics::RigidBodyBuilder, geometry::ColliderBuilder},
};

use crate::{
assets::{
Expand All @@ -14,8 +10,8 @@ use crate::{
Wall, FIRE_ANIMATE_TIME,
},
entitys::{
create_bomb, create_bomb_number_buff, create_center_fire, create_collider, create_ember,
create_portal, create_power_buff, create_speed_buff, create_static_rigid_body,
create_bomb, create_bomb_number_buff, create_center_fire, create_ember, create_portal,
create_power_buff, create_speed_buff,
},
events::GameEvents,
resources::Map,
Expand All @@ -36,7 +32,6 @@ impl BombSystems for SystemStage {
.add_system(animate_bomb.system())
.add_system(animate_fire.system())
.add_system(ember_trigger.system())
.add_system(for_wall_add_collision_detection.system())
}
}

Expand All @@ -58,30 +53,7 @@ impl BombBunble {
}
}
}
fn for_wall_add_collision_detection(
commands: &mut Commands,
query: Query<
(Entity, &Transform),
(
With<Wall>,
Without<RigidBodyBuilder>,
Without<ColliderBuilder>,
Without<RigidBodyHandleComponent>,
Without<ColliderHandleComponent>,
),
>,
) {
for (entity, transform) in query.iter() {
let translation = transform.translation;
commands.insert(
entity,
(
create_static_rigid_body(translation.x, translation.y),
create_collider(entity),
),
);
}
}

fn space_to_set_bomb(
commands: &mut Commands,
bomb_texture_atlas: Res<BombTextureAtlas>,
Expand Down
28 changes: 27 additions & 1 deletion src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,40 @@ impl Ember {
Ember(Timer::from_seconds(EMBER_START_TIME, false), power)
}
}

use bevy_rapier2d::na::Vector2;
use rand::{
distributions::{Distribution, Standard},
Rng,
};
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum Direction {
Left = 0,
Up = 1,
Right = 2,
Down = 3,
}
impl Distribution<Direction> for Standard {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Direction {
match rng.gen_range(0..=3) {
// rand 0.8
0 => Direction::Left,
1 => Direction::Up,
2 => Direction::Right,
_ => Direction::Down,
}
}
}
impl Direction {
pub fn into_dir(&self) -> Vector2<f32> {
match self {
Direction::Up => Vector2::new(-1.0, 0.0),
Direction::Left => Vector2::new(0.0, 1.0),
Direction::Down => Vector2::new(1.0, 0.0),
Direction::Right => Vector2::new(0.0, -1.0),
}
}
}

pub const NEXT_PLAYER_SHEET: u32 = 14;

pub struct AnimateIndexs<T> {
Expand Down
112 changes: 25 additions & 87 deletions src/creatures.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
use crate::{
components::{AnimateIndexs, Animation, Destructible, Direction, Player, Stop, Velocity},
entitys::{create_creature_collider, create_dyn_rigid_body},
components::{AnimateIndexs, Animation, Destructible, Direction, Stop, Velocity},
errors::querr_error_handler,
events::*,
ui::DrawBlinkTimer,
utils::vecs_xy_intersect,
};
use bevy::ecs::{Query, ResMut, SystemStage, With};
use bevy::{ecs::QueryError, prelude::*};
use bevy::{
ecs::{Query, ResMut, SystemStage, With},
sprite::ColorMaterial,
};
use bevy_rapier2d::{
na::Vector2,
physics::{ColliderHandleComponent, RigidBodyHandleComponent},
rapier::{
dynamics::{RigidBodyBuilder, RigidBodySet},
geometry::ColliderBuilder,
},
na::Vector2, physics::RigidBodyHandleComponent, rapier::dynamics::RigidBodySet,
};
use rand::{seq::SliceRandom, thread_rng};
use rand::{thread_rng, Rng};

#[derive(Bundle)]
pub struct CreatureBundle {
Expand All @@ -42,30 +31,37 @@ impl Default for CreatureBundle {
}
}

pub struct CreatureMaterial(pub Handle<ColorMaterial>);

pub struct Creature;

// could be done with a crate
const DIRECTIONS: [Direction; 4] = [
Direction::Up,
Direction::Down,
Direction::Left,
Direction::Right,
];
const TURN_PROBABILITY: f32 = 0.02;
const TURN_PROBABILITY: i32 = 4;
pub trait CreatureSystems {
fn creature_systems(&mut self) -> &mut Self;
}
impl CreatureSystems for SystemStage {
fn creature_systems(&mut self) -> &mut Self {
self.add_system(
creature_movement
.system()
.chain(querr_error_handler.system()),
)
.add_system(despawn_player.system())
.add_system(animate_creature.system())
}
}

fn creature_movement(
mut query: Query<(Entity, &Velocity, &mut Direction), (With<Creature>, Without<Stop>)>,
mut rigid_body_handle_query: Query<&mut RigidBodyHandleComponent>,
mut rigid_body_set: ResMut<RigidBodySet>,
) -> Result<(), QueryError> {
for (entity, velocity, mut direction) in query.iter_mut() {
let mut rng = thread_rng();
let rigid_body_handle =
rigid_body_handle_query.get_component_mut::<RigidBodyHandleComponent>(entity)?;
if rand::random::<f32>() < TURN_PROBABILITY {
let mut rng = thread_rng();
let n = rng.gen_range(0..=100);
if n < TURN_PROBABILITY {
// only change ocassionally
*direction = *DIRECTIONS.choose(&mut rng).unwrap()
*direction = rand::random();
}
let linvel = match *direction {
Direction::Left => Vector2::new(-velocity.0, 0.0),
Expand All @@ -82,67 +78,9 @@ fn creature_movement(
}
Ok(())
}
fn for_creature_add_collision_detection(
commands: &mut Commands,
query: Query<
(Entity, &Transform),
(
With<Creature>,
Without<RigidBodyBuilder>,
Without<ColliderBuilder>,
Without<RigidBodyHandleComponent>,
Without<ColliderHandleComponent>,
),
>,
) {
for (entity, transform) in query.iter() {
let translation = transform.translation;
commands.insert(
entity,
(
create_dyn_rigid_body(translation.x, translation.y),
create_creature_collider(entity),
),
);
}
}

pub trait CreatureSystems {
fn creature_systems(&mut self) -> &mut Self;
}
impl CreatureSystems for SystemStage {
fn creature_systems(&mut self) -> &mut Self {
self.add_system(creature_player_collision.system())
.add_system(for_creature_add_collision_detection.system())
.add_system(
creature_movement
.system()
.chain(querr_error_handler.system()),
)
.add_system(despawn_player.system())
.add_system(animate_creature.system())
}
}

fn creature_player_collision(
commands: &mut Commands,
mut player_query: Query<(Entity, &mut Transform), (With<Player>, Without<Stop>)>,
mut creature_query: Query<&mut Transform, With<Creature>>,
mut game_over_events: ResMut<Events<GameEvents>>,
) {
for (entity, player_transform) in player_query.iter_mut() {
let player_pos = &player_transform.translation.truncate();
for creature_transform in creature_query.iter_mut() {
if vecs_xy_intersect(&creature_transform.translation.truncate(), player_pos) {
commands.insert(entity, StopAndFlashing::default());
game_over_events.send(GameEvents::GameOver);
}
}
}
}

#[derive(Bundle)]
struct StopAndFlashing(Stop, DrawBlinkTimer, Timer);
pub struct StopAndFlashing(Stop, DrawBlinkTimer, Timer);
impl Default for StopAndFlashing {
fn default() -> Self {
Self(
Expand Down
Loading

0 comments on commit 7062b1d

Please sign in to comment.