Skip to content

Commit

Permalink
begin power bar implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Zooce committed Oct 23, 2022
1 parent a386597 commit a50cadd
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 38 deletions.
26 changes: 13 additions & 13 deletions docs/powerups.excalidraw
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"elements": [
{
"type": "rectangle",
"version": 3295,
"versionNonce": 665439565,
"version": 3296,
"versionNonce": 596558685,
"isDeleted": false,
"id": "0nVOpw9I7pNkX7FBjOvWu",
"fillStyle": "hachure",
Expand All @@ -17,7 +17,7 @@
"angle": 0,
"x": 567,
"y": 342.5,
"strokeColor": "#000000",
"strokeColor": "#5f3dc4",
"backgroundColor": "transparent",
"width": 147,
"height": 59,
Expand Down Expand Up @@ -154,14 +154,14 @@
"type": "arrow"
}
],
"updated": 1660544995229,
"updated": 1666507519676,
"link": null,
"locked": false
},
{
"type": "text",
"version": 3559,
"versionNonce": 1718296709,
"version": 3560,
"versionNonce": 876047603,
"isDeleted": false,
"id": "yO02wGDnltjiLNwxSTcHM",
"fillStyle": "hachure",
Expand All @@ -172,15 +172,15 @@
"angle": 0,
"x": 572,
"y": 348,
"strokeColor": "#000000",
"strokeColor": "#5f3dc4",
"backgroundColor": "transparent",
"width": 137,
"height": 48,
"seed": 445041428,
"groupIds": [],
"strokeSharpness": "sharp",
"boundElements": [],
"updated": 1660462006440,
"updated": 1666507519676,
"link": null,
"locked": false,
"fontSize": 20,
Expand Down Expand Up @@ -4568,8 +4568,8 @@
},
{
"type": "text",
"version": 3966,
"versionNonce": 868719423,
"version": 3982,
"versionNonce": 11206067,
"isDeleted": false,
"id": "pMkIO4hFnZclzgb2skGAb",
"fillStyle": "hachure",
Expand All @@ -4588,17 +4588,17 @@
"groupIds": [],
"strokeSharpness": "sharp",
"boundElements": [],
"updated": 1666502034896,
"updated": 1666505661732,
"link": null,
"locked": false,
"fontSize": 20,
"fontFamily": 1,
"text": "update power\npoints",
"text": "update power\nbars",
"baseline": 41,
"textAlign": "center",
"verticalAlign": "middle",
"containerId": "sGKiwXltVyN5sxIj0LYVY",
"originalText": "update power points"
"originalText": "update power bars"
},
{
"type": "arrow",
Expand Down
2 changes: 1 addition & 1 deletion src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl Moving {
}
}

