Compare commits

..

No commits in common. "xplatform-refactor" and "rewrite" have entirely different histories.

59 changed files with 1712 additions and 3189 deletions

2
.cargo/config Normal file
View file

@ -0,0 +1,2 @@
[alias]
run-wasm = "run --release --package run-wasm --"

1
.envrc Normal file
View file

@ -0,0 +1 @@
# use flake

15
.gitignore vendored
View file

@ -1,2 +1,17 @@
/target
**/*.rs.bk
.direnv
# Added by cargo
#
# already existing elements were commented out
#/target
# Added by cargo
#
# already existing elements were commented out
#/target

1619
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -1,7 +1,13 @@
[workspace]
members = ["core", "desktop", "web"]
[package]
name = "wedge"
version = "0.1.0"
edition = "2021"
[profile.release]
opt-level = 'z'
lto = true
panic = 'abort'
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0.79"
json5 = "0.4.1"
macroquad = "0.4.4"
serde = { version = "1.0.195", features = ["derive"] }
serde_json = "1.0.111"

View file

@ -1,15 +0,0 @@
[package]
name = "wedge_core"
version = "0.1.0"
authors = ["Michael Zhang <iptq@protonmail.com>"]
edition = "2018"
[dependencies]
cassowary = "0.3"
glium = "0.25"
image = "0.21"
json5 = "0.2"
nalgebra = "0.18"
nalgebra-glm = "0.4"
serde = "1.0"
serde_derive = "1.0"

View file

@ -1,22 +0,0 @@
#version 330
in vec4 pos;
out vec4 outcolor;
uniform vec4 color;
const float threshold = 0.05;
const vec4 top = vec4(0.5, 0.5, 0.5, 1.0);
const vec4 bot = vec4(0.4, 0.4, 0.4, 1.0);
void main() {
outcolor = vec4(0.2 * (0.4 * (1 - pos.y) + 0.5 * (1 - pos.x)) + 0.2);
outcolor.w = 1.0;
// if ((pos.x > -threshold && pos.x < threshold)
// || (pos.y > -threshold && pos.y < threshold)
// || (pos.x > 1.0-threshold && pos.x < 1.0+threshold)
// || (pos.y > 1.0-threshold && pos.y < 1.0+threshold)) {
// outcolor = vec4(0.0, 0.0, 0.0, 1.0);
// }
}

View file

@ -1,12 +0,0 @@
#version 330
in vec2 point;
out vec4 pos;
uniform mat4 target;
uniform mat4 projection;
void main() {
pos = vec4(point, 0.0, 1.0);
gl_Position = projection * target * pos;
}

View file

@ -1,11 +0,0 @@
#version 330
in vec2 v_tex_coords;
out vec4 outcolor;
uniform sampler2D tex;
uniform vec4 tint;
void main() {
outcolor = tint * texture(tex, v_tex_coords);
}

View file

@ -1,13 +0,0 @@
#version 330
in vec2 pos;
in vec2 tex;
out vec2 v_tex_coords;
uniform mat4 target;
uniform mat4 projection;
void main() {
v_tex_coords = tex;
gl_Position = projection * target * vec4(pos, 0.0, 1.0);
}

View file

@ -1,105 +0,0 @@
use std::collections::HashMap;
use std::time::Duration;
use crate::enums::Board;
use crate::level::{ChangeSet, Entity, FailSet};
pub type MoveResult = Result<ChangeSet, FailSet>;
pub type BlockOffsets = HashMap<Entity, (f32, f32)>;
// TODO: don't yeet around a HashMap all the time
pub type AnimationFn = Box<Fn(MoveResult, BlockOffsets, f32) -> BlockOffsets>;
// in seconds
const ANIMATION_DURATION: f32 = 1.0 / 6.0;
#[derive(Default)]
pub struct AnimationState {
pub is_animating: bool,
pub last_move_result: Option<MoveResult>,
pub progress: f32,
pub block_offsets: BlockOffsets,
progress_function: Option<AnimationFn>,
}
impl AnimationState {
pub fn new() -> Self {
AnimationState {
is_animating: false,
last_move_result: None,
progress: 0.0,
block_offsets: BlockOffsets::new(),
progress_function: None,
}
}
pub fn begin_move_transition(&mut self, result: MoveResult) {
self.last_move_result = Some(result);
self.is_animating = true;
self.progress = 0.0;
let func = |last_move_result: MoveResult, mut offsets: BlockOffsets, progress: f32| {
use std::f32::consts::PI;
match last_move_result {
// transition
Ok(change_set) => {
for (entity, direction) in change_set {
// TODO: implement ease-out?
let pair = direction.as_pair();
// cap progress at 1.0, we don't want blocks going past where they're supposed to
let progress = progress.min(1.0);
let offset = (pair.0 as f32 * progress, pair.1 as f32 * progress);
offsets.insert(entity, offset);
}
}
// vibrate all blocking pieces
Err(fail_set) => {
for index in fail_set {
let delta = 0.05 * (4.0 * PI * progress).sin() / (progress + 0.5);
offsets.insert(Entity::Block(index), (delta, delta));
}
}
}
offsets
};
self.progress_function = Some(Box::new(func));
}
pub fn make_progress(&mut self, delta: Duration) {
let progress = self.progress + (delta.as_millis() as f32 / ANIMATION_DURATION) / 1000.0;
let block_offsets = if let Some(f) = &self.progress_function {
Some(f(
self.last_move_result.clone().unwrap(),
self.block_offsets.clone(),
progress,
))
} else {
None
};
// this should always work
if let Some(block_offsets) = block_offsets {
self.block_offsets = block_offsets;
self.progress = progress;
}
if self.progress > 1.0 {
self.is_animating = false;
self.block_offsets = BlockOffsets::new();
}
}
pub fn get_block_offset(&self, index: usize) -> (f32, f32) {
self.block_offsets
.get(&Entity::Block(index))
.cloned()
.unwrap_or_else(|| (0.0, 0.0))
}
pub fn get_player_offset(&self, board: Board) -> (f32, f32) {
self.block_offsets
.get(&Entity::Player(board))
.cloned()
.unwrap_or_else(|| (0.0, 0.0))
}
}

View file

@ -1,14 +0,0 @@
#[derive(Copy, Clone, Debug)]
pub struct Color(pub f32, pub f32, pub f32, pub f32);
impl Color {
pub fn from_rgb_u32(r: u32, g: u32, b: u32) -> Self {
Color(r as f32 / 256.0, g as f32 / 256.0, b as f32 / 256.0, 1.0)
}
}
impl From<(u32, u32, u32)> for Color {
fn from(tuple: (u32, u32, u32)) -> Self {
Color::from_rgb_u32(tuple.0, tuple.1, tuple.2)
}
}

View file

@ -1,42 +0,0 @@
#[derive(Debug, Deserialize)]
pub struct PlayerData {
pub position: (i32, i32),
pub color: (u32, u32, u32),
}
#[derive(Debug, Deserialize)]
pub struct BlockData {
pub movable: bool,
pub orientation: u32,
pub color: (u32, u32, u32),
pub segments: Vec<[i32; 4]>,
}
#[derive(Debug, Deserialize)]
pub struct LevelData {
pub dimensions: (u32, u32),
pub player1: PlayerData,
pub player2: PlayerData,
pub goal1: (i32, i32),
pub goal2: (i32, i32),
pub blocks: Vec<BlockData>,
}
impl LevelData {
pub fn empty() -> LevelData {
LevelData {
dimensions: (5, 5),
player1: PlayerData {
position: (0, 0),
color: (66, 134, 244),
},
player2: PlayerData {
position: (0, 0),
color: (244, 83, 65),
},
goal1: (4, 4),
goal2: (4, 4),
blocks: Vec::new(),
}
}
}

View file

@ -1,109 +0,0 @@
use std::ops::Add;
#[derive(Debug, Eq, PartialEq, Hash, PartialOrd, Copy, Clone)]
pub enum Board {
Left = 0,
Right = 1,
}
impl From<i32> for Board {
fn from(n: i32) -> Self {
match n {
0 => Board::Left,
1 => Board::Right,
_ => panic!("expecting 0 or 1, got {}", n),
}
}
}
#[derive(Copy, Clone)]
pub enum Orientation {
None = 0,
Horizontal = 1,
Vertical = 2,
Both = 3,
}
impl From<u32> for Orientation {
fn from(n: u32) -> Self {
match n {
0 => Orientation::Both,
1 => Orientation::Horizontal,
2 => Orientation::Vertical,
_ => panic!("expecting 0..2, got {}", n),
}
}
}
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub enum PushDir {
Up,
Down,
Left,
Right,
}
impl PushDir {
pub fn as_pair(self) -> (i32, i32) {
match self {
PushDir::Up => (0, -1),
PushDir::Down => (0, 1),
PushDir::Left => (-1, 0),
PushDir::Right => (1, 0),
}
}
}
impl Add<PushDir> for (i32, i32, Board) {
type Output = (i32, i32, Board);
fn add(self, rhs: PushDir) -> Self::Output {
let offset = rhs.as_pair();
(self.0 + offset.0, self.1 + offset.1, self.2)
}
}
// /\
// /21\
// \34/
// \/
#[derive(Copy, Clone, Debug, PartialOrd, PartialEq)]
pub enum Shape {
Full = 0,
TopRight = 1,
TopLeft = 2,
BottomLeft = 3,
BottomRight = 4,
}
impl From<i32> for Shape {
fn from(n: i32) -> Self {
match n {
0 => Shape::Full,
1 => Shape::TopRight,
2 => Shape::TopLeft,
3 => Shape::BottomLeft,
4 => Shape::BottomRight,
_ => panic!("expecting 0..4, got {}", n),
}
}
}
impl Shape {
pub fn get_opposite(self) -> Option<Shape> {
use Shape::*;
match self {
TopRight => Some(BottomLeft),
BottomLeft => Some(TopRight),
TopLeft => Some(BottomRight),
BottomRight => Some(TopLeft),
Full => None,
}
}
pub fn is_opposite(self, other: Shape) -> bool {
self.get_opposite()
.map(|shape| shape == other)
.unwrap_or_else(|| false)
}
}

View file

@ -1,82 +0,0 @@
use std::collections::HashMap;
use std::time::Duration;
use glium::glutin::{ElementState, Event, VirtualKeyCode, WindowEvent};
use glium::{Display, Frame};
use crate::animations::AnimationState;
use crate::enums::{Board, PushDir};
use crate::keymap::Keymap;
use crate::level::Level;
use crate::renderer::Renderer;
use crate::resources::Resources;
use crate::screens::{MenuScreen, Screen, ScreenStack};
const SEGMENT_VERT: &str = include_str!("../shaders/segment.vert");
const SEGMENT_FRAG: &str = include_str!("../shaders/segment.frag");
const CELL_VERT: &str = include_str!("../shaders/cell.vert");
const CELL_FRAG: &str = include_str!("../shaders/cell.frag");
const SEGMENT_IMAGE: &[u8] = include_bytes!("../textures/segment.png");
pub struct Game<'a> {
pub resources: Resources,
pub display: &'a Display,
keymap: Keymap,
screen_stack: ScreenStack,
}
impl<'a> Game<'a> {
pub fn new(display: &'a Display) -> Game {
let mut resources = Resources::default();
resources
.load_image_from_memory(display, "segment", &SEGMENT_IMAGE, false)
.unwrap();
resources
.load_shader(display, "segment", &SEGMENT_VERT, &SEGMENT_FRAG)
.unwrap();
resources
.load_shader(display, "cell", &CELL_VERT, &CELL_FRAG)
.unwrap();
// bruh
let screen_stack = ScreenStack::with(MenuScreen::new());
Game {
resources,
display,
keymap: Keymap::new(),
screen_stack,
}
}
pub fn handle_event(&mut self, event: Event) {
if let Event::WindowEvent { event, .. } = event {
match event {
WindowEvent::Resized(size) => self.resources.window_dimensions = size.into(),
WindowEvent::KeyboardInput { input, .. } => {
if let Some(code) = &input.virtual_keycode {
if let ElementState::Pressed = &input.state {
self.keymap.pressed(*code);
} else {
self.keymap.release(*code);
}
}
}
_ => (),
}
}
}
pub fn create_renderer<'b>(&self, target: &'b mut Frame) -> Renderer<'b, '_> {
Renderer::new(self, target)
}
pub fn update(&mut self, delta: Duration) {
self.screen_stack.update(delta, &self.keymap);
}
pub fn render(&self, renderer: &mut Renderer) {
self.screen_stack.render(renderer);
}
}

View file

@ -1,27 +0,0 @@
use std::collections::HashMap;
use glium::glutin::VirtualKeyCode;
pub struct Keymap(HashMap<VirtualKeyCode, bool>);
impl Keymap {
pub fn new() -> Self {
Keymap(HashMap::new())
}
pub fn pressed(&mut self, code: VirtualKeyCode) {
self.0.insert(code, true);
}
pub fn release(&mut self, code: VirtualKeyCode) {
self.0.insert(code, false);
}
pub fn is_pressed(&self, code: VirtualKeyCode) -> bool {
if let Some(true) = self.0.get(&code) {
true
} else {
false
}
}
}

View file

@ -1,60 +0,0 @@
use crate::color::Color;
use crate::data::BlockData;
use crate::enums::Orientation;
use crate::level::Segment;
pub trait Blockish {
fn get_color(&self) -> Color;
fn get_orientation(&self) -> Orientation;
// TODO: don't alloc/clone here?
fn get_segments(&self) -> Vec<Segment>;
}
#[derive(Clone)]
pub struct Block {
index: usize,
pub movable: bool,
color: Color,
pub orientation: Orientation,
pub segments: Vec<Segment>,
}
impl Block {
pub fn from_data(index: usize, data: &BlockData) -> Self {
let movable = data.movable;
let segments = data
.segments
.iter()
.map(|segment| Segment {
position: (segment[0], segment[1]),
shape: segment[2].into(),
board: segment[3].into(),
})
.collect();
let orientation = data.orientation.into();
let color = Color::from_rgb_u32(data.color.0, data.color.1, data.color.2);
Block {
index,
movable,
color,
segments,
orientation,
}
}
}
impl Blockish for Block {
fn get_color(&self) -> Color {
self.color
}
fn get_orientation(&self) -> Orientation {
self.orientation
}
fn get_segments(&self) -> Vec<Segment> {
self.segments.clone()
}
}

View file

@ -1,30 +0,0 @@
macro_rules! set {
($($item:expr)*) => {
{
let mut set = std::collections::HashSet::new();
$(set.insert($item);)*
set
}
}
}
macro_rules! fail_set {
($change_set:expr) => {
$change_set
.iter()
.filter_map(|(entity, _)| match entity {
Entity::Block(index) => Some(*index),
Entity::Player(_) => None,
})
.collect()
};
}
macro_rules! entity_fail {
($item:expr) => {
match $item {
Some(index) => set!(index),
None => std::collections::HashSet::new(),
}
};
}

View file

@ -1,420 +0,0 @@
#[macro_use]
mod macros;
mod block;
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;
use self::block::{Block, Blockish};
use self::player::Player;
pub struct Level {
dimensions: (u32, u32),
move_stack: VecDeque<()>,
blocks: Vec<Block>,
player1: Player,
player2: Player,
goal1: (i32, i32),
goal2: (i32, i32),
}
#[derive(Copy, Clone, Debug, PartialOrd, PartialEq)]
pub struct Segment {
position: (i32, i32),
shape: Shape,
board: Board,
}
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub enum Entity {
Block(usize),
Player(Board),
}
pub type ChangeSet = HashMap<Entity, PushDir>;
pub type FailSet = HashSet<usize>;
impl Level {
pub fn from_json(data: impl AsRef<str>) -> Level {
let data: LevelData = json5::from_str(data.as_ref()).unwrap();
let blocks = data
.blocks
.iter()
.enumerate()
.map(|(i, block)| Block::from_data(i, block))
.collect();
let player1 = Player {
position: data.player1.position,
color: data.player1.color.into(),
};
let player2 = Player {
position: data.player2.position,
color: data.player2.color.into(),
};
Level {
dimensions: (data.dimensions.0, data.dimensions.1),
move_stack: VecDeque::new(),
blocks,
player1,
player2,
goal1: data.goal1,
goal2: data.goal2,
}
}
// check if we won
pub fn check_win_condition(&self) -> bool {
self.player1.position == self.goal1 && self.player2.position == self.goal2
}
pub fn apply_change_set(&mut self, change_set: ChangeSet) {
for (entity, direction) in change_set {
let direction = direction.as_pair();
match entity {
Entity::Player(board) => {
let player = match board {
Board::Left => &mut self.player1,
Board::Right => &mut self.player2,
};
player.position.0 += direction.0;
player.position.1 += direction.1;
}
Entity::Block(index) => {
let block = self.blocks.get_mut(index).expect("big failure");
for segment in &mut block.segments {
segment.position.0 += direction.0;
segment.position.1 += direction.1;
}
}
}
}
}
pub fn try_move(&mut self, board: Board, direction: PushDir) -> Result<ChangeSet, FailSet> {
let mut change_set = ChangeSet::default();
change_set.insert(Entity::Player(board), direction);
self.player_can_move(board, direction, change_set)
}
fn player_can_move(
&self,
board: Board,
direction: PushDir,
change_set: ChangeSet,
) -> Result<ChangeSet, FailSet> {
let player = match board {
Board::Left => &self.player1,
Board::Right => &self.player2,
};
let player_segment = Segment {
position: player.position,
shape: Shape::Full,
board,
};
self.segment_can_move(None, player_segment, direction, change_set)
}
fn block_can_move(
&self,
index: usize,
direction: PushDir,
mut change_set: ChangeSet,
) -> Result<ChangeSet, FailSet> {
let block = match self.blocks.get(index) {
Some(block) => block,
None => return Err(HashSet::new()),
};
// is the block even movable?
if !block.movable {
return Err(set!(index));
}
// does the direction match the orientation?
match (block.orientation, direction) {
(Orientation::Horizontal, PushDir::Left)
| (Orientation::Horizontal, PushDir::Right)
| (Orientation::Vertical, PushDir::Up)
| (Orientation::Vertical, PushDir::Down)
| (Orientation::Both, _) => (),
_ => return Err(set!(index)),
}
// TODO: change this to use &mut instead of returning a new one each time
change_set.insert(Entity::Block(index), direction);
for segment in block.get_segments() {
match self.segment_can_move(Some(index), segment, direction, change_set.clone()) {
Ok(new_change_set) => change_set = new_change_set,
Err(fail_set) => return Err(fail_set),
}
}
Ok(change_set)
}
fn segment_can_move(
&self,
block_index: Option<usize>,
segment: Segment,
direction: PushDir,
change_set: ChangeSet,
) -> Result<ChangeSet, FailSet> {
let segment_loc = (segment.position.0, segment.position.1, segment.board);
let target = segment_loc + direction;
// is the target actually in the map?
if target.0 < 0
|| target.0 >= self.dimensions.0 as i32
|| target.1 < 0
|| target.1 >= self.dimensions.1 as i32
{
return Err(entity_fail!(block_index));
}
// retrieve other blocks that might be occupying this current space and the target space
let mut current_occupant = None;
let mut target_occupant = None;
for (i, block) in self.blocks.iter().enumerate() {
// skip other segments of the same block
if let Some(n) = block_index {
if n == i {
continue;
}
}
// offset from the change set
let offset = match change_set.get(&Entity::Block(i)) {
Some(direction) => direction.as_pair(),
None => (0, 0),
};
for segment in block.get_segments() {
// don't get segments on different boards
if segment.board != segment_loc.2 {
continue;
}
let mut segment_pos = segment.position;
segment_pos.0 += offset.0;
segment_pos.1 += offset.1;
if segment_pos == (segment_loc.0, segment_loc.1) {
current_occupant = Some((i, segment.shape));
}
if segment_pos == (target.0, target.1) {
target_occupant = Some((Entity::Block(i), segment.shape));
}
}
}
// handle special pushes
if let Some((other_block, other_shape)) = current_occupant {
// are both shapes triangles?
let both_triangles = match (segment.shape, other_shape) {
(Shape::Full, Shape::Full) => false,
_ => true,
// TODO: enumerate them to get rid of invalid states
};
if both_triangles {
// what directions could we be pushing the other block into?
let possible_directions = match segment.shape {
Shape::TopRight => [PushDir::Up, PushDir::Right],
Shape::TopLeft => [PushDir::Left, PushDir::Up],
Shape::BottomLeft => [PushDir::Down, PushDir::Left],
Shape::BottomRight => [PushDir::Right, PushDir::Down],
Shape::Full => unreachable!("already eliminated this possibility"),
};
// does the direction we're pushing appear in this list?
if possible_directions.contains(&direction) {
// the other shape goes in the other direction
let other_direction = {
let mut set = possible_directions.iter().collect::<HashSet<_>>();
set.remove(&direction);
*set.into_iter().next().unwrap()
};
return self.block_can_move(other_block, other_direction, change_set);
}
}
}
// handle normal pushes
if let Some((entity, shape)) = target_occupant {
match entity {
Entity::Player(_) => {
// TODO: assert that the board is the same
Err(fail_set!(change_set))
}
Entity::Block(index) => {
if
// if it's part of the same block it's ok to push
block_index.is_some() && block_index.unwrap() == index ||
// if the shapes are opposite, we can actually both fit into the same spot
segment.shape.is_opposite(shape)
{
Ok(change_set)
}
// if the block is already in the change set, it can't move
else if change_set.contains_key(&Entity::Block(index)) {
Err(fail_set!(change_set))
}
// if the next block can move then so can this one
else {
self.block_can_move(index, direction, change_set)
}
}
}
} else {
// coast is clear, push away!
Ok(change_set)
}
}
pub fn render(&self, renderer: &mut Renderer, animations: &AnimationState) {
// board positioning calculations
let playfield_ratio = (2 * self.dimensions.0 + 6) as f32 / (self.dimensions.1 + 4) as f32;
let screen_ratio = renderer.window.0 / renderer.window.1;
let cols = self.dimensions.0 as i32;
let rows = self.dimensions.1 as i32;
let (scale, xoff, yoff) = if playfield_ratio > screen_ratio {
let scale = renderer.window.0 as i32 / (2 * cols + 6);
let yoff = renderer.window.1 as i32 / 2 - (rows + 4) * scale / 2;
(scale, 0, yoff)
} else {
let scale = renderer.window.1 as i32 / (rows + 4);
let xoff = renderer.window.0 as i32 / 2 - (2 * cols + 6) * scale / 2;
(scale, xoff, 0)
};
self.render_boards(renderer, scale, (xoff, yoff), animations);
}
fn render_boards(
&self,
renderer: &mut Renderer,
scale: i32,
offset: (i32, i32),
animations: &AnimationState,
) {
let left_off = (offset.0 + 2 * scale, offset.1 + 2 * scale);
let right_off = (
offset.0 + (4 + self.dimensions.0 as i32) * scale,
offset.1 + 2 * scale,
);
// render the grid
// TODO: do this in one single pass instead of once for each cell
for x in 0..self.dimensions.0 as i32 {
for y in 0..self.dimensions.1 as i32 {
renderer.render_cell((left_off.0 + x * scale, left_off.1 + y * scale), scale);
renderer.render_cell((right_off.0 + x * scale, right_off.1 + y * scale), scale);
}
}
// render blocks
for (i, block) in self.blocks.iter().enumerate() {
for segment in block.get_segments().iter() {
let offset = match &segment.board {
Board::Left => left_off,
Board::Right => right_off,
};
let mut location = (
offset.0 + segment.position.0 * scale,
offset.1 + segment.position.1 * scale,
);
let animation_offset = animations.get_block_offset(i);
location.0 += (animation_offset.0 * scale as f32) as i32;
location.1 += (animation_offset.1 * scale as f32) as i32;
renderer.render_segment(
location,
scale,
block.get_color(),
block.get_orientation(),
segment.shape,
);
}
}
// 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,
Board::Left,
&self.player1,
scale,
animations,
left_off,
);
self.render_player(
renderer,
Board::Right,
&self.player2,
scale,
animations,
right_off,
);
}
fn render_player(
&self,
renderer: &mut Renderer,
board: Board,
player: &Player,
scale: i32,
animations: &AnimationState,
offset: (i32, i32),
) {
let mut location = (
offset.0 + player.position.0 * scale + 4,
offset.1 + player.position.1 * scale + 4,
);
let animation_offset = animations.get_player_offset(board);
location.0 += (animation_offset.0 * scale as f32) as i32;
location.1 += (animation_offset.1 * scale as f32) as i32;
renderer.render_segment(
location,
scale - 8,
player.color,
Orientation::Both,
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,
);
}
}

View file

@ -1,23 +0,0 @@
use crate::color::Color;
use crate::enums::Orientation;
use crate::level::{Blockish, Segment};
#[derive(Copy, Clone)]
pub struct Player {
pub position: (i32, i32),
pub color: Color,
}
impl Blockish for Player {
fn get_color(&self) -> Color {
self.color
}
fn get_orientation(&self) -> Orientation {
Orientation::None
}
fn get_segments(&self) -> Vec<Segment> {
vec![]
}
}

View file

@ -1,86 +0,0 @@
#[macro_use]
extern crate glium;
extern crate nalgebra_glm as glm;
#[macro_use]
extern crate serde_derive;
mod animations;
mod color;
mod data;
mod enums;
mod game;
mod keymap;
mod level;
mod platform;
mod renderer;
mod resources;
mod screens;
use std::time::Instant;
use glium::glutin::dpi::PhysicalSize;
use glium::glutin::{ContextBuilder, Event, EventsLoop, WindowBuilder, WindowEvent};
use glium::{Display, Surface};
use crate::game::Game;
pub use crate::platform::Platform;
const GAME_WIDTH: u32 = 1024;
const GAME_HEIGHT: u32 = 768;
pub fn run_with<P: Platform>(platform: P) {
let mut closed = false;
let mut prev = Instant::now();
while !closed {
let now = Instant::now();
let delta = now - prev;
}
}
fn main() {
let mut events_loop = EventsLoop::new();
let primary_monitor = events_loop.get_primary_monitor();
let dpi_factor = primary_monitor.get_hidpi_factor();
let dimensions: PhysicalSize = (GAME_WIDTH, GAME_HEIGHT).into();
let wb = WindowBuilder::new()
.with_dimensions(dimensions.to_logical(dpi_factor))
.with_resizable(false)
.with_title("wedge");
let cb = ContextBuilder::new();
let display = Display::new(wb, cb, &events_loop).unwrap();
{
let gl_window = display.gl_window();
let window = gl_window.window();
}
let mut game = Game::new(&display);
let mut closed = false;
let mut prev = Instant::now();
while !closed {
let now = Instant::now();
let delta = now - prev;
events_loop.poll_events(|event| match event {
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => closed = true,
_ => game.handle_event(event),
});
game.update(delta);
let mut target = display.draw();
target.clear(None, Some((0.0, 0.0, 0.0, 1.0)), true, None, None);
let mut renderer = game.create_renderer(&mut target);
game.render(&mut renderer);
target.finish().unwrap();
prev = now;
}
}

View file

@ -1,13 +0,0 @@
pub trait Platform {
fn init() {}
type GlslCompileContext;
type GlslProgram;
type GlslError;
fn compile_glsl_program<'a>(
&self,
context: &'a Self::GlslCompileContext,
vert: impl AsRef<str>,
frag: impl AsRef<str>,
) -> Result<Self::GlslProgram, Self::GlslError>;
}

View file

@ -1,229 +0,0 @@
use glium::draw_parameters::{Blend, DrawParameters};
use glium::index::{NoIndices, PrimitiveType};
use glium::{Display, Frame, Program, Surface, Texture2d, VertexBuffer};
use nalgebra::{Matrix4, Vector4};
use crate::color::Color;
use crate::enums::{Orientation, Shape};
use crate::game::Game;
pub struct Renderer<'a, 'b> {
pub window: (f32, f32),
target: &'a mut Frame,
display: &'b Display,
cell_program: &'b Program,
segment_program: &'b Program,
segment_texture: &'b Texture2d,
}
impl<'a, 'b> Renderer<'a, 'b> {
pub fn new(game: &'b Game, target: &'a mut Frame) -> Self {
Renderer {
window: (
game.resources.window_dimensions.0 as f32,
game.resources.window_dimensions.1 as f32,
),
target,
display: &game.display,
cell_program: game.resources.get_shader("cell").unwrap(),
segment_program: game.resources.get_shader("segment").unwrap(),
segment_texture: game.resources.get_texture("segment").unwrap(),
}
}
pub fn render_cell(&mut self, location: (i32, i32), scale: i32) {
#[derive(Copy, Clone)]
struct Vertex {
point: [f32; 2],
}
implement_vertex!(Vertex, point);
let indices = NoIndices(PrimitiveType::TrianglesList);
let mut vertices = Vec::<Vertex>::new();
vertices.push(Vertex { point: [0.0, 0.0] });
vertices.push(Vertex { point: [1.0, 0.0] });
vertices.push(Vertex { point: [0.0, 1.0] });
vertices.push(Vertex { point: [1.0, 1.0] });
vertices.push(Vertex { point: [0.0, 1.0] });
vertices.push(Vertex { point: [1.0, 0.0] });
let vertex_buffer = VertexBuffer::new(self.display, &vertices).unwrap();
let projection = glm::ortho::<f32>(
0.0,
self.window.0 as f32,
self.window.1 as f32,
0.0,
-1.0,
1.0,
);
let mut matrix = Matrix4::<f32>::identity();
matrix = matrix.append_nonuniform_scaling(&[scale as f32, scale as f32, 1.0].into());
matrix = matrix.append_translation(&[location.0 as f32, location.1 as f32, 0.0].into());
let color = Vector4::from([0.6, 0.6, 0.8, 1.0f32]);
let uniforms = uniform! {
target: *matrix.as_ref(),
projection: *projection.as_ref(),
color: *color.as_ref(),
};
self.target
.draw(
&vertex_buffer,
&indices,
&self.cell_program,
&uniforms,
&DrawParameters {
blend: Blend::alpha_blending(),
..Default::default()
},
)
.unwrap();
}
pub fn render_segment(
&mut self,
location: (i32, i32),
scale: i32,
color: Color,
orientation: Orientation,
shape: Shape,
) {
#[derive(Copy, Clone)]
struct Vertex {
pos: [f32; 2],
tex: [f32; 2],
}
implement_vertex!(Vertex, pos, tex);
let indices = NoIndices(PrimitiveType::TrianglesList);
let mut vertices = Vec::new();
match shape {
Shape::BottomLeft => {
vertices.push(Vertex {
pos: [1.0, 1.0],
tex: [1.0, 1.0],
});
vertices.push(Vertex {
pos: [1.0, 0.0],
tex: [1.0, 0.0],
});
vertices.push(Vertex {
pos: [0.0, 0.0],
tex: [0.0, 0.0],
});
}
Shape::TopLeft => {
vertices.push(Vertex {
pos: [0.0, 1.0],
tex: [0.0, 1.0],
});
vertices.push(Vertex {
pos: [1.0, 1.0],
tex: [1.0, 1.0],
});
vertices.push(Vertex {
pos: [1.0, 0.0],
tex: [1.0, 0.0],
});
}
Shape::TopRight => {
vertices.push(Vertex {
pos: [1.0, 1.0],
tex: [1.0, 1.0],
});
vertices.push(Vertex {
pos: [0.0, 1.0],
tex: [0.0, 1.0],
});
vertices.push(Vertex {
pos: [0.0, 0.0],
tex: [0.0, 0.0],
});
}
Shape::BottomRight => {
vertices.push(Vertex {
pos: [0.0, 1.0],
tex: [0.0, 1.0],
});
vertices.push(Vertex {
pos: [1.0, 0.0],
tex: [1.0, 0.0],
});
vertices.push(Vertex {
pos: [0.0, 0.0],
tex: [0.0, 0.0],
});
}
_ => {
vertices.push(Vertex {
pos: [0.0, 1.0],
tex: [0.0, 1.0],
});
vertices.push(Vertex {
pos: [1.0, 0.0],
tex: [1.0, 0.0],
});
vertices.push(Vertex {
pos: [0.0, 0.0],
tex: [0.0, 0.0],
});
vertices.push(Vertex {
pos: [0.0, 1.0],
tex: [0.0, 1.0],
});
vertices.push(Vertex {
pos: [1.0, 1.0],
tex: [1.0, 1.0],
});
vertices.push(Vertex {
pos: [1.0, 0.0],
tex: [1.0, 0.0],
});
}
}
let vertex_buffer = VertexBuffer::new(self.display, &vertices).unwrap();
let tint = Vector4::from([color.0, color.1, color.2, 1.0f32]);
let projection = glm::ortho::<f32>(
0.0,
self.window.0 as f32,
self.window.1 as f32,
0.0,
-1.0,
1.0,
);
let mut matrix = Matrix4::<f32>::identity();
matrix = matrix.append_nonuniform_scaling(&[scale as f32, scale as f32, 1.0].into());
matrix = matrix.append_translation(&[location.0 as f32, location.1 as f32, 0.0].into());
let rotate_texture = match orientation {
Orientation::Both => false,
Orientation::None => false,
Orientation::Vertical => true,
Orientation::Horizontal => false,
};
let uniforms = uniform! {
target: *matrix.as_ref(),
rotate_texture: rotate_texture,
projection: *projection.as_ref(),
tint: *tint.as_ref(),
tex: self.segment_texture,
};
self.target
.draw(
&vertex_buffer,
&indices,
&self.segment_program,
&uniforms,
&DrawParameters {
blend: Blend::alpha_blending(),
..Default::default()
},
)
.unwrap();
}
}

View file

@ -1,66 +0,0 @@
use std::collections::HashMap;
use glium::texture::RawImage2d;
use glium::{Display, Program, ProgramCreationError, Texture2d};
use image::{DynamicImage, GenericImageView, ImageError};
#[derive(Default)]
pub struct Resources {
pub window_dimensions: (u32, u32),
textures: HashMap<String, Texture2d>,
shaders: HashMap<String, Program>,
}
impl Resources {
pub fn load_image_from_memory(
&mut self,
display: &Display,
name: impl AsRef<str>,
buffer: &[u8],
alpha: bool,
) -> Result<(), ImageError> {
let image = image::load_from_memory(buffer)?;
self.load_image(display, name, image, alpha);
Ok(())
}
pub fn load_image(
&mut self,
display: &Display,
name: impl AsRef<str>,
image: DynamicImage,
alpha: bool,
) {
let name = name.as_ref().to_owned();
let dimensions = image.dimensions();
let image = if alpha {
RawImage2d::from_raw_rgba_reversed(&image.raw_pixels(), dimensions)
} else {
RawImage2d::from_raw_rgb_reversed(&image.raw_pixels(), dimensions)
};
// TODO: don't unwrap
let texture = Texture2d::new(display, image).unwrap();
self.textures.insert(name, texture);
}
pub fn get_texture(&self, name: impl AsRef<str>) -> Option<&Texture2d> {
self.textures.get(name.as_ref())
}
pub fn load_shader(
&mut self,
display: &Display,
name: impl AsRef<str>,
vertex: &str,
fragment: &str,
) -> Result<(), ProgramCreationError> {
let name = name.as_ref().to_owned();
let program = Program::from_source(display, vertex, fragment, None)?;
self.shaders.insert(name, program);
Ok(())
}
pub fn get_shader(&self, name: impl AsRef<str>) -> Option<&Program> {
self.shaders.get(name.as_ref())
}
}

View file

@ -1,19 +0,0 @@
use crate::data::LevelData;
use crate::renderer::Renderer;
use crate::screens::Screen;
pub struct EditorScreen {
level: LevelData,
}
impl Screen for EditorScreen {
fn render(&self, renderer: &mut Renderer) {}
}
impl EditorScreen {
pub fn new() -> EditorScreen {
EditorScreen {
level: LevelData::empty(),
}
}
}

View file

@ -1,28 +0,0 @@
use std::time::Duration;
use glium::glutin::VirtualKeyCode;
use crate::keymap::Keymap;
use crate::screens::{EditorScreen, PlayScreen, Screen, ScreenAction};
pub struct MenuScreen;
impl Screen for MenuScreen {
fn update(&mut self, delta: Duration, keymap: &Keymap) -> ScreenAction {
if keymap.is_pressed(VirtualKeyCode::Space) {
let play_screen = PlayScreen::new();
ScreenAction::Push(Box::new(play_screen))
} else if keymap.is_pressed(VirtualKeyCode::E) {
let editor_screen = EditorScreen::new();
ScreenAction::Push(Box::new(editor_screen))
} else {
ScreenAction::None
}
}
}
impl MenuScreen {
pub fn new() -> MenuScreen {
MenuScreen
}
}

View file

@ -1,69 +0,0 @@
mod editor;
mod menu;
mod play;
use std::sync::Arc;
use std::time::Duration;
use crate::keymap::Keymap;
use crate::renderer::Renderer;
pub use self::editor::EditorScreen;
pub use self::menu::MenuScreen;
pub use self::play::PlayScreen;
pub trait Screen {
fn update(&mut self, delta: Duration, keymap: &Keymap) -> ScreenAction {
ScreenAction::None
}
fn render(&self, renderer: &mut Renderer) {}
}
pub enum ScreenAction {
None,
Push(Box<dyn Screen>),
Pop(usize),
}
pub struct ScreenStack(Vec<Box<dyn Screen>>);
impl ScreenStack {
pub fn with<S: 'static + Screen>(screen: S) -> Self {
let mut stack = Vec::<Box<Screen>>::new();
stack.push(Box::new(screen));
ScreenStack(stack)
}
pub fn top(&self) -> impl AsRef<dyn Screen + 'static> + '_ {
self.0.last().unwrap()
}
pub fn top_mut(&mut self) -> impl AsMut<dyn Screen + 'static> + '_ {
self.0.last_mut().unwrap()
}
pub fn update(&mut self, delta: Duration, keymap: &Keymap) {
let result = {
let mut screen = self.top_mut();
let screen = screen.as_mut();
screen.update(delta, keymap)
};
match result {
ScreenAction::None => (),
ScreenAction::Push(new_screen) => {
self.0.push(new_screen);
}
ScreenAction::Pop(n) => {
self.0.truncate(self.0.len() - 1);
}
}
}
pub fn render(&self, renderer: &mut Renderer) {
let screen = self.top();
let screen = screen.as_ref();
screen.render(renderer)
}
}

View file

@ -1,105 +0,0 @@
use std::time::Duration;
use glium::glutin::VirtualKeyCode;
use crate::animations::AnimationState;
use crate::enums::{Board, PushDir};
use crate::keymap::Keymap;
use crate::level::Level;
use crate::renderer::Renderer;
use crate::screens::{Screen, ScreenAction};
const LEVEL_TUTORIAL: &str = include_str!("../../levels/tutorial.json");
const LEVEL_TUTORIAL2: &str = include_str!("../../levels/tutorial2.json");
pub struct PlayScreen {
animations: AnimationState,
levels: Vec<Level>,
current_level: usize,
}
impl Screen for PlayScreen {
fn update(&mut self, delta: Duration, keymap: &Keymap) -> ScreenAction {
macro_rules! trigger_move {
($key:expr, $board:expr, $direction:expr) => {
if keymap.is_pressed($key) {
let level = self.get_current_level_mut();
let result = level.try_move($board, $direction);
self.animations.begin_move_transition(result);
}
};
}
if self.animations.is_animating {
self.animations.make_progress(delta);
// we just finished!
if !self.animations.is_animating {
// apply the changes to the entities
// this indirection is used to dodge a concurrent borrow
let change_set = if let Some(Ok(change_set)) = &self.animations.last_move_result {
Some(change_set.clone())
} else {
None
};
if let Some(change_set) = change_set {
let level = self.get_current_level_mut();
level.apply_change_set(change_set.clone());
self.check_win_condition();
}
}
} else {
trigger_move!(VirtualKeyCode::W, Board::Left, PushDir::Up);
trigger_move!(VirtualKeyCode::A, Board::Left, PushDir::Left);
trigger_move!(VirtualKeyCode::S, Board::Left, PushDir::Down);
trigger_move!(VirtualKeyCode::D, Board::Left, PushDir::Right);
trigger_move!(VirtualKeyCode::I, Board::Right, PushDir::Up);
trigger_move!(VirtualKeyCode::J, Board::Right, PushDir::Left);
trigger_move!(VirtualKeyCode::K, Board::Right, PushDir::Down);
trigger_move!(VirtualKeyCode::L, Board::Right, PushDir::Right);
}
if keymap.is_pressed(VirtualKeyCode::Escape) {
return ScreenAction::Pop(1);
}
ScreenAction::None
}
fn render(&self, renderer: &mut Renderer) {
let level = self.get_current_level();
level.render(renderer, &self.animations);
}
}
impl PlayScreen {
pub fn get_current_level(&self) -> &Level {
self.levels.get(self.current_level).unwrap()
}
pub fn get_current_level_mut(&mut self) -> &mut Level {
self.levels.get_mut(self.current_level).unwrap()
}
pub fn new() -> PlayScreen {
let levels = vec![
Level::from_json(&LEVEL_TUTORIAL),
Level::from_json(&LEVEL_TUTORIAL2),
];
PlayScreen {
levels,
current_level: 0,
animations: AnimationState::new(),
}
}
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;
}
}
}

View file

@ -1,3 +0,0 @@
pub struct Entry {
contents: String,
}

View file

@ -1 +0,0 @@
mod entry;

Binary file not shown.

Before

Width:  |  Height:  |  Size: 23 KiB

12
default.nix Normal file
View file

@ -0,0 +1,12 @@
{ rustc, cargo, makeRustPlatform, cmake, pkg-config, fontconfig }:
let rustPlatform = makeRustPlatform { inherit cargo rustc; };
in rustPlatform.buildRustPackage {
name = "wedge";
src = ./.;
cargoLock.lockFile = ./Cargo.lock;
nativeBuildInputs = [ cmake pkg-config ];
buildInputs = [ fontconfig ];
}

View file

@ -1,9 +0,0 @@
[package]
name = "wedge_desktop"
version = "0.1.0"
authors = ["Michael Zhang <iptq@protonmail.com>"]
edition = "2018"
[dependencies]
glium = "0.25"
wedge_core = { path = "../core" }

View file

@ -1,10 +0,0 @@
mod platform;
use wedge_core::Platform;
use crate::platform::DesktopPlatform;
fn main() {
let platform = DesktopPlatform::new();
wedge_core::run_with(platform);
}

View file

@ -1,28 +0,0 @@
use glium::{Display, Program, ProgramCreationError};
use wedge_core::Platform;
pub struct DesktopPlatform {
}
impl DesktopPlatform {
pub fn new() -> Self {
DesktopPlatform {}
}
}
impl Platform for DesktopPlatform {
type GlslCompileContext = Display;
type GlslProgram = Program;
type GlslError = ProgramCreationError;
fn compile_glsl_program<'a>(
&self,
context: &'a Self::GlslCompileContext,
vert: impl AsRef<str>,
frag: impl AsRef<str>,
) -> Result<Self::GlslProgram, Self::GlslError> {
let vert = vert.as_ref();
let frag = frag.as_ref();
Program::from_source(context, vert, frag, None)
}
}

97
flake.lock Normal file
View file

@ -0,0 +1,97 @@
{
"nodes": {
"fenix": {
"inputs": {
"nixpkgs": [
"nixpkgs"
],
"rust-analyzer-src": "rust-analyzer-src"
},
"locked": {
"lastModified": 1706163833,
"narHash": "sha256-Vw+jTVtKceT+ScaIn7tHy8JjRZZpmg2fAdoInLAsW/M=",
"owner": "nix-community",
"repo": "fenix",
"rev": "043f63f55e9c9b808852ea82edee1f2a1af37e91",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "fenix",
"type": "github"
}
},
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1705309234,
"narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26",
"type": "github"
},
"original": {
"id": "flake-utils",
"type": "indirect"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1706006310,
"narHash": "sha256-nDPz0fj0IFcDhSTlXBU2aixcnGs2Jm4Zcuoj0QtmiXQ=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b43bb235efeab5324c5e486882ef46749188eee2",
"type": "github"
},
"original": {
"id": "nixpkgs",
"type": "indirect"
}
},
"root": {
"inputs": {
"fenix": "fenix",
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs"
}
},
"rust-analyzer-src": {
"flake": false,
"locked": {
"lastModified": 1706106882,
"narHash": "sha256-31DivWu0cC50gR2CgbGtLCf77nuiw4kdiI7B8ioqzLw=",
"owner": "rust-lang",
"repo": "rust-analyzer",
"rev": "0d52934d19d7addcafcfda92a1d547b51556beec",
"type": "github"
},
"original": {
"owner": "rust-lang",
"ref": "nightly",
"repo": "rust-analyzer",
"type": "github"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

64
flake.nix Normal file
View file

@ -0,0 +1,64 @@
{
inputs = {
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { self, nixpkgs, flake-utils, fenix }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ fenix.overlays.default ];
};
rustc = pkgs.fenix.stable.rustc;
cargo = pkgs.fenix.stable.cargo;
neededLibs = with pkgs;
(with xorg; [ ])
++ [ ];
flakePkgs = { wedge = pkgs.callPackage ./. { inherit rustc cargo; }; };
in
rec {
packages = flake-utils.lib.flattenTree flakePkgs;
defaultPackage = packages.wedge;
devShell = pkgs.mkShell {
inputsFrom = with packages; [ wedge ];
shellHook = ''
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:${
pkgs.lib.makeLibraryPath neededLibs
}"
'';
packages = (with pkgs; [
# cargo-watch
# cargo-deny
# cargo-edit
# sqlx-cli
# sqlite
(with pkgs.fenix;
combine [
rustc
cargo
# targets.wasm32-unknown-unknown.latest.rust-std
])
]);
PKG_CONFIG_PATH = with pkgs;
lib.concatStringsSep ":" [
# "${fontconfig.dev}/lib/pkgconfig"
# "${xorg.libX11.dev}/lib/pkgconfig"
# "${xorg.libXcursor.dev}/lib/pkgconfig"
# "${xorg.libXi.dev}/lib/pkgconfig"
# "${xorg.libXrandr.dev}/lib/pkgconfig"
];
};
});
}

124
levels/level1.json Normal file
View file

@ -0,0 +1,124 @@
{
"dimensions": [8, 8],
"player1": {
"position": [5, 5],
"color": [66, 134, 244],
},
"player2": {
"position": [5, 5],
"color": [244, 83, 65],
},
"goal1": [0, 0],
"goal2": [0, 0],
"blocks": [
{
"movable": true,
"orientation": 2,
"color": [255, 10, 100],
"segments": [
[6, 4, 2, 0],
[6, 5, 0, 0],
],
},
{
"movable": true,
"orientation": 1,
"color": [0, 255, 100],
"segments": [
[4, 3, 2, 0],
[5, 3, 0, 0],
[6, 3, 4, 0],
[4, 3, 2, 1],
[5, 3, 0, 1],
[6, 3, 4, 1],
],
},
{
"movable": true,
"orientation": 2,
"color": [20, 25, 100],
"segments": [
[6, 2, 4, 0],
[6, 1, 0, 0],
[6, 2, 4, 1],
[6, 1, 0, 1],
],
},
{
"movable": true,
"orientation": 1,
"color": [110, 30, 230],
"segments": [
[3, 5, 4, 0],
[2, 5, 0, 0],
[1, 5, 0, 0],
[3, 5, 4, 1],
[2, 5, 0, 1],
[1, 5, 0, 1],
],
},
{
"movable": true,
"orientation": 2,
"color": [240, 50, 60],
"segments": [
[3, 5, 2, 0],
[3, 6, 0, 0],
],
},
{
"movable": true,
"orientation": 2,
"color": [120, 220, 20],
"segments": [
[3, 3, 4, 1],
[3, 2, 0, 1],
],
},
{
"movable": false,
"orientation": 0,
"color": [0, 0, 0],
"segments": [
[2, 0, 0, 0],
[2, 1, 0, 0],
[2, 2, 0, 0],
[2, 3, 0, 0],
[2, 0, 0, 1],
[2, 1, 0, 1],
[2, 2, 0, 1],
[2, 3, 0, 1],
],
},
{
"movable": false,
"orientation": 0,
"color": [0, 0, 0],
"segments": [
[4, 4, 0, 0],
[4, 5, 0, 0],
[4, 6, 0, 0],
[4, 7, 0, 0],
[4, 4, 0, 1],
[4, 5, 0, 1],
[4, 6, 0, 1],
[4, 7, 0, 1],
],
},
{
"movable": false,
"orientation": 0,
"color": [0, 0, 0],
"segments": [
[7, 4, 0, 0],
[7, 5, 0, 0],
[7, 6, 0, 0],
[7, 7, 0, 0],
[7, 4, 0, 1],
[7, 5, 0, 1],
[7, 6, 0, 1],
[7, 7, 0, 1],
],
},
],
}

View file

@ -1,38 +0,0 @@
wedge: a puzzle game
====================
![screenshot](screenshot.jpg?raw=true)
Mechanics:
- **Objective:** Get the two players to reach the goals by navigating the level and pushing blocks around.
- Blocks with triangular sections will push other blocks in the other direction.
- Blocks of the same color will always move together.
Controls: WASD for the left player and IJKL for the right player
To-do list
----------
- [ ] Gameplay
- [x] Collision algorithm
- [x] Primitive animations
- [ ] Orientation indicator
- [ ] Unique textures
- [ ] Cosmetics
- [ ] A real menu interface
- [ ] In-game editor
Credits
-------
Original game made during MinneHack 2019: https://github.com/iptq/planar
- Yeshi Cai
- Mark Pekala
- Alex Shi
- Michael Zhang
Rewrite: Michael Zhang
License: MIT

4
rustfmt.toml Normal file
View file

@ -0,0 +1,4 @@
max_width = 80
tab_spaces = 2
wrap_comments = true
fn_single_line = true

Binary file not shown.

Before

Width:  |  Height:  |  Size: 51 KiB

116
src/animations.rs Normal file
View file

@ -0,0 +1,116 @@
use std::collections::{HashMap, HashSet};
use std::time::Duration;
use crate::game_state::level::{ChangeSet, Entity, FailSet};
use crate::game_state::Board;
pub type MoveResult = Result<ChangeSet, FailSet>;
pub type BlockOffsets = HashMap<Entity, (f32, f32)>;
// TODO: don't yeet around a HashMap all the time
pub type AnimationFn =
Box<dyn Fn(MoveResult, BlockOffsets, f32) -> BlockOffsets>;
// in seconds
const ANIMATION_DURATION: f32 = 1.0 / 6.0;
#[derive(Default)]
pub struct AnimationState {
pub is_animating: bool,
pub last_move_result: Option<MoveResult>,
pub progress: f32,
pub block_offsets: BlockOffsets,
pub immovable_blocks: HashSet<usize>,
progress_function: Option<AnimationFn>,
}
impl AnimationState {
pub fn new() -> Self {
AnimationState {
is_animating: false,
last_move_result: None,
progress: 0.0,
block_offsets: BlockOffsets::new(),
immovable_blocks: HashSet::new(),
progress_function: None,
}
}
pub fn begin_move_transition(&mut self, result: MoveResult) {
println!("result: {:?}", result);
self.last_move_result = Some(result);
self.is_animating = true;
self.progress = 0.0;
let func = |last_move_result: MoveResult,
mut offsets: BlockOffsets,
progress: f32| {
use std::f32::consts::PI;
match last_move_result {
// transition
Ok(change_set) => {
for (entity, direction) in change_set {
// TODO: implement ease-out?
let pair = direction.as_pair();
// cap progress at 1.0, we don't want blocks going past where they're supposed to
let progress = progress.min(1.0);
let offset = (pair.0 as f32 * progress, pair.1 as f32 * progress);
offsets.insert(entity, offset);
}
}
// vibrate all blocking pieces
Err(fail_set) => {
for index in fail_set {
let delta = 0.05 * (4.0 * PI * progress).sin() / (progress + 0.5);
offsets.insert(Entity::Block(index), (delta, delta));
}
}
}
offsets
};
self.progress_function = Some(Box::new(func));
}
pub fn make_progress(&mut self, delta: Duration) {
let progress =
self.progress + (delta.as_millis() as f32 / ANIMATION_DURATION) / 1000.0;
let block_offsets = if let Some(f) = &self.progress_function {
Some(f(
self.last_move_result.clone().unwrap(),
self.block_offsets.clone(),
progress,
))
} else {
None
};
// this should always work
if let Some(block_offsets) = block_offsets {
self.block_offsets = block_offsets;
self.progress = progress;
}
if self.progress > 1.0 {
self.is_animating = false;
self.block_offsets = BlockOffsets::new();
}
}
pub fn get_block_offset(&self, index: usize) -> (f32, f32) {
self
.block_offsets
.get(&Entity::Block(index))
.cloned()
.unwrap_or_else(|| (0.0, 0.0))
}
pub fn get_player_offset(&self, board: Board) -> (f32, f32) {
self
.block_offsets
.get(&Entity::Player(board))
.cloned()
.unwrap_or_else(|| (0.0, 0.0))
}
}

1
src/draw.rs Normal file
View file

@ -0,0 +1 @@
pub fn draw() {}

35
src/game_state/block.rs Normal file
View file

@ -0,0 +1,35 @@
use super::{BlockData, Color, Orientation, SegmentState};
#[derive(Clone, Debug)]
pub struct BlockState {
pub index: usize,
pub movable: bool,
pub color: Color,
pub orientation: Orientation,
pub segments: Vec<SegmentState>,
}
impl BlockState {
pub fn from_data(index: usize, data: &BlockData) -> Self {
let movable = data.movable;
let segments = data
.segments
.iter()
.map(|segment| SegmentState {
position: (segment[0], segment[1]),
shape: segment[2].into(),
board: segment[3].into(),
})
.collect();
let orientation = data.orientation.into();
let color = Color(data.color.0, data.color.1, data.color.2);
BlockState {
index,
movable,
color,
segments,
orientation,
}
}
}

48
src/game_state/level.rs Normal file
View file

@ -0,0 +1,48 @@
use std::collections::{HashMap, HashSet};
use super::{Board, LevelState, PushDir, Shape};
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub enum Entity {
Block(usize),
Player(Board),
}
pub type ChangeSet = HashMap<Entity, PushDir>;
pub type FailSet = HashSet<usize>;
impl LevelState {
pub fn apply_change_set(&mut self, change_set: ChangeSet) {
for (entity, direction) in change_set {
let direction = direction.as_pair();
match entity {
Entity::Player(board) => {
let player = match board {
Board::Left => &mut self.player1_position,
Board::Right => &mut self.player2_position,
};
player.0 += direction.0;
player.1 += direction.1;
}
Entity::Block(index) => {
let block = self.blocks.get_mut(index).expect("big failure");
for segment in &mut block.segments {
segment.position.0 += direction.0;
segment.position.1 += direction.1;
}
}
}
}
}
pub fn try_move(
&mut self,
board: Board,
direction: PushDir,
) -> Result<ChangeSet, FailSet> {
let mut change_set = ChangeSet::default();
change_set.insert(Entity::Player(board), direction);
self.player_can_move(board, direction, change_set)
}
}

197
src/game_state/mod.rs Normal file
View file

@ -0,0 +1,197 @@
pub mod block;
pub mod level;
pub mod move_rules;
pub mod render;
use std::ops::Add;
use anyhow::Result;
use macroquad::color::Color as MQColor;
use self::block::BlockState;
#[derive(Debug)]
pub struct LevelState {
pub data: LevelData,
pub blocks: Vec<BlockState>,
pub player1_position: (i32, i32),
pub player2_position: (i32, i32),
}
#[derive(Copy, Clone, Debug, PartialOrd, PartialEq)]
pub struct SegmentState {
position: (i32, i32),
shape: Shape,
board: Board,
}
impl LevelState {
pub fn new(data: LevelData) -> Self {
let blocks = data
.blocks
.iter()
.enumerate()
.map(|(i, block)| BlockState::from_data(i, block))
.collect();
LevelState {
blocks,
player1_position: data.player1.position,
player2_position: data.player2.position,
data,
}
}
pub fn check_win_condition(&self) -> bool {
self.player1_position == self.data.goal1
&& self.player2_position == self.data.goal2
}
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct Color(u8, u8, u8);
impl Into<MQColor> for Color {
fn into(self) -> MQColor {
MQColor::from_rgba(self.0, self.1, self.2, 255)
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct PlayerData {
pub position: (i32, i32),
pub color: Color,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct BlockData {
pub movable: bool,
pub orientation: u32,
pub color: Color,
pub segments: Vec<[i32; 4]>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct LevelData {
pub dimensions: [u32; 2],
pub player1: PlayerData,
pub player2: PlayerData,
pub goal1: (i32, i32),
pub goal2: (i32, i32),
pub blocks: Vec<BlockData>,
}
impl LevelData {
pub fn load_from_string(string: &str) -> Result<Self> {
json5::from_str(string).map_err(|err| err.into())
}
}
#[derive(Debug, Eq, PartialEq, Hash, PartialOrd, Copy, Clone)]
pub enum Board {
Left = 0,
Right = 1,
}
impl From<i32> for Board {
fn from(n: i32) -> Self {
match n {
0 => Board::Left,
1 => Board::Right,
_ => panic!("expecting 0 or 1, got {}", n),
}
}
}
#[derive(Copy, Clone, Debug)]
pub enum Orientation {
None = 0,
Horizontal = 1,
Vertical = 2,
Both = 3,
}
impl From<u32> for Orientation {
fn from(n: u32) -> Self {
match n {
0 => Orientation::Both,
1 => Orientation::Horizontal,
2 => Orientation::Vertical,
_ => panic!("expecting 0..2, got {}", n),
}
}
}
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub enum PushDir {
Up,
Down,
Left,
Right,
}
impl PushDir {
pub fn as_pair(self) -> (i32, i32) {
match self {
PushDir::Up => (0, -1),
PushDir::Down => (0, 1),
PushDir::Left => (-1, 0),
PushDir::Right => (1, 0),
}
}
}
impl Add<PushDir> for (i32, i32, Board) {
type Output = (i32, i32, Board);
fn add(self, rhs: PushDir) -> Self::Output {
let offset = rhs.as_pair();
(self.0 + offset.0, self.1 + offset.1, self.2)
}
}
// /\
// /21\
// \34/
// \/
#[derive(Copy, Clone, Debug, PartialOrd, PartialEq)]
pub enum Shape {
Full = 0,
TopRight = 1,
TopLeft = 2,
BottomLeft = 3,
BottomRight = 4,
}
impl From<i32> for Shape {
fn from(n: i32) -> Self {
match n {
0 => Shape::Full,
1 => Shape::TopRight,
2 => Shape::TopLeft,
3 => Shape::BottomLeft,
4 => Shape::BottomRight,
_ => panic!("expecting 0..4, got {}", n),
}
}
}
impl Shape {
pub fn get_opposite(self) -> Option<Shape> {
use Shape::*;
match self {
TopRight => Some(BottomLeft),
BottomLeft => Some(TopRight),
TopLeft => Some(BottomRight),
BottomRight => Some(TopLeft),
Full => None,
}
}
pub fn is_opposite(self, other: Shape) -> bool {
self
.get_opposite()
.map(|shape| shape == other)
.unwrap_or_else(|| false)
}
}

View file

@ -0,0 +1,245 @@
use std::collections::HashSet;
use crate::game_state::{level::Entity, Board, Orientation, PushDir, Shape};
use super::{
level::{ChangeSet, FailSet},
LevelState, SegmentState,
};
impl LevelState {
pub fn player_can_move(
&self,
board: Board,
direction: PushDir,
change_set: ChangeSet,
) -> Result<ChangeSet, FailSet> {
let player_position = match board {
Board::Left => &self.player1_position,
Board::Right => &self.player2_position,
};
let player_segment = SegmentState {
position: *player_position,
shape: Shape::Full,
board,
};
self.segment_can_move(None, player_segment, direction, change_set)
}
fn block_can_move(
&self,
index: usize,
direction: PushDir,
mut change_set: ChangeSet,
) -> Result<ChangeSet, FailSet> {
println!("block_can_move({:?}, {:?})", index, direction);
let block = match self.blocks.get(index) {
Some(block) => block,
None => return Err(HashSet::new()),
};
// is the block even movable?
if !block.movable {
return Err(set!(index));
}
// does the direction match the orientation?
match (block.orientation, direction) {
(Orientation::Horizontal, PushDir::Left)
| (Orientation::Horizontal, PushDir::Right)
| (Orientation::Vertical, PushDir::Up)
| (Orientation::Vertical, PushDir::Down)
| (Orientation::Both, _) => (),
_ => return Err(set!(index)),
}
// TODO: change this to use &mut instead of returning a new one each time
change_set.insert(Entity::Block(index), direction);
for segment in block.segments.iter() {
match self.segment_can_move(
Some(index),
segment.clone(),
direction,
change_set.clone(),
) {
Ok(new_change_set) => change_set = new_change_set,
Err(fail_set) => return Err(fail_set),
}
}
Ok(change_set)
}
fn segment_can_move(
&self,
block_index: Option<usize>,
segment: SegmentState,
direction: PushDir,
change_set: ChangeSet,
) -> Result<ChangeSet, FailSet> {
println!(
"segment_can_move({:?}, {:?}, {:?})",
block_index, segment, direction
);
let segment_loc = (segment.position.0, segment.position.1, segment.board);
let target = segment_loc + direction;
println!(" - target: {:?}", target);
// is the target actually in the map?
if target.0 < 0
|| target.0 >= self.data.dimensions[0] as i32
|| target.1 < 0
|| target.1 >= self.data.dimensions[1] as i32
{
return Err(entity_fail!(block_index));
}
// retrieve other blocks that might be occupying this current space and the target space
let mut current_occupant = None;
let mut target_occupant = None;
for (i, block) in self.blocks.iter().enumerate() {
// skip other segments of the same block
if let Some(n) = block_index {
if n == i {
continue;
}
}
// offset from the change set
let offset = match change_set.get(&Entity::Block(i)) {
Some(direction) => direction.as_pair(),
None => (0, 0),
};
for segment in block.segments.iter() {
// don't get segments on different boards
if segment.board != segment_loc.2 {
continue;
}
let mut segment_pos = segment.position;
if segment_pos == (segment_loc.0, segment_loc.1) {
current_occupant = Some((i, segment.shape, block.orientation));
}
segment_pos.0 += offset.0;
segment_pos.1 += offset.1;
if segment_pos == (target.0, target.1) {
target_occupant =
Some((Entity::Block(i), segment.shape, block.orientation));
}
}
}
// check if the target occupant is actually a player
if let None = target_occupant {
if segment.board == Board::Left
&& self.player1_position == (target.0, target.1)
{
target_occupant =
Some((Entity::Player(Board::Left), Shape::Full, Orientation::None));
} else if segment.board == Board::Right
&& self.player2_position == (target.0, target.1)
{
target_occupant =
Some((Entity::Player(Board::Right), Shape::Full, Orientation::None));
}
}
println!(
" - occupants: current={:?} | target={:?}",
current_occupant, target_occupant
);
// handle special pushes
if let Some((other_block, other_shape, other_orientation)) =
current_occupant
{
// are both shapes triangles?
let both_triangles = match (segment.shape, other_shape) {
(Shape::Full, Shape::Full) => false,
(Shape::Full, _) => unreachable!("invalid to have triangle + full"),
(_, Shape::Full) => unreachable!("invalid to have triangle + full"),
_ => true,
};
if both_triangles {
// what directions could we be pushing the other block into?
let possible_directions = match segment.shape {
Shape::TopRight => [PushDir::Up, PushDir::Right],
Shape::TopLeft => [PushDir::Left, PushDir::Up],
Shape::BottomLeft => [PushDir::Down, PushDir::Left],
Shape::BottomRight => [PushDir::Right, PushDir::Down],
Shape::Full => unreachable!("already eliminated this possibility"),
};
println!(" - possible directions: {:?}", possible_directions);
// does the direction we're pushing appear in this list?
if possible_directions.contains(&direction) {
// the other shape goes in the other direction
let other_direction = match other_orientation {
Orientation::None => {
unreachable!("already eliminated this possibility")
}
Orientation::Vertical => [PushDir::Up, PushDir::Down],
Orientation::Horizontal => [PushDir::Left, PushDir::Right],
Orientation::Both => unimplemented!(),
};
let possible_directions =
possible_directions.iter().collect::<HashSet<_>>();
let other_direction = other_direction.iter().collect();
let mut intersected_direction =
possible_directions.intersection(&other_direction);
let new_direction = **intersected_direction.next().unwrap();
// let other_direction = {
// let mut set = possible_directions.iter().collect::<HashSet<_>>();
// set.remove(&direction);
// *set.into_iter().next().unwrap()
// };
let mut result =
self.block_can_move(other_block, new_direction, change_set);
if let Ok(ref mut change_set) = result {
change_set.insert(Entity::Block(other_block), new_direction);
}
return result;
}
}
}
// handle normal pushes
if let Some((entity, shape, _orientation)) = target_occupant {
match entity {
Entity::Player(_) => {
// TODO: assert that the board is the same
Err(fail_set!(change_set))
}
Entity::Block(index) => {
if
// if it's part of the same block it's ok to push
block_index.is_some() && block_index.unwrap() == index ||
// if the shapes are opposite, we can actually both fit into the same spot
segment.shape.is_opposite(shape)
{
Ok(change_set)
}
// if the block is already in the change set, it can't move
else if change_set.contains_key(&Entity::Block(index)) {
Err(fail_set!(change_set))
}
// if the next block can move then so can this one
else {
self.block_can_move(index, direction, change_set)
}
}
}
} else {
// coast is clear, push away!
Ok(change_set)
}
}
}

183
src/game_state/render.rs Normal file
View file

@ -0,0 +1,183 @@
use macroquad::color::Color as MQColor;
use macroquad::math::{vec2, Vec2};
use macroquad::{
shapes::draw_rectangle,
window::{screen_height, screen_width},
};
use crate::animations::AnimationState;
use super::{Board, Color, LevelState, Orientation, Shape};
impl LevelState {
pub fn render(&self, animations: &AnimationState) {
let width = screen_width();
let height = screen_height();
// board positioning calculations
let playfield_ratio = (2 * self.data.dimensions[0] + 6) as f32
/ (self.data.dimensions[1] + 4) as f32;
let screen_ratio = width / height;
let cols = self.data.dimensions[0] as i32;
let rows = self.data.dimensions[1] as i32;
let (scale, xoff, yoff) = if playfield_ratio > screen_ratio {
let scale = width as f32 / (2 * cols + 6) as f32;
let yoff = height as f32 / 2.0 - (rows + 4) as f32 * scale / 2.0;
(scale, 0.0, yoff)
} else {
let scale = height as f32 / (rows + 4) as f32;
let xoff = width as f32 / 2.0 - (2 * cols + 6) as f32 * scale / 2.0;
(scale, xoff, 0.0)
};
let left_offset = vec2(xoff, yoff);
self.render_boards(scale, left_offset, animations);
}
fn render_boards(
&self,
scale: f32,
offset: Vec2,
animations: &AnimationState,
) {
let left_off = (offset.0 + 2 * scale, offset.1 + 2 * scale);
let right_off = (
offset.0 + (4 + self.data.dimensions[0] as i32) * scale,
offset.1 + 2 * scale,
);
// render the grid
// TODO: do this in one single pass instead of once for each cell
for x in 0..self.data.dimensions[0] as i32 {
for y in 0..self.data.dimensions[1] as i32 {
self
.render_cell((left_off.0 + x * scale, left_off.1 + y * scale), scale);
self.render_cell(
(right_off.0 + x * scale, right_off.1 + y * scale),
scale,
);
}
}
// render blocks
for (i, block) in self.blocks.iter().enumerate() {
for segment in block.segments.iter() {
let offset = match &segment.board {
Board::Left => left_off,
Board::Right => right_off,
};
let mut location = (
offset.0 + segment.position.0 * scale,
offset.1 + segment.position.1 * scale,
);
let animation_offset = animations.get_block_offset(i);
location.0 += (animation_offset.0 * scale as f32) as i32;
location.1 += (animation_offset.1 * scale as f32) as i32;
self.render_segment(
location,
scale,
block.color,
block.orientation,
segment.shape,
);
}
}
// render goals
self.render_goal(self.data.goal1, scale, left_off);
self.render_goal(self.data.goal2, scale, right_off);
// render player
self.render_player(
Board::Left,
self.player1_position,
self.data.player1.color,
scale,
animations,
left_off,
);
self.render_player(
Board::Right,
self.player2_position,
self.data.player1.color,
scale,
animations,
right_off,
);
}
fn render_player(
&self,
board: Board,
player_position: (i32, i32),
player_color: Color,
scale: i32,
animations: &AnimationState,
offset: (i32, i32),
) {
let mut location = (
offset.0 + player_position.0 * scale + 4,
offset.1 + player_position.1 * scale + 4,
);
let animation_offset = animations.get_player_offset(board);
location.0 += (animation_offset.0 * scale as f32) as i32;
location.1 += (animation_offset.1 * scale as f32) as i32;
self.render_segment(
location,
scale - 8,
player_color,
Orientation::Both,
Shape::Full,
);
}
fn render_goal(&self, location: (i32, i32), scale: f32, offset: (i32, i32)) {
let position = (
offset.0 + location.0 * scale + 4,
offset.1 + location.1 * scale + 4,
);
self.render_segment(
position,
scale - 8,
Color(102, 204, 102),
Orientation::Both,
Shape::Full,
);
}
fn render_cell(&self, location: (i32, i32), scale: f32) {
let new_x = location.0 * scale;
let new_y = location.1 * scale;
let color = MQColor::from_rgba(153, 153, 153, 255);
draw_rectangle(
new_x as f32,
new_y as f32,
scale as f32,
scale as f32,
color.into(),
)
}
fn render_segment(
&self,
location: (i32, i32),
scale: i32,
color: Color,
orientation: Orientation,
shape: Shape,
) {
// draw_circle(
// location.0 as f32,
// location.1 as f32,
// scale as f32 * 0.8,
// color.into(),
// );
}
}

30
src/macros.rs Normal file
View file

@ -0,0 +1,30 @@
macro_rules! set {
($($item:expr)*) => {
{
let mut set = std::collections::HashSet::new();
$(set.insert($item);)*
set
}
}
}
macro_rules! fail_set {
($change_set:expr) => {
$change_set
.iter()
.filter_map(|(entity, _)| match entity {
Entity::Block(index) => Some(*index),
Entity::Player(_) => None,
})
.collect()
};
}
macro_rules! entity_fail {
($item:expr) => {
match $item {
Some(index) => set!(index),
None => std::collections::HashSet::new(),
}
};
}

46
src/main.rs Normal file
View file

@ -0,0 +1,46 @@
#[macro_use]
extern crate serde;
#[macro_use]
pub mod macros;
pub mod animations;
pub mod draw;
pub mod game_state;
pub mod screens;
use anyhow::Result;
use macroquad::{
prelude::*,
ui::{hash, root_ui, widgets::Window},
};
use screens::{Screen, ScreenAction};
use crate::screens::{MenuScreen, ScreenStack};
#[macroquad::main("BasicShapes")]
async fn main() -> Result<()> {
let mut screen_stack = ScreenStack::with(MenuScreen::new());
loop {
clear_background(WHITE);
Window::new(hash!(), vec2(20., 20.), vec2(120., 120.))
.titlebar(true)
.label("level control")
.ui(&mut root_ui(), |ui| {
ui.button(vec2(0., 0.), "helloge");
let top = screen_stack.top();
let top = top.as_ref();
ui.label(vec2(0.0, 10.0), &format!("screen: {}", top.status()));
});
screen_stack.render();
screen_stack.update();
next_frame().await
}
Ok(())
}

27
src/screens/menu.rs Normal file
View file

@ -0,0 +1,27 @@
use macroquad::{input::is_key_pressed, miniquad::KeyCode};
// use crate::keymap::Keymap;
use crate::screens::{PlayScreen, Screen, ScreenAction};
pub struct MenuScreen;
impl Screen for MenuScreen {
fn status(&self) -> String {
format!("menu screen")
}
fn update(&mut self) -> ScreenAction {
if is_key_pressed(KeyCode::Space) {
let play_screen = PlayScreen::new();
ScreenAction::Push(Box::new(play_screen))
} else {
ScreenAction::None
}
}
}
impl MenuScreen {
pub fn new() -> MenuScreen {
MenuScreen
}
}

59
src/screens/mod.rs Normal file
View file

@ -0,0 +1,59 @@
mod menu;
mod play;
pub use self::menu::MenuScreen;
pub use self::play::PlayScreen;
pub trait Screen {
fn status(&self) -> String;
fn update(&mut self) -> ScreenAction {
ScreenAction::None
}
fn render(&self) {}
}
pub enum ScreenAction {
None,
Push(Box<dyn Screen>),
}
pub struct ScreenStack(Vec<Box<dyn Screen>>);
impl ScreenStack {
pub fn with<S: Screen + 'static>(screen: S) -> Self {
let mut stack = Vec::<Box<dyn Screen>>::new();
stack.push(Box::new(screen));
ScreenStack(stack)
}
pub fn top(&self) -> impl AsRef<dyn Screen + 'static> + '_ {
self.0.last().unwrap()
}
pub fn top_mut(&mut self) -> impl AsMut<dyn Screen + 'static> + '_ {
self.0.last_mut().unwrap()
}
pub fn update(&mut self) {
let result = {
let mut screen = self.top_mut();
let screen = screen.as_mut();
screen.update()
};
match result {
ScreenAction::None => (),
ScreenAction::Push(new_screen) => {
println!("pushed new screen");
self.0.push(new_screen);
}
}
}
pub fn render(&self) {
let screen = self.top();
let screen = screen.as_ref();
screen.render()
}
}

136
src/screens/play.rs Normal file
View file

@ -0,0 +1,136 @@
use std::time::Duration;
use macroquad::{
input::is_key_pressed, miniquad::KeyCode, time::get_frame_time,
};
use crate::{
animations::AnimationState,
game_state::{Board, LevelData, LevelState, PushDir},
screens::{Screen, ScreenAction},
};
const LEVEL_TUTORIAL: &str = include_str!("../../levels/tutorial.json");
const LEVEL_TUTORIAL2: &str = include_str!("../../levels/tutorial2.json");
const LEVEL_1: &str = include_str!("../../levels/level1.json");
pub struct PlayScreen {
animations: AnimationState,
levels: Vec<&'static str>,
current_level: LevelState,
current_level_idx: usize,
}
impl Screen for PlayScreen {
fn status(&self) -> String {
format!("play screen")
}
fn update(&mut self) -> ScreenAction {
macro_rules! btn_handler {
($key:expr, $board:expr, $direction:expr) => {
if is_key_pressed($key) {
println!("pushed: {:?}", $key);
let level = self.get_current_level_mut();
let result = level.try_move($board, $direction);
self.animations.begin_move_transition(result);
}
};
}
if self.animations.is_animating {
let delta = Duration::from_secs_f32(get_frame_time());
self.animations.make_progress(delta);
// we just finished!
if !self.animations.is_animating {
// apply the changes to the entities
// this indirection is used to dodge a concurrent borrow
let change_set =
if let Some(Ok(change_set)) = &self.animations.last_move_result {
Some(change_set.clone())
} else {
None
};
if let Some(change_set) = change_set {
let level = self.get_current_level_mut();
level.apply_change_set(change_set.clone());
self.check_win_condition();
}
}
} else {
btn_handler!(KeyCode::W, Board::Left, PushDir::Up);
btn_handler!(KeyCode::A, Board::Left, PushDir::Left);
btn_handler!(KeyCode::S, Board::Left, PushDir::Down);
btn_handler!(KeyCode::D, Board::Left, PushDir::Right);
btn_handler!(KeyCode::I, Board::Right, PushDir::Up);
btn_handler!(KeyCode::J, Board::Right, PushDir::Left);
btn_handler!(KeyCode::K, Board::Right, PushDir::Down);
btn_handler!(KeyCode::L, Board::Right, PushDir::Right);
if is_key_pressed(KeyCode::R) {
// restart the level
self.restart_level();
}
}
ScreenAction::None
}
fn render(&self) {
let level = self.get_current_level();
level.render(&self.animations);
}
}
impl PlayScreen {
pub fn get_current_level(&self) -> &LevelState {
&self.current_level
}
pub fn get_current_level_mut(&mut self) -> &mut LevelState {
&mut self.current_level
}
pub fn new() -> PlayScreen {
let levels = vec![LEVEL_TUTORIAL, LEVEL_TUTORIAL2, LEVEL_1];
PlayScreen {
levels,
current_level: LevelState::new(
LevelData::load_from_string(&LEVEL_TUTORIAL).unwrap(),
),
current_level_idx: 0,
animations: AnimationState::new(),
}
}
fn restart_level(&mut self) {
self.switch_to_level(self.current_level_idx);
}
fn switch_to_level(&mut self, idx: usize) {
self.current_level =
LevelState::new(LevelData::load_from_string(&self.levels[idx]).unwrap());
self.current_level_idx = idx;
}
fn go_to_next_level(&mut self) {
// TODO: make an actual win screen
let next_level = if self.current_level_idx + 1 >= self.levels.len() {
0
} else {
self.current_level_idx + 1
};
self.switch_to_level(next_level);
}
fn check_win_condition(&mut self) {
let level = self.get_current_level();
if level.check_win_condition() {
// go on to the next level
self.go_to_next_level();
}
}
}

View file

@ -1,21 +0,0 @@
[package]
name = "wedge_web"
version = "0.1.0"
authors = ["Michael Zhang <iptq@protonmail.com>"]
edition = "2018"
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "0.2"
wedge_core = { path = "../core" }
[dependencies.web-sys]
version = "0.3"
features = [
"WebGlBuffer",
"WebGlProgram",
"WebGlRenderingContext",
"WebGlShader"
]

View file

@ -1,30 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>wedge</title>
</head>
<body>
<canvas id="canvas" oncontextmenu="event.preventDefault()"></canvas>
<script type="text/javascript">
var data;
var Module = {
canvas: (function() {
var canvas = document.getElementById("canvas");
canvas.addEventListener("webglcontextlost", e => {
alert("WebGL context lost. You will need to reload the page.");
e.preventDefault();
}, false);
return canvas;
})()
};
fetch("/pkg/wedge_web_bg.wasm")
.then(response => response.arrayBuffer())
.then(buffer => WebAssembly.instantiate(buffer, {}))
.then(result => {
console.log(result);
});
</script>
</body>
</html>

View file

@ -1,8 +0,0 @@
mod platform;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(start)]
pub fn start() -> Result<(), JsValue> {
Ok(())
}

View file

@ -1,74 +0,0 @@
// https://rustwasm.github.io/wasm-bindgen/examples/webgl.html
use web_sys::{WebGlProgram, WebGlRenderingContext, WebGlShader};
use wedge_core::Platform;
pub struct WebPlatform {}
impl WebPlatform {
pub fn new() -> Self {
WebPlatform {}
}
}
impl Platform for WebPlatform {
// TODO: don't use strings lol
type GlslCompileContext = WebGlRenderingContext;
type GlslProgram = WebGlProgram;
type GlslError = String;
fn compile_glsl_program<'a>(
&self,
context: &'a Self::GlslCompileContext,
vert: impl AsRef<str>,
frag: impl AsRef<str>,
) -> Result<Self::GlslProgram, Self::GlslError> {
let program = context
.create_program()
.ok_or_else(|| String::from("Unable to create shader object"))?;
let vert = vert.as_ref();
let frag = frag.as_ref();
let vert_shader = compile_shader(context, WebGlRenderingContext::VERTEX_SHADER, vert)?;
let frag_shader = compile_shader(context, WebGlRenderingContext::FRAGMENT_SHADER, frag)?;
context.attach_shader(&program, &vert_shader);
context.attach_shader(&program, &frag_shader);
context.link_program(&program);
if context
.get_program_parameter(&program, WebGlRenderingContext::LINK_STATUS)
.as_bool()
.unwrap_or(false)
{
Ok(program)
} else {
Err(context
.get_program_info_log(&program)
.unwrap_or_else(|| String::from("Unknown error creating program object")))
}
}
}
fn compile_shader(
context: &WebGlRenderingContext,
shader_type: u32,
source: &str,
) -> Result<WebGlShader, String> {
let shader = context
.create_shader(shader_type)
.ok_or_else(|| String::from("Unable to create shader object"))?;
context.shader_source(&shader, source);
context.compile_shader(&shader);
if context
.get_shader_parameter(&shader, WebGlRenderingContext::COMPILE_STATUS)
.as_bool()
.unwrap_or(false)
{
Ok(shader)
} else {
Err(context
.get_shader_info_log(&shader)
.unwrap_or_else(|| String::from("Unknown error creating shader")))
}
}