Skip to content

Commit

Permalink
added a win condition
Browse files Browse the repository at this point in the history
  • Loading branch information
Pietrek14 committed Jul 8, 2023
1 parent 7b4ee5d commit 83934f0
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]
name = "bevy_game" # ToDo
name = "bevy_game" # TODO: Change the game's title
version = "0.1.0"
publish = false
authors = ["Niklas Eicker <git@nikl.me>"] # ToDo: you are the author ;)
authors = ["BigNtertainment", "Dawid Piotrowski", "Kajetan Witkowski", "Sebastian Flajszer"]
edition = "2021"
exclude = ["dist", "build", "assets", "credits"]

Expand Down
4 changes: 2 additions & 2 deletions src/ball/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct Ball {
pub struct BallBundle {
pub ball: Ball,
pub rigidbody: RigidBody,
pub ccd: Ccd,
pub active_events: ActiveEvents,
pub velocity: Velocity,
pub collider: Collider,
}
Expand All @@ -41,7 +41,7 @@ impl Default for BallBundle {
energy: MAX_BALL_ENERGY,
},
rigidbody: RigidBody::Dynamic,
ccd: Ccd::enabled(),
active_events: ActiveEvents::COLLISION_EVENTS,
velocity: Velocity {
linvel: Vec3::new(0., 0., 0.),
angvel: Vec3::new(0., 0., 0.),
Expand Down
45 changes: 45 additions & 0 deletions src/hole.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use bevy::prelude::*;
use bevy_rapier3d::prelude::*;

use crate::{ball::Ball, GameState};

pub struct HolePlugin;

impl Plugin for HolePlugin {
fn build(&self, app: &mut App) {
app.register_type::<Hole>()
.add_system(win_condition.in_set(OnUpdate(GameState::Playing)));
}
}

#[derive(Component, Reflect, Clone, Copy, Debug)]
pub struct Hole;

fn win_condition(
mut collision_events: EventReader<CollisionEvent>,
ball_query: Query<Entity, With<Ball>>,
hole_query: Query<Entity, With<Hole>>,
hole_mesh_query: Query<&Parent, With<Collider>>,
) {
if let Ok(ball) = ball_query.get_single() {
let hole = hole_query.single();

for collision in collision_events.iter() {
if let CollisionEvent::Started(entity0, entity1, _) = collision {
let other = if *entity0 == ball {
*entity1
} else if *entity1 == ball {
*entity0
} else {
continue;
};

if let Ok(parent) = hole_mesh_query.get(other) {
if parent.get() == hole {
println!("you win!!");
}
}
}
}
}
}
5 changes: 4 additions & 1 deletion src/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bevy::prelude::*;
use bevy_rapier3d::prelude::{Collider, ComputedColliderShape};
use bevy_scene_hook::{HookPlugin, HookedSceneBundle, SceneHook};

use crate::{ball::BallBundle, loading::ModelAssets, GameState};
use crate::{ball::BallBundle, hole::Hole, loading::ModelAssets, GameState};

pub struct LevelPlugin;

Expand Down Expand Up @@ -32,6 +32,9 @@ fn load_level(mut commands: Commands, models: Res<ModelAssets>) {
Some("ball") => {
commands.insert(BallBundle::default());
}
Some("hole") => {
commands.insert(Hole);
}
_ => {
let mesh = entity.get::<Handle<Mesh>>();
let parent = entity.get::<Parent>();
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod actions;
mod ball;
mod camera;
mod hole;
mod level;
mod light;
mod loading;
Expand All @@ -24,6 +25,7 @@ use bevy_rapier3d::{
render::RapierDebugRenderPlugin,
};
use camera::CameraPlugin;
use hole::HolePlugin;
use level::LevelPlugin;
use light::LightPlugin;
use loading_screen::LoadingScreenPlugin;
Expand Down Expand Up @@ -56,7 +58,8 @@ impl Plugin for GamePlugin {
.add_plugin(AudioPlugin)
.add_plugin(LightPlugin)
.add_plugin(MenuPlugin)
.add_plugin(BallPlugin);
.add_plugin(BallPlugin)
.add_plugin(HolePlugin);

#[cfg(debug_assertions)]
{
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ fn main() {
.insert_resource(ClearColor(Color::rgb(0.4, 0.4, 0.4)))
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
title: "Bevy game".to_string(), // ToDo
resolution: (800., 600.).into(),
title: "Golf game".to_string(), // TODO: Change the game's title
resolution: (1280., 720.).into(),
canvas: Some("#bevy".to_owned()),
..default()
}),
Expand Down

0 comments on commit 83934f0

Please sign in to comment.