add second level

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

54
levels/tutorial2.json Normal file
View file

@ -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],
],
},
],
}

View file

@ -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<BlockData>,
}

View file

@ -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,

View file

@ -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<Block>,
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,
);
}
}