tetris: we have a block that can move

main
Elijah Voigt 3 weeks ago
parent 3edb3687cb
commit 333dae9089

@ -19,14 +19,20 @@ optimize NAME:
# Replace old bin with new (compressed) bin # Replace old bin with new (compressed) bin
mv ./dist/{{NAME}}/bin_bg-tmp.wasm ./dist/{{NAME}}/bin_bg.wasm mv ./dist/{{NAME}}/bin_bg-tmp.wasm ./dist/{{NAME}}/bin_bg.wasm
build-example EXAMPLE: build-example-dev EXAMPLE:
cargo build --example {{EXAMPLE}} --profile wasm-dev --target wasm32-unknown-unknown cargo build --example {{EXAMPLE}} --profile wasm-dev --target wasm32-unknown-unknown
build-example EXAMPLE:
cargo build --example {{EXAMPLE}} --profile release --target wasm32-unknown-unknown
build-bin GAME: build-bin GAME:
# wasm binary # wasm binary
cargo build --bin {{GAME}} --profile wasm-release --target wasm32-unknown-unknown --features hide_debug cargo build --bin {{GAME}} --profile wasm-release --target wasm32-unknown-unknown --features hide_debug
example NAME: (build-example NAME) (bindgen "wasm-dev" "examples"/NAME) example-dev NAME: (build-example-dev NAME) (bindgen "wasm-dev" "examples"/NAME)
cp web/example.html ./dist/examples/{{NAME}}/index.html
example NAME: (build-example NAME) (bindgen "release" "examples"/NAME)
cp web/example.html ./dist/examples/{{NAME}}/index.html cp web/example.html ./dist/examples/{{NAME}}/index.html
# Build the web version # Build the web version

@ -48,8 +48,6 @@ impl Plugin for BaseGamePlugin {
.add_plugins(LoadingPlugin) .add_plugins(LoadingPlugin)
.add_plugins(BaseUiPlugin) .add_plugins(BaseUiPlugin)
.add_plugins(ParallaxPlugin) .add_plugins(ParallaxPlugin)
// .add_systems(Update, scale_game.run_if(any_component_changed::<Window>))
.insert_resource(TargetResolution(self.target_resolution.clone()))
.init_resource::<Rand>(); .init_resource::<Rand>();
match self.game_type { match self.game_type {
@ -59,9 +57,6 @@ impl Plugin for BaseGamePlugin {
} }
} }
#[derive(Resource)]
struct TargetResolution(WindowResolution);
/// System to toggle the visibility of entities based on their state /// System to toggle the visibility of entities based on their state
pub fn toggle_state_visibility<S: States + Component>( pub fn toggle_state_visibility<S: States + Component>(
mut q: Query<(Entity, &mut Visibility, &S)>, mut q: Query<(Entity, &mut Visibility, &S)>,
@ -84,6 +79,7 @@ pub fn create_camera_3d(mut commands: Commands) {
Camera3d { ..default() }, Camera3d { ..default() },
Camera { ..default() }, Camera { ..default() },
AmbientLight::default(), AmbientLight::default(),
Transform::default(),
)); ));
} }
@ -95,22 +91,3 @@ pub fn create_camera_2d(mut commands: Commands) {
#[derive(Default, Resource)] #[derive(Default, Resource)]
pub struct Rand(pub RandomState); pub struct Rand(pub RandomState);
/// Scale the game based on the difference between the target and real resolution
fn scale_game(
mut window: Single<&mut Window>,
target_resolution: Res<TargetResolution>,
mut last_scale_factor: Local<f32>,
) {
let current_resolution: Vec2 = window.resolution.size();
let scale_width = current_resolution.x.round() / target_resolution.0.width().round();
let scale_height = current_resolution.y.round() / target_resolution.0.height().round();
let scale_factor = f32::min(scale_width, scale_height);
if window.resolution.scale_factor() != scale_factor {
// Need to check the previously set scale factor because the system can flip-flop otherwise
if scale_factor != *last_scale_factor {
*last_scale_factor = window.resolution.scale_factor();
window.resolution.set_scale_factor(scale_factor);
}
}
}

@ -0,0 +1,7 @@
# Falling Blocks RPG
This game is inspired by both Tetris and Peglin.
Your goal is to play Tetris (or a similar falling block game) but while that is happening you are carrying out a 2d real-time combat RPG battle.
Between battles/levels you choose a different level to go to in an overworld, choose upgrades, perks, and maybe some other stuff!

@ -3,8 +3,84 @@ use games::*;
fn main() { fn main() {
App::new() App::new()
.add_plugins(BaseGamePlugin { .add_plugins(BaseGamePlugin {
name: "tetris-rpg".into(), name: "falling-block-rpg".into(),
target_resolution: (640.0, 480.0).into(),
..default() ..default()
}) })
.add_systems(Startup, init_pieces)
.add_systems(Update, (kb_movement.run_if(on_event::<KeyboardInput>), update_position))
.run(); .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;
});
}
});
}

Loading…
Cancel
Save