Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienjuif committed Dec 9, 2023
1 parent 71d6bff commit c57d937
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 34 deletions.
41 changes: 26 additions & 15 deletions src/minions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const DESTROY_MINIONS_AFTER_SECS: f32 = 120.;
const DECAY_VALUE_PER_SEC: f32 = 10.;
const REWARDS_GOLD: f32 = 1.;

const EXPLOSION_AUDIO_ID: &str = "sounds/explosion.ogg";

pub struct MinionsPlugin;

// TODO: move this into common
Expand All @@ -25,21 +27,30 @@ struct Minion {
had_exploded: bool,
}

#[derive(Resource)]
struct AudioExplosion(Handle<AudioSource>);

impl Plugin for MinionsPlugin {
fn build(&self, app: &mut App) {
app.add_systems(
Update,
(
update_move_minions,
check_collisions_minions,
decay_life,
explosion_damage,
),
)
.add_systems(PostUpdate, (destroy_minions, destroy_after_timer));
app.add_systems(Startup, setup_audio)
.add_systems(
Update,
(
update_move_minions,
check_collisions_minions,
decay_life,
explosion_damage,
),
)
.add_systems(PostUpdate, (destroy_minions, destroy_after_timer));
}
}

fn setup_audio(mut commands: Commands, server: Res<AssetServer>) {
let handle = server.load(EXPLOSION_AUDIO_ID);
commands.insert_resource(AudioExplosion(handle));
}

#[derive(Bundle)]
pub struct MinionBundle {
minion: Minion,
Expand Down Expand Up @@ -118,7 +129,7 @@ impl ExplosionBundle {
pub fn new(
meshes: &mut ResMut<Assets<Mesh>>,
materials: &mut ResMut<Assets<ColorMaterial>>,
asset_server: &Res<AssetServer>,
audio_asset: &Handle<AudioSource>,
mut translation: Vec3,
team: Team,
) -> Self {
Expand All @@ -141,7 +152,7 @@ impl ExplosionBundle {
timer: Timer::from_seconds(0.2, bevy::time::TimerMode::Once),
},
audio: AudioBundle {
source: asset_server.load("sounds/explosion.ogg"),
source: audio_asset.clone(),
settings: PlaybackSettings::ONCE.with_spatial(true),
},
}
Expand Down Expand Up @@ -245,7 +256,7 @@ fn check_collisions_minions(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
asset_server: Res<AssetServer>,
audio_explosion: Res<AudioExplosion>,
mut collision_events: EventReader<CollisionEvent>,
// queries
mut query_minions: Query<(&Transform, &Team, &mut Minion), With<Minion>>,
Expand Down Expand Up @@ -275,7 +286,7 @@ fn check_collisions_minions(
commands.spawn(ExplosionBundle::new(
&mut meshes,
&mut materials,
&asset_server,
&audio_explosion.0,
transform_a.translation,
team_a.clone(),
));
Expand Down Expand Up @@ -313,7 +324,7 @@ fn check_collisions_minions(
commands.spawn(ExplosionBundle::new(
&mut meshes,
&mut materials,
&asset_server,
&audio_explosion.0,
minion_transform.translation,
minion_team.clone(),
));
Expand Down
58 changes: 39 additions & 19 deletions web/index.html
Original file line number Diff line number Diff line change
@@ -1,50 +1,70 @@
<html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>An other bevy 2D game</title>
<style>
body {
background: linear-gradient(
45deg,
#4b0082 0%,
#8a2be2 100%
);
background: linear-gradient(45deg, #4b0082 0%, #8a2be2 100%);
background-repeat: no-repeat;
background-size: cover;
height: 100vh;
margin: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
color: #ffffff;
}

h1 {
font-size: 2.5em;
text-align: center;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
padding: 2em;
margin: 0; /* Remove default margin for better alignment */
}
canvas {
box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
outline: none;

button {
background-color: #ff8c00;
color: #ffffff;
font-size: 1.5em;
padding: 0.5em 1em;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s ease;
}

button:hover {
background-color: #ffa500;
}
</style>
</head>
<body>
<h1>An other bevy 2D game</h1>
<button onclick="play()">Play</button>
<script type="module">
import init from './wasm_game.js'
init()
</script>
<script>
interval = setInterval(function () {
let canvas = document.getElementsByTagName("canvas")[0]
import init from "./wasm_game.js";

window.play = function () {
// Hide header and button
document.querySelector("h1").style.display = "none";
document.querySelector("button").style.display = "none";

// Call your initialization function
init();

// Focus on the canvas
let interval = setInterval(function () {
let canvas = document.getElementsByTagName("canvas")[0];
if (canvas) {
canvas.focus()
clearInterval(interval)
canvas.focus();
clearInterval(interval);
}
}, 100)
}, 100);
};
</script>
</body>
</html>

0 comments on commit c57d937

Please sign in to comment.