check for win

This commit is contained in:
Michael Zhang 2019-08-10 00:56:23 -05:00
parent eb3c12fc58
commit 5d89880e3c
No known key found for this signature in database
GPG key ID: 5BAEFE5D04F0CE6C
4 changed files with 23 additions and 9 deletions

View file

@ -14,7 +14,6 @@
{ {
"movable": true, "movable": true,
"orientation": 1, "orientation": 1,
"position": [1, 3],
"color": [255, 10, 100], "color": [255, 10, 100],
"segments": [ "segments": [
[1, 3, 0, 0], [1, 3, 0, 0],
@ -24,7 +23,6 @@
{ {
"movable": true, "movable": true,
"orientation": 2, "orientation": 2,
"position": [2, 4],
"color": [105, 210, 50], "color": [105, 210, 50],
"segments": [ "segments": [
[2, 4, 2, 0], [2, 4, 2, 0],
@ -34,7 +32,6 @@
{ {
"movable": true, "movable": true,
"orientation": 2, "orientation": 2,
"position": [0, 4],
"color": [35, 150, 100], "color": [35, 150, 100],
"segments": [ "segments": [
[0, 4, 1, 1], [0, 4, 1, 1],
@ -44,7 +41,6 @@
{ {
"movable": true, "movable": true,
"orientation": 1, "orientation": 1,
"position": [0, 3],
"color": [25, 120, 10], "color": [25, 120, 10],
"segments": [ "segments": [
[0, 3, 3, 1], [0, 3, 3, 1],
@ -54,7 +50,6 @@
{ {
"movable": false, "movable": false,
"orientation": 0, "orientation": 0,
"position": [0, 2],
"color": [15, 15, 15], "color": [15, 15, 15],
"segments": [ "segments": [
[0, 2, 0, 0], [0, 2, 0, 0],
@ -63,7 +58,6 @@
{ {
"movable": false, "movable": false,
"orientation": 0, "orientation": 0,
"position": [2, 2],
"color": [15, 15, 15], "color": [15, 15, 15],
"segments": [ "segments": [
[2, 2, 0, 1], [2, 2, 0, 1],

View file

@ -8,7 +8,6 @@ pub struct PlayerData {
pub struct BlockData { pub struct BlockData {
pub movable: bool, pub movable: bool,
pub orientation: u32, pub orientation: u32,
pub position: (i32, i32),
pub color: (u32, u32, u32), pub color: (u32, u32, u32),
pub segments: Vec<[i32; 4]>, pub segments: Vec<[i32; 4]>,
} }
@ -18,7 +17,7 @@ pub struct LevelData {
pub dimensions: [u32; 2], pub dimensions: [u32; 2],
pub player1: PlayerData, pub player1: PlayerData,
pub player2: PlayerData, pub player2: PlayerData,
pub goal1: [u32; 2], pub goal1: (u32, u32),
pub goal2: [u32; 2], pub goal2: (u32, u32),
pub blocks: Vec<BlockData>, pub blocks: Vec<BlockData>,
} }

View file

@ -119,6 +119,7 @@ impl<'a> Game<'a> {
if let Some(change_set) = change_set { if let Some(change_set) = change_set {
let level = self.get_current_level_mut(); let level = self.get_current_level_mut();
level.apply_change_set(change_set.clone()); level.apply_change_set(change_set.clone());
self.check_win_condition();
} }
} }
} else { } else {
@ -134,6 +135,14 @@ impl<'a> Game<'a> {
} }
} }
fn check_win_condition(&mut self) {
let level = self.get_current_level();
if level.check_win_condition() {
// go on to the next level
self.current_level += 1;
}
}
pub fn render(&self, renderer: &mut Renderer) { pub fn render(&self, renderer: &mut Renderer) {
let level = self.get_current_level(); let level = self.get_current_level();
level.render(renderer, &self.animations); level.render(renderer, &self.animations);

View file

@ -20,6 +20,8 @@ pub struct Level {
blocks: Vec<Block>, blocks: Vec<Block>,
player1: Player, player1: Player,
player2: Player, player2: Player,
goal1: (u32, u32),
goal2: (u32, u32),
} }
#[derive(Copy, Clone, Debug, PartialOrd, PartialEq)] #[derive(Copy, Clone, Debug, PartialOrd, PartialEq)]
@ -66,9 +68,19 @@ impl Level {
blocks, blocks,
player1, player1,
player2, player2,
goal1: data.goal1,
goal2: data.goal2,
} }
} }
// check if we won
pub fn check_win_condition(&self) -> bool {
self.player1.position.0 as u32 == self.goal1.0
&& self.player1.position.1 as u32 == self.goal1.1
&& self.player2.position.0 as u32 == self.goal2.0
&& self.player2.position.1 as u32 == self.goal2.1
}
pub fn apply_change_set(&mut self, change_set: ChangeSet) { pub fn apply_change_set(&mut self, change_set: ChangeSet) {
for (entity, direction) in change_set { for (entity, direction) in change_set {
let direction = direction.as_pair(); let direction = direction.as_pair();