|
|
|
@ -5,19 +5,24 @@ fn main() {
|
|
|
|
.add_plugins(BaseGamePlugin {
|
|
|
|
.add_plugins(BaseGamePlugin {
|
|
|
|
name: "falling-block-rpg".into(),
|
|
|
|
name: "falling-block-rpg".into(),
|
|
|
|
target_resolution: (640.0, 480.0).into(),
|
|
|
|
target_resolution: (640.0, 480.0).into(),
|
|
|
|
|
|
|
|
game_type: GameType::Two,
|
|
|
|
..default()
|
|
|
|
..default()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.add_systems(Startup, init_pieces)
|
|
|
|
.add_systems(Startup, init_pieces)
|
|
|
|
.add_systems(Update, (kb_movement.run_if(on_event::<KeyboardInput>), update_position))
|
|
|
|
.add_systems(Update, (kb_movement.run_if(on_event::<KeyboardInput>), update_position))
|
|
|
|
|
|
|
|
.add_systems(Update, draw_grid)
|
|
|
|
|
|
|
|
.add_systems(Update, zoom_camera)
|
|
|
|
.run();
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const SCALE: f32 = 30.0;
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Component, Default, Debug, Clone, Copy)]
|
|
|
|
#[derive(Component, Default, Debug, Clone, Copy)]
|
|
|
|
struct GridPosition { x: isize, y: isize }
|
|
|
|
struct GridPosition { x: isize, y: isize }
|
|
|
|
|
|
|
|
|
|
|
|
impl From<&GridPosition> for Vec3 {
|
|
|
|
impl From<&GridPosition> for Vec3 {
|
|
|
|
fn from(GridPosition { x, y }: &GridPosition) -> Vec3 {
|
|
|
|
fn from(GridPosition { x, y }: &GridPosition) -> Vec3 {
|
|
|
|
Vec3::new((*x as f32) * 1.0, (*y as f32) * 1.0, -10.0)
|
|
|
|
Vec3::new((*x as f32) * SCALE, (*y as f32) * SCALE, 0.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -38,11 +43,14 @@ impl std::ops::Add for GridPosition {
|
|
|
|
fn init_pieces(
|
|
|
|
fn init_pieces(
|
|
|
|
mut commands: Commands,
|
|
|
|
mut commands: Commands,
|
|
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
|
|
mut materials: ResMut<Assets<ColorMaterial>>,
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
commands.spawn((
|
|
|
|
commands.spawn((
|
|
|
|
Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0))),
|
|
|
|
Mesh2d(meshes.add(Rectangle::new(SCALE, SCALE))),
|
|
|
|
MeshMaterial3d(materials.add(Color::srgb_u8(124, 144, 255))),
|
|
|
|
MeshMaterial2d(materials.add(ColorMaterial {
|
|
|
|
|
|
|
|
color: WHITE.into(),
|
|
|
|
|
|
|
|
..default()
|
|
|
|
|
|
|
|
})),
|
|
|
|
Transform::default(),
|
|
|
|
Transform::default(),
|
|
|
|
GridPosition::default(),
|
|
|
|
GridPosition::default(),
|
|
|
|
));
|
|
|
|
));
|
|
|
|
@ -84,3 +92,27 @@ fn kb_movement(mut events: EventReader<KeyboardInput>, mut query: Query<&mut Gri
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn draw_grid(
|
|
|
|
|
|
|
|
mut gizmos: Gizmos,
|
|
|
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
gizmos.grid_2d(
|
|
|
|
|
|
|
|
Isometry2d::IDENTITY,
|
|
|
|
|
|
|
|
UVec2::new(10, 20),
|
|
|
|
|
|
|
|
Vec2::new(SCALE, SCALE),
|
|
|
|
|
|
|
|
GREEN
|
|
|
|
|
|
|
|
).outer_edges();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn zoom_camera(
|
|
|
|
|
|
|
|
mut events: EventReader<MouseWheel>,
|
|
|
|
|
|
|
|
mut query: Single<&mut Transform, With<Camera>>,
|
|
|
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
events.read().for_each(|MouseWheel { unit, y, .. }| {
|
|
|
|
|
|
|
|
let movement = match unit {
|
|
|
|
|
|
|
|
MouseScrollUnit::Line => y * 1.0,
|
|
|
|
|
|
|
|
MouseScrollUnit::Pixel => y * 1.0,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
query.translation.z += movement;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|