|
|
|
|
@ -3,8 +3,84 @@ use games::*;
|
|
|
|
|
fn main() {
|
|
|
|
|
App::new()
|
|
|
|
|
.add_plugins(BaseGamePlugin {
|
|
|
|
|
name: "tetris-rpg".into(),
|
|
|
|
|
name: "falling-block-rpg".into(),
|
|
|
|
|
target_resolution: (640.0, 480.0).into(),
|
|
|
|
|
..default()
|
|
|
|
|
})
|
|
|
|
|
.add_systems(Startup, init_pieces)
|
|
|
|
|
.add_systems(Update, (kb_movement.run_if(on_event::<KeyboardInput>), update_position))
|
|
|
|
|
.run();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Component, Default, Debug, Clone, Copy)]
|
|
|
|
|
struct GridPosition { x: isize, y: isize }
|
|
|
|
|
|
|
|
|
|
impl From<&GridPosition> for Vec3 {
|
|
|
|
|
fn from(GridPosition { x, y }: &GridPosition) -> Vec3 {
|
|
|
|
|
Vec3::new((*x as f32) * 1.0, (*y as f32) * 1.0, -10.0)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<(isize, isize)> for GridPosition {
|
|
|
|
|
fn from((x, y): (isize, isize)) -> GridPosition {
|
|
|
|
|
GridPosition { x, y }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl std::ops::Add for GridPosition {
|
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
|
|
fn add(self, GridPosition { x: x2, y: y2 }: Self) -> Self {
|
|
|
|
|
GridPosition { x: self.x + x2, y: self.y + y2 }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn init_pieces(
|
|
|
|
|
mut commands: Commands,
|
|
|
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
|
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
|
|
|
) {
|
|
|
|
|
commands.spawn((
|
|
|
|
|
Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0))),
|
|
|
|
|
MeshMaterial3d(materials.add(Color::srgb_u8(124, 144, 255))),
|
|
|
|
|
Transform::default(),
|
|
|
|
|
GridPosition::default(),
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn update_position(
|
|
|
|
|
mut query: Query<(&GridPosition, &mut Transform), Changed<GridPosition>>,
|
|
|
|
|
) {
|
|
|
|
|
query.iter_mut().for_each(|(gp, mut t)| {
|
|
|
|
|
let tmp: Vec3 = gp.into();
|
|
|
|
|
info!("Updating position {:?}", tmp);
|
|
|
|
|
|
|
|
|
|
t.translation = gp.into();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn kb_movement(mut events: EventReader<KeyboardInput>, mut query: Query<&mut GridPosition>) {
|
|
|
|
|
events.read().for_each(|KeyboardInput { key_code, state, .. }| {
|
|
|
|
|
if let ButtonState::Pressed = state {
|
|
|
|
|
let diff: GridPosition = match key_code {
|
|
|
|
|
KeyCode::ArrowUp => {
|
|
|
|
|
(0, 1)
|
|
|
|
|
},
|
|
|
|
|
KeyCode::ArrowDown => {
|
|
|
|
|
(0, -1)
|
|
|
|
|
},
|
|
|
|
|
KeyCode::ArrowLeft => {
|
|
|
|
|
(-1, 0)
|
|
|
|
|
},
|
|
|
|
|
KeyCode::ArrowRight => {
|
|
|
|
|
(1, 0)
|
|
|
|
|
},
|
|
|
|
|
_ => (0, 0)
|
|
|
|
|
}.into();
|
|
|
|
|
query.iter_mut().for_each(|mut gp| {
|
|
|
|
|
info!("Moving by {:?}", diff);
|
|
|
|
|
*gp = *gp + diff;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|