generated from hheik/bevy-template
134 lines
2.8 KiB
Rust
134 lines
2.8 KiB
Rust
use bevy::prelude::*;
|
|
|
|
use crate::util::Vector2I;
|
|
|
|
use super::{grid::*, prefab::*};
|
|
|
|
#[derive(Component, Clone, Debug, Reflect)]
|
|
#[reflect(Component)]
|
|
#[require(Grid, NextPiece)]
|
|
pub struct GameArea {
|
|
pub bottom_boundary: i32,
|
|
pub top_boundary: i32,
|
|
pub left_boundary: i32,
|
|
pub right_boundary: i32,
|
|
}
|
|
|
|
impl Default for GameArea {
|
|
fn default() -> Self {
|
|
Self {
|
|
bottom_boundary: 0,
|
|
top_boundary: 20,
|
|
left_boundary: 0,
|
|
right_boundary: 9,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl GameArea {
|
|
pub fn block_spawn_point(&self) -> Vector2I {
|
|
Vector2I::new(
|
|
(self.left_boundary + self.right_boundary) / 2,
|
|
self.top_boundary + 2,
|
|
)
|
|
}
|
|
|
|
pub fn center(&self) -> Vec2 {
|
|
Vec2 {
|
|
x: (self.left_boundary + self.right_boundary) as f32 / 2.0,
|
|
y: (self.bottom_boundary + self.top_boundary) as f32 / 2.0,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Component, Debug, Reflect)]
|
|
#[reflect(Component)]
|
|
pub struct GameGravity {
|
|
pub current: f32,
|
|
}
|
|
|
|
impl Default for GameGravity {
|
|
fn default() -> Self {
|
|
Self { current: 1.5 }
|
|
}
|
|
}
|
|
|
|
pub mod piece_input {
|
|
use bevy::prelude::*;
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Reflect)]
|
|
pub enum Rotation {
|
|
Clockwise,
|
|
CounterClockwise,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Reflect)]
|
|
pub enum Move {
|
|
Left,
|
|
Right,
|
|
}
|
|
}
|
|
|
|
#[derive(Component, Debug, Default, Reflect)]
|
|
#[reflect(Component)]
|
|
#[require(Grid, GridTransform)]
|
|
pub struct Piece;
|
|
|
|
#[derive(Component, Debug, Default, Reflect)]
|
|
#[reflect(Component)]
|
|
#[require(Piece)]
|
|
pub struct PieceControls {
|
|
pub cumulated_gravity: f32,
|
|
pub instant_drop: bool,
|
|
pub movement: Option<piece_input::Move>,
|
|
pub rotation: Option<piece_input::Rotation>,
|
|
}
|
|
|
|
impl PieceControls {
|
|
pub fn reset_inputs(&mut self) {
|
|
self.instant_drop = false;
|
|
self.movement = None;
|
|
self.rotation = None;
|
|
}
|
|
}
|
|
|
|
#[derive(Component, Debug, Reflect)]
|
|
#[reflect(Component)]
|
|
#[require(PieceControls)]
|
|
pub struct ControllablePiece;
|
|
|
|
#[derive(Component, Clone, Debug, Reflect)]
|
|
#[reflect(Component)]
|
|
pub struct NextPiece {
|
|
pub piece: PieceType,
|
|
}
|
|
|
|
impl Default for NextPiece {
|
|
fn default() -> Self {
|
|
Self {
|
|
piece: fastrand::choice(PieceType::iter_all()).unwrap(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl NextPiece {
|
|
pub fn generate() -> PieceType {
|
|
fastrand::choice(PieceType::iter_all()).unwrap()
|
|
}
|
|
|
|
pub fn take_and_generate(&mut self) -> PieceType {
|
|
let result = self.piece;
|
|
self.piece = Self::generate();
|
|
result
|
|
}
|
|
}
|
|
|
|
#[derive(Component, Clone, Debug, Reflect)]
|
|
#[reflect(Component)]
|
|
pub struct Block;
|
|
|
|
#[derive(Event, Clone, Debug)]
|
|
pub struct OnPiecePlaced {
|
|
pub entity: Entity,
|
|
}
|