From 5f8291042a46c76b69398df752c06b0a51e2fcd1 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Sat, 10 Aug 2019 01:11:07 -0500 Subject: [PATCH] add second level --- levels/tutorial2.json | 54 +++++++++++++++++++++++++++++++++++++++++++ src/data.rs | 4 ++-- src/game.rs | 6 ++++- src/level/mod.rs | 34 ++++++++++++++++++++++----- 4 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 levels/tutorial2.json diff --git a/levels/tutorial2.json b/levels/tutorial2.json new file mode 100644 index 0000000..a42fde5 --- /dev/null +++ b/levels/tutorial2.json @@ -0,0 +1,54 @@ +{ + "dimensions": [4, 8], + "player1": { + "position": [1, 7], + "color": [66, 134, 244], + }, + "player2": { + "position": [1, 7], + "color": [244, 83, 65], + }, + "goal1": [0, 0], + "goal2": [3, 0], + "blocks": [ + { + "movable": false, + "orientation": 1, + "color": [0, 0, 0], + "segments": [ + [0, 2, 0, 0], + [3, 2, 0, 0], + [0, 4, 0, 0], + [3, 4, 0, 0], + [3, 6, 0, 0], + [0, 2, 0, 1], + [3, 2, 0, 1], + [0, 4, 0, 1], + [0, 6, 0, 1], + [3, 6, 0, 1], + ], + }, + { + "movable": true, + "orientation": 1, + "color": [255, 10, 100], + "segments": [ + [1, 5, 0, 0], + [2, 5, 4, 0], + [1, 5, 0, 1], + [2, 5, 4, 1], + ], + }, + { + "movable": true, + "orientation": 1, + "color": [10, 255, 100], + "segments": [ + [1, 3, 3, 0], + [2, 3, 0, 0], + [1, 3, 3, 1], + [2, 3, 0, 1], + ], + }, + ], +} diff --git a/src/data.rs b/src/data.rs index 10ecf24..f08ade6 100644 --- a/src/data.rs +++ b/src/data.rs @@ -17,7 +17,7 @@ pub struct LevelData { pub dimensions: [u32; 2], pub player1: PlayerData, pub player2: PlayerData, - pub goal1: (u32, u32), - pub goal2: (u32, u32), + pub goal1: (i32, i32), + pub goal2: (i32, i32), pub blocks: Vec, } diff --git a/src/game.rs b/src/game.rs index faafd0f..898cf37 100644 --- a/src/game.rs +++ b/src/game.rs @@ -18,6 +18,7 @@ const CELL_FRAG: &str = include_str!("../shaders/cell.frag"); const SEGMENT_IMAGE: &[u8] = include_bytes!("../textures/segment.png"); const LEVEL_TUTORIAL: &str = include_str!("../levels/tutorial.json"); +const LEVEL_TUTORIAL2: &str = include_str!("../levels/tutorial2.json"); pub struct Game<'a> { pub resources: Resources, @@ -41,7 +42,10 @@ impl<'a> Game<'a> { .load_shader(display, "cell", &CELL_VERT, &CELL_FRAG) .unwrap(); - let levels = vec![Level::from_json(&LEVEL_TUTORIAL)]; + let levels = vec![ + Level::from_json(&LEVEL_TUTORIAL), + Level::from_json(&LEVEL_TUTORIAL2), + ]; Game { resources, display, diff --git a/src/level/mod.rs b/src/level/mod.rs index b316b10..e4927ae 100644 --- a/src/level/mod.rs +++ b/src/level/mod.rs @@ -7,6 +7,7 @@ mod player; use std::collections::{HashMap, HashSet, VecDeque}; use crate::animations::AnimationState; +use crate::color::Color; use crate::data::LevelData; use crate::enums::{Board, Orientation, PushDir, Shape}; use crate::renderer::Renderer; @@ -20,8 +21,8 @@ pub struct Level { blocks: Vec, player1: Player, player2: Player, - goal1: (u32, u32), - goal2: (u32, u32), + goal1: (i32, i32), + goal2: (i32, i32), } #[derive(Copy, Clone, Debug, PartialOrd, PartialEq)] @@ -75,10 +76,7 @@ impl Level { // 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 + self.player1.position == self.goal1 && self.player2.position == self.goal2 } pub fn apply_change_set(&mut self, change_set: ChangeSet) { @@ -363,6 +361,10 @@ impl Level { } } + // render goals + self.render_goal(renderer, self.goal1, scale, left_off); + self.render_goal(renderer, self.goal2, scale, right_off); + // render player self.render_player( renderer, @@ -406,4 +408,24 @@ impl Level { Shape::Full, ); } + + fn render_goal( + &self, + renderer: &mut Renderer, + location: (i32, i32), + scale: i32, + offset: (i32, i32), + ) { + let position = ( + offset.0 + location.0 * scale + 4, + offset.1 + location.1 * scale + 4, + ); + renderer.render_segment( + position, + scale - 8, + Color::from_rgb_u32(102, 204, 102), + Orientation::Both, + Shape::Full, + ); + } }