Skip to content

Commit

Permalink
Ep 1 - 6: Component, System, Resource, Event (#1)
Browse files Browse the repository at this point in the history
* fix tests

* Update package name

* Add bevy-ball assets

* Update readme.md

* Remove template code

* Move assets to root

* episode 2

* Add WindowHelper

* trivial reorder

* ep3: player movement + helper

* ep3: player confinement

* Add RandomHelper

* ep3: spawn enemies

* ep4: enemy movement

* ep4: update enemy direction

* ep4: confine enemy

* ep4: play sound on enemy bounce

* ep4: player despawn when hit

* ep 5: spawn star

* ep 5: collect star

* ep 5: score, score default, and on change printing

* ep 5: star spawn timer and spawn star overtime

* ep 6: spawn enemy overtime

* ep 6: example event

* Custom Despawn component

* ep 6: custom event and passing value to an event

* ep 6: high scores - demonstrating that single event can be read by many systems
  • Loading branch information
rahmatnazali authored Jul 1, 2024
1 parent f508558 commit 737e89f
Show file tree
Hide file tree
Showing 19 changed files with 559 additions and 223 deletions.
39 changes: 38 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[package]
name = "bevy-skeleton"
name = "bevy-ball"
version = "0.1.0"
edition = "2021"

[dependencies]
bevy = "0.13.2"
rand = "0.8.5"
Binary file added assets/audio/explosionCrunch_000.ogg
Binary file not shown.
Binary file added assets/audio/laserLarge_000.ogg
Binary file not shown.
Binary file added assets/audio/pluck_001.ogg
Binary file not shown.
Binary file added assets/audio/pluck_002.ogg
Binary file not shown.
Binary file added assets/sprites/ball_blue_large.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sprites/ball_red_large.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sprites/star.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 2 additions & 44 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,2 @@
Collection of useful docs to quickly start developing with bevy using RustRover on Ubuntu.

# Setup

- Install dependencies

For ubuntu:

```
sudo apt update
sudo apt install g++ pkg-config libx11-dev libasound2-dev libudev-dev libxkbcommon-x11-0
```

> More on this: https://github.com/bevyengine/bevy/blob/main/docs/linux_dependencies.md
- Add bevy dependency to `Cargo.toml`

- Run `cargo build` and check if compilation succeed

- Enable Query support for RustRover
- `org.rust.cargo.evaluate.build.scripts`
- `org.rust.macros.proc`

> More on this: https://bevy-cheatbook.github.io/setup/editor/jetbrains.html
- Enable Reformat Code & Optimize Import on Save

Open Setting > Tools > Action on Save and check `Reformat Code` and `Optimize Import`

- Enable dynamic linking for faster compile in development
- On Run configuration, append `--features bevy/dynamic_linking` inside the command
- On Settings > Rust > External Linters, add `--features bevy/dynamic_linking` on Additional arguments

> More on this: https://bevyengine.org/learn/quick-start/getting-started/setup/#dynamic-linking
# Testing

See [test.rs](src/people/tests.rs) for examples.

> More on this: https://bevy-cheatbook.github.io/patterns/system-tests.html
# References

- [Bevy opinionated best practices](https://github.com/tbillington/bevy_best_practices)
Practicing bevy ball game coding
from [a tutorial by Jacques](https://www.youtube.com/playlist?list=PLVnntJRoP85JHGX7rGDu6LaF3fmDDbqyd).
4 changes: 4 additions & 0 deletions src/commons/components.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use bevy::prelude::Component;

#[derive(Component)]
pub struct Despawn {}
1 change: 1 addition & 0 deletions src/commons/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod components;
99 changes: 99 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
use bevy::input::ButtonInput;
use bevy::math::Vec3;
use bevy::prelude::{KeyCode, Res, Transform, Window};
use rand::random;

pub mod commons;

pub struct WindowHelper {}

impl WindowHelper {
/// Given a &Window, return its center of the screen as Transform
pub fn center(window: &Window) -> Transform {
let middle_x = window.width() / 2.0;
let middle_y = window.height() / 2.0;
Transform::from_xyz(middle_x, middle_y, 0.0)
}
}

pub struct MovementHelper {}

impl MovementHelper {
/// Given a keyboard_input, return the final normalized direction as Vec3
pub fn handle_input(keyboard_input: Res<ButtonInput<KeyCode>>) -> Vec3 {
let mut direction = Vec3::ZERO;

if keyboard_input.pressed(KeyCode::ArrowLeft) || keyboard_input.pressed(KeyCode::KeyA) {
direction += Vec3::new(-1.0, 0.0, 0.0)
}
if keyboard_input.pressed(KeyCode::ArrowRight) || keyboard_input.pressed(KeyCode::KeyD) {
direction += Vec3::new(1.0, 0.0, 0.0)
}
if keyboard_input.pressed(KeyCode::ArrowUp) || keyboard_input.pressed(KeyCode::KeyW) {
direction += Vec3::new(0.0, 1.0, 0.0)
}
if keyboard_input.pressed(KeyCode::ArrowDown) || keyboard_input.pressed(KeyCode::KeyS) {
direction += Vec3::new(0.0, -1.0, 0.0)
}

if direction.length() > 0.0 {
direction = direction.normalize();
}

return direction;
}

pub fn confine(window: &Window, unit_translation: Vec3, unit_size: f32) -> Vec3 {
let half_unit_size = unit_size / 2.0;
let x_min = 0.0 + half_unit_size;
let x_max = window.width() - half_unit_size;
let y_min = 0.0 + half_unit_size;
let y_max = window.height() - half_unit_size;

let mut new_translation = unit_translation;

// bound x
if new_translation.x < x_min {
new_translation.x = x_min;
} else if new_translation.x > x_max {
new_translation.x = x_max;
}

// bound y
if new_translation.y < y_min {
new_translation.y = y_min;
} else if new_translation.y > y_max {
new_translation.y = y_max;
}

new_translation
}
}

pub struct RandomHelper {}

impl RandomHelper {
pub fn random_f32() -> f32 {
random::<f32>()
}
}

pub struct SoundHelper {}

impl SoundHelper {
pub fn bounce_sound() -> String {
return if RandomHelper::random_f32() < 0.5 {
"audio/pluck_001.ogg".to_string()
} else {
"audio/pluck_002.ogg".to_string()
};
}

pub fn obtain_star_sound() -> String {
"audio/laserLarge_000.ogg".to_string()
}

pub fn game_over_sound() -> String {
"audio/explosionCrunch_000.ogg".to_string()
}
}
Loading

0 comments on commit 737e89f

Please sign in to comment.