#[derive(Component, Debug, Eq, PartialEq, Clone, Copy)]
#[derive(Component, Debug, Eq, PartialEq, Hash, Clone, Copy)]
pub enum Player {
Red,
Green,
Expand Down
8 changes: 8 additions & 0 deletions src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@ pub struct HighlightEvent{
pub move_index: Option<usize>,
}

// POWERUP: add power bar event
#[derive(Debug)]
pub enum PowerBarEvent {
Capture{captor: Player, captive: Player},
Deflection{deflector: Player, deflected: Player},
Index(Player, usize),
}

17 changes: 9 additions & 8 deletions src/resources.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

use bevy::prelude::*;
use crate::components::*;

Expand Down Expand Up @@ -101,6 +103,7 @@ pub enum GameButtonAction {
}

// POWERUP: this needs some thought - currently just a placeholder
#[derive(Debug)]
pub enum PowerUp {
RollAgain,
EvadeCapture,
Expand All @@ -110,25 +113,23 @@ pub enum PowerUp {
DoubleDice,
}

#[derive(Debug, Default)]
pub struct PlayerData {
pub player: Player,
pub consecutive_empty_moves: u8,
pub power: f32,
pub power_ups: Vec<PowerUp>,
}

impl PlayerData {
pub fn new(player: Player) -> Self {
Self{
player,
consecutive_empty_moves: 0,
power_ups: vec![],
}
pub fn update_power(&mut self, delta: f32) {
self.power += delta;
println!("power {} ({delta})", self.power);
}
}

/// The data keeping track of the current state of the game.
pub struct GameData {
pub players: [PlayerData; 4],
pub players: HashMap<Player, PlayerData>,
}

#[derive(Debug, Clone, Eq, PartialEq, Hash, Copy)]
Expand Down
24 changes: 24 additions & 0 deletions src/shared_systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,30 @@ pub fn watch_button_state_changes(
}
}

// POWERUP: add update power bar system - recieves power bar events
pub fn update_power_bars(
mut power_bar_events: EventReader<PowerBarEvent>,
mut game_data: ResMut<GameData>,
) {
for event in power_bar_events.iter() {
println!("recieved {:?}", event);
match event {
PowerBarEvent::Capture{ captor, captive } => {
game_data.players.get_mut(captor).unwrap().update_power(3.0);
game_data.players.get_mut(captive).unwrap().update_power(-3.0);
},
PowerBarEvent::Deflection{ deflector, deflected } => {},
PowerBarEvent::Index(player, index) => {
// FIXME: these numbers are completely wacky
game_data.players.get_mut(player).unwrap().update_power(match index {
0..=47 => 10.0/48.0,
_ => 20.0/48.0,
});
}
}
}
}

pub fn generate_power_up(
mut power_up_events: EventReader<GeneratePowerUpEvent>,
mut game_data: ResMut<GameData>,
Expand Down
3 changes: 2 additions & 1 deletion src/system_sets/next_player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub fn choose_next_player(
mut commands: Commands,
mut current_player_data: ResMut<CurrentPlayerData>,
marbles: Query<(Entity, &Player, Option<&CurrentPlayer>), With<Marble>>,
game_data: Res<GameData>,
) {
// move clockwise to the next player
current_player_data.player = match current_player_data.player {
Expand All @@ -26,7 +27,7 @@ pub fn choose_next_player(
}
}

println!("{:?}", current_player_data.player);
println!("{:?} : {:?}", current_player_data.player, game_data.players.get(&current_player_data.player).unwrap());
}

pub fn show_or_hide_buttons(
Expand Down
15 changes: 15 additions & 0 deletions src/system_sets/process_move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ use bevy::prelude::*;
use crate::components::*;
use crate::constants::*;
use crate::events::GeneratePowerUpEvent;
use crate::events::PowerBarEvent;
use crate::resources::*;

pub fn check_for_capture(
mut commands: Commands,
current_player_data: Res<CurrentPlayerData>,
selected_marble: Query<(Entity, &Marble), (With<CurrentPlayer>, With<SelectedMarble>)>,
mut opponent_marbles: Query<(Entity, &mut Marble, &Transform, &Player), Without<CurrentPlayer>>,
mut power_bar_events: EventWriter<PowerBarEvent>,
) {
// TODO: the `e` is only here for logging - remove this later
let (e, cur) = selected_marble.single();
Expand All @@ -31,9 +33,22 @@ pub fn check_for_capture(
);
opponent_marble.index = BOARD.len();
commands.entity(entity).insert(Moving::new(opponent_marble.origin, transform.translation));
power_bar_events.send(PowerBarEvent::Capture{ captor: current_player_data.player, captive: *opponent });
}
}

// POWERUP: add deflection check system (essentially a reverse capture)

// POWERUP: add process index system - generates index power bar event
pub fn process_index(
mut power_bar_events: EventWriter<PowerBarEvent>,
current_player_data: Res<CurrentPlayerData>,
selected_marble: Query<&Marble, (With<CurrentPlayer>, With<SelectedMarble>)>,
) {
let marble = selected_marble.single();
power_bar_events.send(PowerBarEvent::Index(current_player_data.player, marble.index));
}

pub fn check_for_power_up(
current_player_data: Res<CurrentPlayerData>,
selected_marble: Query<&Marble, With<SelectedMarble>>,
Expand Down
7 changes: 1 addition & 6 deletions src/system_sets/turn_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,7 @@ pub fn check_empty_moves(
mut power_up_events: EventWriter<GeneratePowerUpEvent>,
) {
if dice_data.did_use_die() { return; }
let mut player_data = &mut game_data.players[match current_player_data.player {
Player::Red => 0,
Player::Green => 1,
Player::Blue => 2,
Player::Yellow => 3,
}];
let mut player_data = game_data.players.get_mut(&current_player_data.player).unwrap();
player_data.consecutive_empty_moves = if current_player_data.possible_moves.is_empty() {
player_data.consecutive_empty_moves + 1
} else {
Expand Down
23 changes: 14 additions & 9 deletions src/vexation.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

use bevy::prelude::*;
use crate::components::*;
use crate::constants::*;
Expand All @@ -21,6 +23,7 @@ impl Plugin for VexationPlugin {
.add_event::<HighlightEvent>()
.add_event::<MarbleAnimationDoneEvent>()
.add_event::<ActionEvent<GameButtonAction>>()
.add_event::<PowerBarEvent>()

// game play enter
.add_system_set(SystemSet::on_update(GameState::GameStart)
Expand All @@ -42,6 +45,7 @@ impl Plugin for VexationPlugin {
.with_system(animate_tile_highlights)
.with_system(dim_used_die)
.with_system(generate_power_up)
.with_system(update_power_bars)
)

// next player
Expand Down Expand Up @@ -73,9 +77,10 @@ impl Plugin for VexationPlugin {

// process move
.add_system_set(SystemSet::on_update(GameState::ProcessMove)
.with_system(check_for_capture)
.with_system(check_for_power_up.after(check_for_capture))
.with_system(check_for_winner.after(check_for_power_up))
.with_system(check_for_capture.before(check_for_winner))
.with_system(process_index.before(check_for_winner))
.with_system(check_for_power_up.before(check_for_winner))
.with_system(check_for_winner)
)
.add_system_set(SystemSet::on_exit(GameState::ProcessMove)
.with_system(clear_selected_marble)
Expand All @@ -102,12 +107,12 @@ pub fn create_game(
});
commands.insert_resource(RollAnimationTimer(Timer::from_seconds(1.5, false)));
commands.insert_resource(GameData{
players: [
PlayerData::new(Player::Red),
PlayerData::new(Player::Green),
PlayerData::new(Player::Blue),
PlayerData::new(Player::Yellow),
],
players: HashMap::from([
(Player::Red, PlayerData::default()),
(Player::Green, PlayerData::default()),
(Player::Blue, PlayerData::default()),
(Player::Yellow, PlayerData::default()),
]),
});

// pick the first player randomly
Expand Down

0 comments on commit a50cadd

Please sign in to comment.