Compare commits
44 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 23e4ef3093 | |||
| 235d89ebb3 | |||
| 12c303d0ab | |||
| 03b985ee57 | |||
| 16b118f37d | |||
| 172b528138 | |||
| 70de4bb67d | |||
| ebee953955 | |||
| d2cb75c3a1 | |||
| 099926d368 | |||
| 76426094d3 | |||
| 4b101633e7 | |||
| a5a6f32037 | |||
| 4d899d3c97 | |||
| 6d5afc2445 | |||
| d34d0a31f2 | |||
| 72f062ea10 | |||
| eecfbf5d7c | |||
| 1e09e3ce3a | |||
| 19ac032792 | |||
| de75e25ca6 | |||
| d7802bdbed | |||
| ae093d2c9c | |||
| 3963b548b9 | |||
| b1fd2e5f73 | |||
| 09ff4dc6ca | |||
| a48dfc1d65 | |||
| 6e425e8eb9 | |||
| 7bd88c702f | |||
| cfe35c61ed | |||
| 13643f73fb | |||
| 8fc9e682cc | |||
| be83be1a7b | |||
| 3639122e54 | |||
| 97e0313c23 | |||
| de79ca0258 | |||
| 1edbd3e78c | |||
| 5af59863a1 | |||
| 84d93d496a | |||
| 4a9d252691 | |||
| 3c9a9a7d9d | |||
| 010cbd6d4b | |||
| 918992702f | |||
| c8c64e4d22 |
@@ -18,7 +18,12 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
components: rustfmt clippy
|
components: rustfmt clippy
|
||||||
- name: Install system dependencies
|
- name: Install system dependencies
|
||||||
run: sudo apt-get update && sudo apt-get install -y --no-install-recommends libasound2-dev libudev-dev
|
run: >
|
||||||
|
sudo apt-get update &&
|
||||||
|
sudo apt-get install -y --no-install-recommends
|
||||||
|
libasound2-dev
|
||||||
|
libudev-dev
|
||||||
|
libwayland-dev
|
||||||
- name : cargo generate-lockfile
|
- name : cargo generate-lockfile
|
||||||
if: hashFiles('Cargo.lock') == ''
|
if: hashFiles('Cargo.lock') == ''
|
||||||
run: cargo generate-lockfile
|
run: cargo generate-lockfile
|
||||||
@@ -41,6 +46,6 @@ jobs:
|
|||||||
name: roberts-bad-asteroids-game
|
name: roberts-bad-asteroids-game
|
||||||
path: target/release/asteroids
|
path: target/release/asteroids
|
||||||
if-no-files-found: 'error'
|
if-no-files-found: 'error'
|
||||||
retention-days: 1
|
retention-days: 7
|
||||||
compression-level: 9
|
compression-level: 9
|
||||||
overwrite: true
|
overwrite: true
|
||||||
|
|||||||
94
.gitea/workflows/check.yml
Normal file
94
.gitea/workflows/check.yml
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
# This workflow runs whenever a PR is opened or updated, or a commit is pushed to main. It runs
|
||||||
|
# several checks:
|
||||||
|
# - fmt: checks that the code is formatted according to rustfmt
|
||||||
|
# - clippy: checks that the code does not contain any clippy warnings
|
||||||
|
# - doc: checks that the code can be documented without errors
|
||||||
|
# - hack: check combinations of feature flags
|
||||||
|
# - msrv: check that the msrv specified in the crate is correct
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
# This configuration allows maintainers of this repo to create a branch and pull request based on
|
||||||
|
# the new branch. Restricting the push trigger to the main branch ensures that the PR only gets
|
||||||
|
# built once.
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [trunk]
|
||||||
|
pull_request:
|
||||||
|
# If new code is pushed to a PR branch, then cancel in progress workflows for that PR. Ensures that
|
||||||
|
# we don't waste CI time, and returns results quicker https://github.com/jonhoo/rust-ci-conf/pull/5
|
||||||
|
concurrency:
|
||||||
|
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
|
||||||
|
cancel-in-progress: true
|
||||||
|
name: check
|
||||||
|
jobs:
|
||||||
|
fmt:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
name: stable / fmt
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
submodules: true
|
||||||
|
- name: Install stable
|
||||||
|
uses: dtolnay/rust-toolchain@stable
|
||||||
|
with:
|
||||||
|
components: rustfmt
|
||||||
|
- name: cargo fmt --check
|
||||||
|
run: cargo fmt --check
|
||||||
|
doc:
|
||||||
|
# run docs generation on nightly rather than stable. This enables features like
|
||||||
|
# https://doc.rust-lang.org/beta/unstable-book/language-features/doc-cfg.html which allows an
|
||||||
|
# API be documented as only available in some specific platforms.
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
name: nightly / doc
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
submodules: true
|
||||||
|
- name: Install Dependencies
|
||||||
|
run: sudo apt-get update; sudo apt-get install -y --no-install-recommends libasound2-dev libudev-dev libwayland-dev
|
||||||
|
- name: Install nightly
|
||||||
|
uses: dtolnay/rust-toolchain@nightly
|
||||||
|
- name: cargo doc
|
||||||
|
run: cargo doc --no-deps --all-features
|
||||||
|
env:
|
||||||
|
RUSTDOCFLAGS: --cfg docsrs
|
||||||
|
hack:
|
||||||
|
# cargo-hack checks combinations of feature flags to ensure that features are all additive
|
||||||
|
# which is required for feature unification
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
name: ubuntu / stable / features
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
submodules: true
|
||||||
|
- name: Install Dependencies
|
||||||
|
run: sudo apt-get update; sudo apt-get install -y --no-install-recommends libasound2-dev libudev-dev libwayland-dev cmake
|
||||||
|
- name: Install stable
|
||||||
|
uses: dtolnay/rust-toolchain@stable
|
||||||
|
- name: cargo install cargo-hack
|
||||||
|
uses: taiki-e/install-action@cargo-hack
|
||||||
|
# intentionally no target specifier; see https://github.com/jonhoo/rust-ci-conf/pull/4
|
||||||
|
# --feature-powerset runs for every combination of features
|
||||||
|
- name: cargo hack
|
||||||
|
run: cargo hack --feature-powerset check
|
||||||
|
msrv:
|
||||||
|
# check that we can build using the minimal rust version that is specified by this crate
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
# we use a matrix here just because env can't be used in job names
|
||||||
|
# https://docs.github.com/en/actions/learn-github-actions/contexts#context-availability
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
msrv: ["1.88.0"]
|
||||||
|
name: ubuntu / ${{ matrix.msrv }}
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
submodules: true
|
||||||
|
- name: Install Dependencies
|
||||||
|
run: sudo apt-get update; sudo apt-get install -y --no-install-recommends libasound2-dev libudev-dev libwayland-dev
|
||||||
|
- name: Install ${{ matrix.msrv }}
|
||||||
|
uses: dtolnay/rust-toolchain@master
|
||||||
|
with:
|
||||||
|
toolchain: ${{ matrix.msrv }}
|
||||||
|
- name: cargo +${{ matrix.msrv }} check
|
||||||
|
run: cargo check
|
||||||
6640
Cargo.lock
generated
Normal file
6640
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
13
Cargo.toml
13
Cargo.toml
@@ -1,14 +1,15 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "asteroids"
|
name = "asteroids"
|
||||||
version = "0.3.0"
|
version = "0.7.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
license = "AGPL-3.0-only"
|
license = "AGPL-3.0-only"
|
||||||
|
rust-version = "1.88.0"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bevy = "0.16"
|
bevy = "0.17"
|
||||||
bevy-inspector-egui = "0.32.0"
|
bevy-inspector-egui = "0.34"
|
||||||
bevy_rapier2d = "0.31.0"
|
bevy_rapier2d = "0.32"
|
||||||
rand = "0.9.2"
|
rand = "0.9"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["dynamic_linking"]
|
default = ["dynamic_linking"]
|
||||||
@@ -16,7 +17,7 @@ dynamic_linking = ["bevy/dynamic_linking"]
|
|||||||
debug_ui = ["bevy_rapier2d/debug-render-2d"]
|
debug_ui = ["bevy_rapier2d/debug-render-2d"]
|
||||||
|
|
||||||
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
||||||
getrandom = { version = "0.3.3", features = ["wasm_js"] }
|
getrandom = { version = "0.3", features = ["wasm_js"] }
|
||||||
|
|
||||||
[profile.speedy]
|
[profile.speedy]
|
||||||
inherits = "release"
|
inherits = "release"
|
||||||
|
|||||||
15
Dockerfile
15
Dockerfile
@@ -1,22 +1,11 @@
|
|||||||
FROM rust:1.89 AS builder
|
FROM rust:1.89 AS builder
|
||||||
|
|
||||||
RUN apt-get update
|
|
||||||
RUN apt-get install -y --no-install-recommends libasound2-dev libudev-dev
|
|
||||||
RUN rustup target add wasm32-unknown-unknown
|
RUN rustup target add wasm32-unknown-unknown
|
||||||
RUN cargo install --locked wasm-bindgen-cli
|
RUN cargo install --locked wasm-bindgen-cli
|
||||||
|
|
||||||
# Copy in only the parts we care about. This is to prevent Docker from re-
|
COPY . .
|
||||||
# running some steps because unimportant files changed (e.g.: the .git/ folder)
|
|
||||||
COPY src/ ./src
|
|
||||||
COPY Cargo.toml ./Cargo.toml
|
|
||||||
# WARN: The lockfile doesn't exist in the repo. You will have to create it
|
|
||||||
# before building the Docker image (i.e.: run `cargo update` first)
|
|
||||||
COPY Cargo.lock ./Cargo.lock
|
|
||||||
COPY www/ ./www
|
|
||||||
COPY Makefile ./Makefile
|
|
||||||
|
|
||||||
# Oops. There's no text output in the Docker build command line (it still works, though)
|
RUN make -j
|
||||||
RUN make
|
|
||||||
|
|
||||||
FROM busybox:musl
|
FROM busybox:musl
|
||||||
RUN mkdir -p /var/www
|
RUN mkdir -p /var/www
|
||||||
|
|||||||
59
Makefile
59
Makefile
@@ -3,22 +3,40 @@
|
|||||||
## Do not use it if that isn't your goal!
|
## Do not use it if that isn't your goal!
|
||||||
##
|
##
|
||||||
|
|
||||||
SRC_DIR = ./src
|
# # # Configuration Variables # # #
|
||||||
SRCS := $(wildcard $(SRC_DIR)/**)
|
#
|
||||||
|
|
||||||
# Patch these to select a different build profile or target
|
# Patch these to select a different build profile or target
|
||||||
# The target shouldn't change any time soon. WASM64, I guess. Other targets
|
# The target shouldn't change any time soon. WASM64, I guess. Other targets
|
||||||
# aren't aimed at the web, so you shouldn't be using this makefile.
|
# aren't aimed at the web, so you shouldn't be using this makefile.
|
||||||
CARGO_TARGET := wasm32-unknown-unknown
|
CARGO_TARGET := wasm32-unknown-unknown
|
||||||
CARGO_PROFILE := tiny
|
CARGO_PROFILE := tiny
|
||||||
|
|
||||||
.PHONY: clean full-clean web tarball
|
# # # Automatic Variables # # #
|
||||||
|
SRC_DIR = ./src
|
||||||
|
SRCS := $(wildcard $(SRC_DIR)/**)
|
||||||
|
|
||||||
web: out/asteroids.js out/asteroids_bg.wasm out/index.html
|
ASSET_SOURCE := $(wildcard assets/**)
|
||||||
|
ASSETS := $(patsubst assets/%.ogg, out/assets/%.ogg, $(ASSET_SOURCE))
|
||||||
|
|
||||||
|
CRATE_VERSION != sed -nre 's/^version = "(.*)"/\1/p' Cargo.toml
|
||||||
|
|
||||||
|
.PHONY: clean full-clean tarball tarball-standalone web web-standalone
|
||||||
|
|
||||||
|
# "Standalone" version. It includes an index.html to serve as-is
|
||||||
|
web-standalone: out/asteroids.js out/asteroids_bg.wasm.gz out/index.html $(ASSETS)
|
||||||
|
|
||||||
|
# "Bundle-able" version. It has a page, but no index.html. Consumers are
|
||||||
|
# expected to provide their own index.html and link to this page.
|
||||||
|
web: out/asteroids.js out/asteroids_bg.wasm.gz out/asteroids.html $(ASSETS)
|
||||||
|
|
||||||
tarball: asteroids_web_root.tar
|
tarball: asteroids_web_root.tar
|
||||||
|
|
||||||
asteroids_web_root.tar: out/asteroids.js out/asteroids_bg.wasm out/index.html
|
tarball_standalone: asteroids_web_root_standalone.tar
|
||||||
|
|
||||||
|
asteroids_web_root.tar: out/asteroids.js out/asteroids_bg.wasm.gz out/asteroids.html $(ASSETS)
|
||||||
|
tar -caf $@ $^
|
||||||
|
|
||||||
|
asteroids_web_root_standalone.tar: out/asteroids.js out/asteroids_bg.wasm.gz out/index.html $(ASSETS)
|
||||||
tar -caf $@ $^
|
tar -caf $@ $^
|
||||||
|
|
||||||
target/$(CARGO_TARGET)/$(CARGO_PROFILE)/asteroids.wasm: $(SRCS) Cargo.lock Cargo.toml
|
target/$(CARGO_TARGET)/$(CARGO_PROFILE)/asteroids.wasm: $(SRCS) Cargo.lock Cargo.toml
|
||||||
@@ -27,20 +45,45 @@ target/$(CARGO_TARGET)/$(CARGO_PROFILE)/asteroids.wasm: $(SRCS) Cargo.lock Cargo
|
|||||||
out:
|
out:
|
||||||
mkdir $@
|
mkdir $@
|
||||||
|
|
||||||
|
out/assets: | out
|
||||||
|
mkdir $@
|
||||||
|
|
||||||
|
out/assets/%.ogg: assets/%.ogg | out/assets
|
||||||
|
cp -ar assets/$*.ogg $@
|
||||||
|
|
||||||
# Both the JS and WASM files are generated by the wasm-bindgen call, so both
|
# Both the JS and WASM files are generated by the wasm-bindgen call, so both
|
||||||
# get to be on the target half of this recipe.
|
# get to be on the target half of this recipe.
|
||||||
out/asteroids.js out/asteroids_bg.wasm &: target/$(CARGO_TARGET)/$(CARGO_PROFILE)/asteroids.wasm | out
|
out/asteroids.js out/asteroids_bg.wasm.gz &: target/$(CARGO_TARGET)/$(CARGO_PROFILE)/asteroids.wasm | out
|
||||||
wasm-bindgen --no-typescript --target web --out-dir ./out/ --out-name asteroids target/$(CARGO_TARGET)/$(CARGO_PROFILE)/asteroids.wasm
|
wasm-bindgen --no-typescript --target web --out-dir ./out/ --out-name asteroids target/$(CARGO_TARGET)/$(CARGO_PROFILE)/asteroids.wasm
|
||||||
|
gzip -9 -f out/asteroids_bg.wasm
|
||||||
|
|
||||||
|
# Copies the index page to the output dir.
|
||||||
out/index.html: www/index.html
|
out/index.html: www/index.html
|
||||||
cp -a $< $@
|
cp -a $< $@
|
||||||
|
rm -f out/asteroids.html
|
||||||
|
sed -i -e "s/#CRATE_VERSION_PLACEHOLDER#/$(CRATE_VERSION)/" $@
|
||||||
|
|
||||||
|
# Like `out/index.html`, but renames the page for use in a larger site.
|
||||||
|
out/asteroids.html: www/index.html
|
||||||
|
cp -a $< $@
|
||||||
|
rm -f out/index.html
|
||||||
|
|
||||||
# Clean the web build, but not the Cargo cache. Cargo handles it's own caching
|
# Clean the web build, but not the Cargo cache. Cargo handles it's own caching
|
||||||
# and I don't want to obliterate it all the time.
|
# and I don't want to obliterate it all the time.
|
||||||
clean:
|
clean:
|
||||||
rm -rf out/ asteroids_web_root.tar
|
rm -rf out/ asteroids_web_root.tar asteroids_web_root_standalone.tar
|
||||||
|
|
||||||
# Delete everything, including the Cargo build cache. In case someone needs
|
# Delete everything, including the Cargo build cache. In case someone needs
|
||||||
# this, I guess.
|
# this, I guess.
|
||||||
full-clean: clean
|
full-clean: clean
|
||||||
cargo clean
|
cargo clean
|
||||||
|
|
||||||
|
# Installation goal. It's meant to be a helper utility for moving the built
|
||||||
|
# output into the web root. Only supports the "bundle-able" mode.
|
||||||
|
install: web
|
||||||
|
install -dm0755 $(DESTDIR)
|
||||||
|
install -dm0755 $(DESTDIR)/assets
|
||||||
|
install -m0644 out/asteroids.js $(DESTDIR)/
|
||||||
|
install -m0644 out/asteroids_bg.wasm.gz $(DESTDIR)/
|
||||||
|
install -m0644 out/asteroids.html $(DESTDIR)/
|
||||||
|
install -m0644 $(ASSETS) $(DESTDIR)/assets/
|
||||||
|
|||||||
14
README.md
Normal file
14
README.md
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
# Asteroids
|
||||||
|
|
||||||
|
*Another* Asteroids game I'm making. This time in Rust with the Bevy game engine.
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
| File(s) | License |
|
||||||
|
|-|-|
|
||||||
|
| * | AGPLv3 |
|
||||||
|
| assets/* | CC0 |
|
||||||
|
|
||||||
|
(the most-specific match is the applicable license)
|
||||||
|
|
||||||
|
The sound files are from KenneyNL's "Sci-Fi Sounds (1.0)" pack. Find their work at [www.kenney.nl].
|
||||||
BIN
assets/explosionCrunch_000.ogg
Normal file
BIN
assets/explosionCrunch_000.ogg
Normal file
Binary file not shown.
BIN
assets/explosionCrunch_004.ogg
Normal file
BIN
assets/explosionCrunch_004.ogg
Normal file
Binary file not shown.
BIN
assets/laserSmall_001.ogg
Normal file
BIN
assets/laserSmall_001.ogg
Normal file
Binary file not shown.
BIN
assets/thrusterFire_004.ogg
Normal file
BIN
assets/thrusterFire_004.ogg
Normal file
Binary file not shown.
@@ -3,25 +3,27 @@
|
|||||||
|
|
||||||
use bevy::color::Color;
|
use bevy::color::Color;
|
||||||
|
|
||||||
pub const WINDOW_SIZE: bevy::prelude::Vec2 = bevy::prelude::Vec2::new(800.0, 600.0);
|
pub const WINDOW_SIZE: (u32, u32) = (800, 600);
|
||||||
|
|
||||||
pub const UI_BUTTON_NORMAL: Color = Color::srgb(0.15, 0.15, 0.15); // Button color when it's just hanging out
|
pub const UI_BUTTON_NORMAL: Color = Color::srgb(0.15, 0.15, 0.15); // Button color when it's just hanging out
|
||||||
pub const UI_BUTTON_HOVERED: Color = Color::srgb(0.25, 0.25, 0.25); // ... when it's hovered
|
pub const UI_BUTTON_HOVERED: Color = Color::srgb(0.25, 0.25, 0.25); // ... when it's hovered
|
||||||
pub const UI_BUTTON_PRESSED: Color = Color::srgb(0.55, 0.55, 0.55); // ... when it's pressed
|
pub const UI_BUTTON_PRESSED: Color = Color::srgb(0.55, 0.55, 0.55); // ... when it's pressed
|
||||||
|
|
||||||
pub(crate) const BACKGROUND_COLOR: Color = Color::srgb(0.3, 0.3, 0.3);
|
pub(crate) const BACKGROUND_COLOR: Color = Color::srgb(0.1, 0.1, 0.1);
|
||||||
pub(crate) const PLAYER_SHIP_COLOR: Color = Color::srgb(1.0, 1.0, 1.0);
|
pub(crate) const PLAYER_SHIP_COLOR: Color = Color::srgb(1.0, 1.0, 1.0);
|
||||||
pub(crate) const SHIP_THRUSTER_COLOR_ACTIVE: Color = Color::srgb(1.0, 0.2, 0.2);
|
pub(crate) const SHIP_THRUSTER_COLOR_ACTIVE: Color = Color::srgb(1.0, 0.2, 0.2);
|
||||||
pub(crate) const SHIP_THRUSTER_COLOR_INACTIVE: Color = Color::srgb(0.5, 0.5, 0.5);
|
pub(crate) const SHIP_THRUSTER_COLOR_INACTIVE: Color = Color::srgb(0.5, 0.5, 0.5);
|
||||||
pub(crate) const SHIP_FIRE_RATE: f32 = 3.0; // in bullets-per-second
|
pub(crate) const SHIP_FIRE_RATE: f32 = 3.0; // in bullets-per-second
|
||||||
pub(crate) const ASTEROID_SMALL_COLOR: Color = Color::srgb(1.0, 0., 0.);
|
pub(crate) const ASTEROID_SMALL_COLOR: Color = Color::srgb(0.9, 0.9, 0.9);
|
||||||
pub(crate) const BULLET_COLOR: Color = Color::srgb(0.0, 0.1, 0.9);
|
pub(crate) const ASTEROID_MEDIUM_COLOR: Color = Color::srgb(0.8, 0.8, 0.8);
|
||||||
|
pub(crate) const ASTEROID_LARGE_COLOR: Color = Color::srgb(0.6, 0.6, 0.6);
|
||||||
|
pub(crate) const BULLET_COLOR: Color = Color::srgb(0.9, 0.9, 0.9);
|
||||||
// TODO: asteroid medium & large
|
// TODO: asteroid medium & large
|
||||||
|
|
||||||
pub(crate) const SHIP_THRUST: f32 = 1.0;
|
pub(crate) const SHIP_THRUST: f32 = 4.0;
|
||||||
pub(crate) const SHIP_ROTATION: f32 = 4.0; // +/- rotation speed in... radians per frame
|
pub(crate) const SHIP_ROTATION: f32 = 4.0; // +/- rotation speed in... radians per frame
|
||||||
|
|
||||||
pub(crate) const BULLET_SPEED: f32 = 150.0;
|
pub(crate) const BULLET_SPEED: f32 = 500.0;
|
||||||
pub(crate) const BULLET_LIFETIME: f32 = 2.0;
|
pub(crate) const BULLET_LIFETIME: f32 = 2.0;
|
||||||
|
|
||||||
pub(crate) const ASTEROID_LIFETIME: f32 = 40.0;
|
pub(crate) const ASTEROID_LIFETIME: f32 = 40.0;
|
||||||
|
|||||||
47
src/lib.rs
47
src/lib.rs
@@ -3,17 +3,17 @@
|
|||||||
//! Compile-time configurables can be found in the [`config`] module.
|
//! Compile-time configurables can be found in the [`config`] module.
|
||||||
|
|
||||||
pub mod config;
|
pub mod config;
|
||||||
mod events;
|
|
||||||
mod machinery;
|
mod machinery;
|
||||||
|
mod messages;
|
||||||
mod objects;
|
mod objects;
|
||||||
mod physics;
|
mod physics;
|
||||||
mod resources;
|
mod resources;
|
||||||
mod widgets;
|
mod widgets;
|
||||||
|
|
||||||
use crate::config::{
|
use crate::config::{
|
||||||
ASTEROID_SMALL_COLOR, BACKGROUND_COLOR, BULLET_COLOR, BULLET_LIFETIME, BULLET_SPEED,
|
ASTEROID_LARGE_COLOR, ASTEROID_MEDIUM_COLOR, ASTEROID_SMALL_COLOR, BACKGROUND_COLOR,
|
||||||
PLAYER_SHIP_COLOR, SHIP_ROTATION, SHIP_THRUST, SHIP_THRUSTER_COLOR_ACTIVE,
|
BULLET_COLOR, BULLET_LIFETIME, BULLET_SPEED, PLAYER_SHIP_COLOR, SHIP_ROTATION, SHIP_THRUST,
|
||||||
SHIP_THRUSTER_COLOR_INACTIVE,
|
SHIP_THRUSTER_COLOR_ACTIVE, SHIP_THRUSTER_COLOR_INACTIVE,
|
||||||
};
|
};
|
||||||
use crate::machinery::AsteroidSpawner;
|
use crate::machinery::AsteroidSpawner;
|
||||||
use crate::objects::{Bullet, Ship, Weapon};
|
use crate::objects::{Bullet, Ship, Weapon};
|
||||||
@@ -65,6 +65,7 @@ impl Plugin for AsteroidPlugin {
|
|||||||
objects::ship_impact_listener,
|
objects::ship_impact_listener,
|
||||||
physics::collision_listener,
|
physics::collision_listener,
|
||||||
machinery::tick_lifetimes,
|
machinery::tick_lifetimes,
|
||||||
|
machinery::update_scoreboard,
|
||||||
)
|
)
|
||||||
.run_if(in_state(GameState::Playing)),
|
.run_if(in_state(GameState::Playing)),
|
||||||
)
|
)
|
||||||
@@ -76,10 +77,10 @@ impl Plugin for AsteroidPlugin {
|
|||||||
)
|
)
|
||||||
.run_if(in_state(GameState::Playing)),
|
.run_if(in_state(GameState::Playing)),
|
||||||
)
|
)
|
||||||
.add_event::<events::SpawnAsteroid>()
|
.add_message::<messages::SpawnAsteroid>()
|
||||||
.add_event::<events::AsteroidDestroy>()
|
.add_message::<messages::AsteroidDestroy>()
|
||||||
.add_event::<events::ShipDestroy>()
|
.add_message::<messages::ShipDestroy>()
|
||||||
.add_event::<events::BulletDestroy>();
|
.add_message::<messages::BulletDestroy>();
|
||||||
app.insert_state(GameState::TitleScreen);
|
app.insert_state(GameState::TitleScreen);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -110,14 +111,28 @@ fn spawn_camera(mut commands: Commands) {
|
|||||||
/// Checks if "W" is pressed and increases velocity accordingly.
|
/// Checks if "W" is pressed and increases velocity accordingly.
|
||||||
fn input_ship_thruster(
|
fn input_ship_thruster(
|
||||||
keyboard_input: Res<ButtonInput<KeyCode>>,
|
keyboard_input: Res<ButtonInput<KeyCode>>,
|
||||||
mut query: Query<(&mut physics::Velocity, &Transform, &mut Children), With<Ship>>,
|
mut query: Query<
|
||||||
|
(
|
||||||
|
&mut physics::Velocity,
|
||||||
|
&Transform,
|
||||||
|
Option<&mut AudioSink>,
|
||||||
|
&mut Children,
|
||||||
|
),
|
||||||
|
With<Ship>,
|
||||||
|
>,
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
game_assets: Res<GameAssets>,
|
game_assets: Res<GameAssets>,
|
||||||
) {
|
) {
|
||||||
// TODO: Maybe change for a Single<Ship>> so this only runs for the one ship
|
// TODO: Maybe change for a Single<Ship>> so this only runs for the one ship
|
||||||
// buuut... that would silently do nothing if there are 0 or >1 ships, and
|
// buuut... that would silently do nothing if there are 0 or >1 ships, and
|
||||||
// I might want to crash on purpose in that case.
|
// I might want to crash on purpose in that case.
|
||||||
let Ok((mut velocity, transform, children)) = query.single_mut() else {
|
//
|
||||||
|
// The AudioSink component doesn't exist for just one frame, forcing it to
|
||||||
|
// be an optional system parameter. I'm not sure if I want to guard it with
|
||||||
|
// a check like it does now, or finally switch to using a Single<...> query
|
||||||
|
// parameter. I would lose ship control if the sound sink didn't spawn, but
|
||||||
|
// that should be fine -- any time that fails, more has likely also failed.
|
||||||
|
let Ok((mut velocity, transform, audio, children)) = query.single_mut() else {
|
||||||
let count = query.iter().count();
|
let count = query.iter().count();
|
||||||
panic!("There should be exactly one player ship! Instead, there seems to be {count}.");
|
panic!("There should be exactly one player ship! Instead, there seems to be {count}.");
|
||||||
};
|
};
|
||||||
@@ -131,10 +146,16 @@ fn input_ship_thruster(
|
|||||||
commands
|
commands
|
||||||
.entity(*thrusters)
|
.entity(*thrusters)
|
||||||
.insert(MeshMaterial2d(game_assets.thruster_mat_active()));
|
.insert(MeshMaterial2d(game_assets.thruster_mat_active()));
|
||||||
|
if let Some(audio) = audio {
|
||||||
|
audio.play();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
commands
|
commands
|
||||||
.entity(*thrusters)
|
.entity(*thrusters)
|
||||||
.insert(MeshMaterial2d(game_assets.thruster_mat_inactive()));
|
.insert(MeshMaterial2d(game_assets.thruster_mat_inactive()));
|
||||||
|
if let Some(audio) = audio {
|
||||||
|
audio.pause();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -181,7 +202,7 @@ fn input_ship_shoot(
|
|||||||
|
|
||||||
// If the weapon is ready and the player presses the trigger,
|
// If the weapon is ready and the player presses the trigger,
|
||||||
// spawn a bullet & reset the timer.
|
// spawn a bullet & reset the timer.
|
||||||
if weapon.finished() && keyboard_input.pressed(KeyCode::Space) {
|
if weapon.is_finished() && keyboard_input.pressed(KeyCode::Space) {
|
||||||
weapon.reset();
|
weapon.reset();
|
||||||
// Derive bullet velocity, add to the ship's velocity
|
// Derive bullet velocity, add to the ship's velocity
|
||||||
let bullet_vel = (ship_pos.rotation * Vec3::X).xy() * BULLET_SPEED;
|
let bullet_vel = (ship_pos.rotation * Vec3::X).xy() * BULLET_SPEED;
|
||||||
@@ -196,8 +217,10 @@ fn input_ship_shoot(
|
|||||||
physics::Velocity(bullet_vel),
|
physics::Velocity(bullet_vel),
|
||||||
Mesh2d(game_assets.bullet().0),
|
Mesh2d(game_assets.bullet().0),
|
||||||
MeshMaterial2d(game_assets.bullet().1),
|
MeshMaterial2d(game_assets.bullet().1),
|
||||||
ship_pos.clone(), // clone ship transform
|
*ship_pos, // clone ship transform
|
||||||
Lifetime(Timer::from_seconds(BULLET_LIFETIME, TimerMode::Once)),
|
Lifetime(Timer::from_seconds(BULLET_LIFETIME, TimerMode::Once)),
|
||||||
|
AudioPlayer::new(game_assets.laser_sound()),
|
||||||
|
PlaybackSettings::ONCE, // `Lifetime` already despawns the entity, so this doesn't need to
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,12 @@ use std::time::Duration;
|
|||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
use crate::{WorldSize, events::SpawnAsteroid, objects::AsteroidSize};
|
use crate::{
|
||||||
|
WorldSize,
|
||||||
|
messages::{AsteroidDestroy, SpawnAsteroid},
|
||||||
|
objects::AsteroidSize,
|
||||||
|
resources::Score,
|
||||||
|
};
|
||||||
|
|
||||||
/// Asteroid spawning parameters and state.
|
/// Asteroid spawning parameters and state.
|
||||||
///
|
///
|
||||||
@@ -38,7 +43,7 @@ impl AsteroidSpawner {
|
|||||||
/// Update the asteroid spawn timer in the [`AsteroidSpawner`] resource, and
|
/// Update the asteroid spawn timer in the [`AsteroidSpawner`] resource, and
|
||||||
/// spawns any asteroids that are due this frame.
|
/// spawns any asteroids that are due this frame.
|
||||||
pub fn tick_asteroid_manager(
|
pub fn tick_asteroid_manager(
|
||||||
mut events: EventWriter<SpawnAsteroid>,
|
mut events: MessageWriter<SpawnAsteroid>,
|
||||||
mut spawner: ResMut<AsteroidSpawner>,
|
mut spawner: ResMut<AsteroidSpawner>,
|
||||||
time: Res<Time>,
|
time: Res<Time>,
|
||||||
play_area: Res<WorldSize>,
|
play_area: Res<WorldSize>,
|
||||||
@@ -135,3 +140,15 @@ pub fn operate_sparklers(sparklers: Query<(&mut Visibility, &mut Sparkler)>, tim
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Event listener for adding score after an asteroid was destroyed
|
||||||
|
///
|
||||||
|
/// Refreshing the HUD element is done by [crate::widgets::operate_ui] (a private function)
|
||||||
|
pub fn update_scoreboard(
|
||||||
|
mut destroy_events: MessageReader<AsteroidDestroy>,
|
||||||
|
mut scoreboard: ResMut<Score>,
|
||||||
|
) {
|
||||||
|
for _event in destroy_events.read() {
|
||||||
|
scoreboard.0 += 100;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
30
src/main.rs
30
src/main.rs
@@ -1,20 +1,28 @@
|
|||||||
use bevy::{prelude::*, window::WindowResolution};
|
use bevy::{prelude::*, window::WindowResolution};
|
||||||
|
|
||||||
use asteroids::{AsteroidPlugin, config::WINDOW_SIZE};
|
use asteroids::{AsteroidPlugin, config::WINDOW_SIZE};
|
||||||
|
|
||||||
|
#[cfg(feature = "debug_ui")]
|
||||||
use bevy_inspector_egui::{bevy_egui::EguiPlugin, quick::WorldInspectorPlugin};
|
use bevy_inspector_egui::{bevy_egui::EguiPlugin, quick::WorldInspectorPlugin};
|
||||||
|
#[cfg(feature = "debug_ui")]
|
||||||
|
use bevy_rapier2d::render::RapierDebugRenderPlugin;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new()
|
let mut app = App::new();
|
||||||
.add_plugins(DefaultPlugins.set(WindowPlugin {
|
app.add_plugins(DefaultPlugins.set(WindowPlugin {
|
||||||
primary_window: Some(Window {
|
primary_window: Some(Window {
|
||||||
canvas: Some("#game-canvas".to_owned()),
|
canvas: Some("#game-canvas".to_owned()),
|
||||||
resolution: WindowResolution::new(WINDOW_SIZE.x, WINDOW_SIZE.y),
|
resolution: WindowResolution::new(WINDOW_SIZE.0, WINDOW_SIZE.1),
|
||||||
..default()
|
|
||||||
}),
|
|
||||||
..default()
|
..default()
|
||||||
}))
|
}),
|
||||||
.add_plugins(AsteroidPlugin)
|
..default()
|
||||||
.add_plugins(EguiPlugin::default())
|
}))
|
||||||
|
.add_plugins(AsteroidPlugin);
|
||||||
|
|
||||||
|
#[cfg(feature = "debug_ui")]
|
||||||
|
app.add_plugins(EguiPlugin::default())
|
||||||
.add_plugins(WorldInspectorPlugin::new())
|
.add_plugins(WorldInspectorPlugin::new())
|
||||||
.run();
|
.add_plugins(RapierDebugRenderPlugin::default());
|
||||||
|
|
||||||
|
app.run();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,15 +6,15 @@ use crate::objects::AsteroidSize;
|
|||||||
///
|
///
|
||||||
/// Produced by the [`fn collision_listener(...)`](`crate::physics::collision_listener`)
|
/// Produced by the [`fn collision_listener(...)`](`crate::physics::collision_listener`)
|
||||||
/// system when the player collides with an asteroid. They are consumed by [`fn ship_impact_listener(...)`](`crate::objects::ship_impact_listener`).
|
/// system when the player collides with an asteroid. They are consumed by [`fn ship_impact_listener(...)`](`crate::objects::ship_impact_listener`).
|
||||||
#[derive(Event)]
|
#[derive(Message)]
|
||||||
pub(crate) struct ShipDestroy;
|
pub(crate) struct ShipDestroy;
|
||||||
|
|
||||||
/// Signals that a particular asteroid has been destroyed.
|
/// Signals that a particular asteroid has been destroyed.
|
||||||
///
|
///
|
||||||
/// Produced by the [`fn collision_listener(...)`](`crate::physics::collision_listener`)
|
/// Produced by the [`fn collision_listener(...)`](`crate::physics::collision_listener`)
|
||||||
/// system when bullets contact asteroids. It is consumed by [`fn split_asteroid(...)`](`crate::objects::spawn_asteroid`)
|
/// system when bullets contact asteroids. It is consumed by [`fn split_asteroid(...)`](`crate::objects::spawn_asteroid`)
|
||||||
/// system, which re-emits [spawn events](`SpawnAsteroid`).
|
/// system, which re-emits [spawn messages](`SpawnAsteroid`).
|
||||||
#[derive(Event)]
|
#[derive(Message)]
|
||||||
pub(crate) struct AsteroidDestroy(pub Entity);
|
pub(crate) struct AsteroidDestroy(pub Entity);
|
||||||
|
|
||||||
/// Signals that an asteroid needs to be spawned and provides the parameters
|
/// Signals that an asteroid needs to be spawned and provides the parameters
|
||||||
@@ -22,7 +22,7 @@ pub(crate) struct AsteroidDestroy(pub Entity);
|
|||||||
///
|
///
|
||||||
/// Produced by the [`tick_asteroid_manager(...)`](`crate::machinery::tick_asteroid_manager`)
|
/// Produced by the [`tick_asteroid_manager(...)`](`crate::machinery::tick_asteroid_manager`)
|
||||||
/// system and consumed by the [`spawn_asteroid(...)`](`crate::objects::spawn_asteroid`) system.
|
/// system and consumed by the [`spawn_asteroid(...)`](`crate::objects::spawn_asteroid`) system.
|
||||||
#[derive(Event)]
|
#[derive(Message)]
|
||||||
pub struct SpawnAsteroid {
|
pub struct SpawnAsteroid {
|
||||||
pub pos: Vec2,
|
pub pos: Vec2,
|
||||||
pub vel: Vec2,
|
pub vel: Vec2,
|
||||||
@@ -32,5 +32,5 @@ pub struct SpawnAsteroid {
|
|||||||
/// Signals that a particular bullet has been destroyed (after it strikes an Asteroid).
|
/// Signals that a particular bullet has been destroyed (after it strikes an Asteroid).
|
||||||
///
|
///
|
||||||
/// TODO: Maybe use it for lifetime expiration (which is also a TODO item).
|
/// TODO: Maybe use it for lifetime expiration (which is also a TODO item).
|
||||||
#[derive(Event)]
|
#[derive(Message)]
|
||||||
pub(crate) struct BulletDestroy(pub Entity);
|
pub(crate) struct BulletDestroy(pub Entity);
|
||||||
@@ -3,18 +3,19 @@
|
|||||||
//! Asteroids, the player's ship, and such.
|
//! Asteroids, the player's ship, and such.
|
||||||
|
|
||||||
use bevy::{
|
use bevy::{
|
||||||
|
audio::{AudioPlayer, PlaybackSettings},
|
||||||
|
camera::visibility::Visibility,
|
||||||
ecs::{
|
ecs::{
|
||||||
bundle::Bundle,
|
|
||||||
component::Component,
|
component::Component,
|
||||||
entity::Entity,
|
entity::Entity,
|
||||||
event::{EventReader, EventWriter},
|
message::{MessageReader, MessageWriter},
|
||||||
query::With,
|
query::With,
|
||||||
system::{Commands, Query, Res, ResMut, Single},
|
system::{Commands, Query, Res, ResMut, Single},
|
||||||
},
|
},
|
||||||
math::{Vec2, Vec3, Vec3Swizzles},
|
math::{Vec2, Vec3, Vec3Swizzles},
|
||||||
|
mesh::Mesh2d,
|
||||||
prelude::{Deref, DerefMut},
|
prelude::{Deref, DerefMut},
|
||||||
render::{mesh::Mesh2d, view::Visibility},
|
sprite_render::MeshMaterial2d,
|
||||||
sprite::MeshMaterial2d,
|
|
||||||
state::state::NextState,
|
state::state::NextState,
|
||||||
time::{Timer, TimerMode},
|
time::{Timer, TimerMode},
|
||||||
transform::components::Transform,
|
transform::components::Transform,
|
||||||
@@ -24,8 +25,8 @@ use bevy_rapier2d::prelude::{ActiveCollisionTypes, ActiveEvents, Collider, Senso
|
|||||||
use crate::{
|
use crate::{
|
||||||
AngularVelocity, GameAssets, GameState, Lives,
|
AngularVelocity, GameAssets, GameState, Lives,
|
||||||
config::{ASTEROID_LIFETIME, DEBRIS_LIFETIME, SHIP_FIRE_RATE},
|
config::{ASTEROID_LIFETIME, DEBRIS_LIFETIME, SHIP_FIRE_RATE},
|
||||||
events::{AsteroidDestroy, BulletDestroy, ShipDestroy, SpawnAsteroid},
|
|
||||||
machinery::{Lifetime, Sparkler},
|
machinery::{Lifetime, Sparkler},
|
||||||
|
messages::{AsteroidDestroy, BulletDestroy, ShipDestroy, SpawnAsteroid},
|
||||||
physics::{Velocity, Wrapping},
|
physics::{Velocity, Wrapping},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -70,7 +71,7 @@ pub struct Debris;
|
|||||||
|
|
||||||
/// Responds to [`SpawnAsteroid`] events, spawning as specified
|
/// Responds to [`SpawnAsteroid`] events, spawning as specified
|
||||||
pub fn spawn_asteroid(
|
pub fn spawn_asteroid(
|
||||||
mut events: EventReader<SpawnAsteroid>,
|
mut events: MessageReader<SpawnAsteroid>,
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
game_assets: Res<GameAssets>,
|
game_assets: Res<GameAssets>,
|
||||||
) {
|
) {
|
||||||
@@ -82,9 +83,9 @@ pub fn spawn_asteroid(
|
|||||||
};
|
};
|
||||||
|
|
||||||
let collider_radius = match spawn.size {
|
let collider_radius = match spawn.size {
|
||||||
AsteroidSize::Small => 10.0,
|
AsteroidSize::Small => 5.0,
|
||||||
AsteroidSize::Medium => 20.0,
|
AsteroidSize::Medium => 10.0,
|
||||||
AsteroidSize::Large => 40.0,
|
AsteroidSize::Large => 20.0,
|
||||||
};
|
};
|
||||||
|
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
@@ -110,10 +111,11 @@ pub fn spawn_asteroid(
|
|||||||
/// The velocity of the child asteroids is scattered somewhat, as if they were
|
/// The velocity of the child asteroids is scattered somewhat, as if they were
|
||||||
/// explosively pushed apart.
|
/// explosively pushed apart.
|
||||||
pub fn split_asteroids(
|
pub fn split_asteroids(
|
||||||
mut destroy_events: EventReader<AsteroidDestroy>,
|
mut destroy_events: MessageReader<AsteroidDestroy>,
|
||||||
mut respawn_events: EventWriter<SpawnAsteroid>,
|
mut respawn_events: MessageWriter<SpawnAsteroid>,
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
query: Query<(&Transform, &Asteroid, &Velocity)>,
|
query: Query<(&Transform, &Asteroid, &Velocity)>,
|
||||||
|
game_assets: Res<GameAssets>,
|
||||||
) {
|
) {
|
||||||
for event in destroy_events.read() {
|
for event in destroy_events.read() {
|
||||||
if let Ok((transform, rock, velocity)) = query.get(event.0) {
|
if let Ok((transform, rock, velocity)) = query.get(event.0) {
|
||||||
@@ -136,6 +138,12 @@ pub fn split_asteroids(
|
|||||||
// Always despawn the asteroid. New ones (may) be spawned in it's
|
// Always despawn the asteroid. New ones (may) be spawned in it's
|
||||||
// place, but this one is gone.
|
// place, but this one is gone.
|
||||||
commands.entity(event.0).despawn();
|
commands.entity(event.0).despawn();
|
||||||
|
|
||||||
|
// Play a sound for the asteroid exploding
|
||||||
|
commands.spawn((
|
||||||
|
AudioPlayer::new(game_assets.asteroid_crack_sound()),
|
||||||
|
PlaybackSettings::DESPAWN,
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -160,6 +168,12 @@ pub fn spawn_player(mut commands: Commands, game_assets: Res<GameAssets>) {
|
|||||||
Mesh2d(game_assets.ship().0),
|
Mesh2d(game_assets.ship().0),
|
||||||
MeshMaterial2d(game_assets.ship().1),
|
MeshMaterial2d(game_assets.ship().1),
|
||||||
Transform::default().with_scale(Vec3::new(20.0, 20.0, 20.0)),
|
Transform::default().with_scale(Vec3::new(20.0, 20.0, 20.0)),
|
||||||
|
AudioPlayer::new(game_assets.ship_thruster_sound()),
|
||||||
|
PlaybackSettings {
|
||||||
|
mode: bevy::audio::PlaybackMode::Loop,
|
||||||
|
paused: true,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
))
|
))
|
||||||
.with_child((
|
.with_child((
|
||||||
Mesh2d(game_assets.thruster_mesh()),
|
Mesh2d(game_assets.thruster_mesh()),
|
||||||
@@ -172,7 +186,7 @@ pub fn spawn_player(mut commands: Commands, game_assets: Res<GameAssets>) {
|
|||||||
|
|
||||||
/// Watch for [`BulletDestroy`] events and despawn
|
/// Watch for [`BulletDestroy`] events and despawn
|
||||||
/// the associated bullet.
|
/// the associated bullet.
|
||||||
pub fn bullet_impact_listener(mut commands: Commands, mut events: EventReader<BulletDestroy>) {
|
pub fn bullet_impact_listener(mut commands: Commands, mut events: MessageReader<BulletDestroy>) {
|
||||||
for event in events.read() {
|
for event in events.read() {
|
||||||
commands.entity(event.0).despawn();
|
commands.entity(event.0).despawn();
|
||||||
}
|
}
|
||||||
@@ -188,7 +202,7 @@ pub fn bullet_impact_listener(mut commands: Commands, mut events: EventReader<Bu
|
|||||||
/// - Clear all asteroids
|
/// - Clear all asteroids
|
||||||
/// - Respawn player
|
/// - Respawn player
|
||||||
pub fn ship_impact_listener(
|
pub fn ship_impact_listener(
|
||||||
mut events: EventReader<ShipDestroy>,
|
mut events: MessageReader<ShipDestroy>,
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
mut lives: ResMut<Lives>,
|
mut lives: ResMut<Lives>,
|
||||||
rocks: Query<Entity, With<Asteroid>>,
|
rocks: Query<Entity, With<Asteroid>>,
|
||||||
@@ -224,7 +238,7 @@ pub fn ship_impact_listener(
|
|||||||
Sparkler::at_interval(0.15),
|
Sparkler::at_interval(0.15),
|
||||||
Mesh2d(game_assets.thruster_mesh()), // borrow the thruster mesh for now
|
Mesh2d(game_assets.thruster_mesh()), // borrow the thruster mesh for now
|
||||||
MeshMaterial2d(game_assets.thruster_mat_active()), // ... and the active thruster material
|
MeshMaterial2d(game_assets.thruster_mat_active()), // ... and the active thruster material
|
||||||
player.0.clone(), // clone the player transform
|
*player.0, // clone the player transform
|
||||||
Velocity(vel),
|
Velocity(vel),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
@@ -232,5 +246,11 @@ pub fn ship_impact_listener(
|
|||||||
// STEP 4: Respawn player (teleport them to the origin)
|
// STEP 4: Respawn player (teleport them to the origin)
|
||||||
player.0.translation = Vec3::ZERO;
|
player.0.translation = Vec3::ZERO;
|
||||||
player.1.0 = Vec2::ZERO;
|
player.1.0 = Vec2::ZERO;
|
||||||
|
|
||||||
|
// STEP 5: Play crash sound
|
||||||
|
commands.spawn((
|
||||||
|
AudioPlayer::new(game_assets.wreck_sound()),
|
||||||
|
PlaybackSettings::DESPAWN, // despawn this entity when playback ends.
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
//! to detect them for me (so that I don't have to write clipping code).
|
//! to detect them for me (so that I don't have to write clipping code).
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
WorldSize, events,
|
WorldSize, messages,
|
||||||
objects::{Asteroid, Bullet, Ship},
|
objects::{Asteroid, Bullet, Ship},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -89,10 +89,10 @@ pub(crate) fn wrap_entities(
|
|||||||
/// | Bullet & Bullet | Nothing. Bullets won't collide with each other (and probably can't under normal gameplay conditions) |
|
/// | Bullet & Bullet | Nothing. Bullets won't collide with each other (and probably can't under normal gameplay conditions) |
|
||||||
/// | Bullet & Ship | Nothing. The player shouldn't be able to shoot themselves (and the Flying Saucer hasn't been impl.'d, so it's bullets don't count) |
|
/// | Bullet & Ship | Nothing. The player shouldn't be able to shoot themselves (and the Flying Saucer hasn't been impl.'d, so it's bullets don't count) |
|
||||||
pub fn collision_listener(
|
pub fn collision_listener(
|
||||||
mut collisions: EventReader<CollisionEvent>,
|
mut collisions: MessageReader<CollisionEvent>,
|
||||||
mut ship_writer: EventWriter<events::ShipDestroy>,
|
mut ship_writer: MessageWriter<messages::ShipDestroy>,
|
||||||
mut asteroid_writer: EventWriter<events::AsteroidDestroy>,
|
mut asteroid_writer: MessageWriter<messages::AsteroidDestroy>,
|
||||||
mut bullet_writer: EventWriter<events::BulletDestroy>,
|
mut bullet_writer: MessageWriter<messages::BulletDestroy>,
|
||||||
player: Single<Entity, With<Ship>>,
|
player: Single<Entity, With<Ship>>,
|
||||||
bullets: Query<&Bullet>,
|
bullets: Query<&Bullet>,
|
||||||
rocks: Query<&Asteroid>,
|
rocks: Query<&Asteroid>,
|
||||||
@@ -111,29 +111,21 @@ pub fn collision_listener(
|
|||||||
if *one == *player {
|
if *one == *player {
|
||||||
if rocks.contains(*two) {
|
if rocks.contains(*two) {
|
||||||
// player-asteroid collision
|
// player-asteroid collision
|
||||||
dbg!("Writing ShipDestroy event");
|
ship_writer.write(messages::ShipDestroy);
|
||||||
ship_writer.write(events::ShipDestroy);
|
|
||||||
} // else, we don't care
|
} // else, we don't care
|
||||||
} else if *two == *player {
|
} else if *two == *player && rocks.contains(*one) {
|
||||||
if rocks.contains(*one) {
|
ship_writer.write(messages::ShipDestroy);
|
||||||
dbg!("Writing ShipDestroy event");
|
|
||||||
ship_writer.write(events::ShipDestroy);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Option 2: Bullet & Asteroid
|
// Option 2: Bullet & Asteroid
|
||||||
if bullets.contains(*one) {
|
if bullets.contains(*one) {
|
||||||
if rocks.contains(*two) {
|
if rocks.contains(*two) {
|
||||||
dbg!("Writing AsteroidDestroy & BulletDestroy events");
|
asteroid_writer.write(messages::AsteroidDestroy(*two));
|
||||||
asteroid_writer.write(events::AsteroidDestroy(*two));
|
bullet_writer.write(messages::BulletDestroy(*one));
|
||||||
bullet_writer.write(events::BulletDestroy(*one));
|
|
||||||
}
|
|
||||||
} else if rocks.contains(*one) {
|
|
||||||
if bullets.contains(*two) {
|
|
||||||
dbg!("Writing AsteroidDestroy & BulletDestroy events");
|
|
||||||
asteroid_writer.write(events::AsteroidDestroy(*one));
|
|
||||||
bullet_writer.write(events::BulletDestroy(*two));
|
|
||||||
}
|
}
|
||||||
|
} else if rocks.contains(*one) && bullets.contains(*two) {
|
||||||
|
asteroid_writer.write(messages::AsteroidDestroy(*one));
|
||||||
|
bullet_writer.write(messages::BulletDestroy(*two));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
119
src/resources.rs
119
src/resources.rs
@@ -1,25 +1,27 @@
|
|||||||
//! All the resources for the game
|
//! All the resources for the game
|
||||||
|
|
||||||
use bevy::{
|
use bevy::{
|
||||||
asset::{Assets, Handle},
|
asset::{AssetServer, Assets, Handle},
|
||||||
|
audio::AudioSource,
|
||||||
ecs::{
|
ecs::{
|
||||||
resource::Resource,
|
resource::Resource,
|
||||||
world::{FromWorld, World},
|
world::{FromWorld, World},
|
||||||
},
|
},
|
||||||
math::{
|
math::{
|
||||||
Vec2,
|
Vec2,
|
||||||
primitives::{Circle, Triangle2d},
|
primitives::{Polyline2d, Segment2d, Triangle2d},
|
||||||
},
|
},
|
||||||
|
mesh::Mesh,
|
||||||
prelude::{Deref, DerefMut, Reflect, ReflectResource},
|
prelude::{Deref, DerefMut, Reflect, ReflectResource},
|
||||||
render::mesh::Mesh,
|
sprite_render::ColorMaterial,
|
||||||
sprite::ColorMaterial,
|
|
||||||
};
|
};
|
||||||
use bevy_inspector_egui::InspectorOptions;
|
use bevy_inspector_egui::InspectorOptions;
|
||||||
use bevy_inspector_egui::inspector_options::ReflectInspectorOptions;
|
use bevy_inspector_egui::inspector_options::ReflectInspectorOptions;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ASTEROID_SMALL_COLOR, BULLET_COLOR, PLAYER_SHIP_COLOR, SHIP_THRUSTER_COLOR_ACTIVE,
|
ASTEROID_LARGE_COLOR, ASTEROID_MEDIUM_COLOR, ASTEROID_SMALL_COLOR, BULLET_COLOR,
|
||||||
SHIP_THRUSTER_COLOR_INACTIVE, config::WINDOW_SIZE,
|
PLAYER_SHIP_COLOR, SHIP_THRUSTER_COLOR_ACTIVE, SHIP_THRUSTER_COLOR_INACTIVE,
|
||||||
|
config::WINDOW_SIZE,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(InspectorOptions, Reflect, Resource, Debug, Deref, Clone, Copy)]
|
#[derive(InspectorOptions, Reflect, Resource, Debug, Deref, Clone, Copy)]
|
||||||
@@ -42,12 +44,14 @@ impl From<Lives> for String {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: consider switching this to use a u32 pair like the Window settings
|
||||||
|
// thing now does.
|
||||||
#[derive(Deref, DerefMut, Resource)]
|
#[derive(Deref, DerefMut, Resource)]
|
||||||
pub struct WorldSize(Vec2);
|
pub struct WorldSize(Vec2);
|
||||||
|
|
||||||
impl Default for WorldSize {
|
impl Default for WorldSize {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
WorldSize(Vec2::new(WINDOW_SIZE.x, WINDOW_SIZE.y))
|
WorldSize(Vec2::new(WINDOW_SIZE.0 as f32, WINDOW_SIZE.1 as f32))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,6 +59,7 @@ impl Default for WorldSize {
|
|||||||
pub struct GameAssets {
|
pub struct GameAssets {
|
||||||
meshes: [Handle<Mesh>; 5],
|
meshes: [Handle<Mesh>; 5],
|
||||||
materials: [Handle<ColorMaterial>; 7],
|
materials: [Handle<ColorMaterial>; 7],
|
||||||
|
sounds: [Handle<AudioSource>; 4],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GameAssets {
|
impl GameAssets {
|
||||||
@@ -79,20 +84,36 @@ impl GameAssets {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn asteroid_small(&self) -> (Handle<Mesh>, Handle<ColorMaterial>) {
|
pub fn asteroid_small(&self) -> (Handle<Mesh>, Handle<ColorMaterial>) {
|
||||||
(self.meshes[1].clone(), self.materials[1].clone())
|
(self.meshes[1].clone(), self.materials[3].clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn asteroid_medium(&self) -> (Handle<Mesh>, Handle<ColorMaterial>) {
|
pub fn asteroid_medium(&self) -> (Handle<Mesh>, Handle<ColorMaterial>) {
|
||||||
(self.meshes[2].clone(), self.materials[2].clone())
|
(self.meshes[2].clone(), self.materials[4].clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn asteroid_large(&self) -> (Handle<Mesh>, Handle<ColorMaterial>) {
|
pub fn asteroid_large(&self) -> (Handle<Mesh>, Handle<ColorMaterial>) {
|
||||||
(self.meshes[3].clone(), self.materials[3].clone())
|
(self.meshes[3].clone(), self.materials[5].clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn bullet(&self) -> (Handle<Mesh>, Handle<ColorMaterial>) {
|
pub fn bullet(&self) -> (Handle<Mesh>, Handle<ColorMaterial>) {
|
||||||
(self.meshes[4].clone(), self.materials[6].clone())
|
(self.meshes[4].clone(), self.materials[6].clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn wreck_sound(&self) -> Handle<AudioSource> {
|
||||||
|
self.sounds[0].clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn laser_sound(&self) -> Handle<AudioSource> {
|
||||||
|
self.sounds[1].clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn asteroid_crack_sound(&self) -> Handle<AudioSource> {
|
||||||
|
self.sounds[2].clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn ship_thruster_sound(&self) -> Handle<AudioSource> {
|
||||||
|
self.sounds[3].clone()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromWorld for GameAssets {
|
impl FromWorld for GameAssets {
|
||||||
@@ -104,10 +125,63 @@ impl FromWorld for GameAssets {
|
|||||||
Vec2::new(-0.5, 0.45),
|
Vec2::new(-0.5, 0.45),
|
||||||
Vec2::new(-0.5, -0.45),
|
Vec2::new(-0.5, -0.45),
|
||||||
)),
|
)),
|
||||||
world_meshes.add(Circle::new(10.0)),
|
world_meshes.add(Polyline2d::new(
|
||||||
world_meshes.add(Circle::new(20.0)),
|
[
|
||||||
world_meshes.add(Circle::new(40.0)),
|
Vec2::new(0.1, 0.0),
|
||||||
world_meshes.add(Circle::new(0.2)),
|
Vec2::new(0.8, 0.2),
|
||||||
|
Vec2::new(0.8, 0.3),
|
||||||
|
Vec2::new(0.1, 1.0),
|
||||||
|
Vec2::new(-0.5, 1.0),
|
||||||
|
Vec2::new(-0.3, 0.3),
|
||||||
|
Vec2::new(-1.0, 0.3),
|
||||||
|
Vec2::new(-1.0, -0.2),
|
||||||
|
Vec2::new(-0.5, -1.0),
|
||||||
|
Vec2::new(0.1, -0.8),
|
||||||
|
Vec2::new(0.5, -0.9),
|
||||||
|
Vec2::new(1.0, -0.4),
|
||||||
|
Vec2::new(0.1, 0.0),
|
||||||
|
]
|
||||||
|
.into_iter()
|
||||||
|
.map(|vert| vert * 5.0),
|
||||||
|
)),
|
||||||
|
world_meshes.add(Polyline2d::new(
|
||||||
|
[
|
||||||
|
Vec2::new(0.6, 0.3),
|
||||||
|
Vec2::new(1.0, 0.6),
|
||||||
|
Vec2::new(0.6, 1.0),
|
||||||
|
Vec2::new(0.1, 0.8),
|
||||||
|
Vec2::new(-0.4, 1.0),
|
||||||
|
Vec2::new(-1.0, 0.6),
|
||||||
|
Vec2::new(-0.8, -0.1),
|
||||||
|
Vec2::new(-1.0, -0.5),
|
||||||
|
Vec2::new(-0.4, -1.0),
|
||||||
|
Vec2::new(-0.3, -0.7),
|
||||||
|
Vec2::new(0.6, -1.0),
|
||||||
|
Vec2::new(1.0, -0.3),
|
||||||
|
Vec2::new(0.6, 0.3),
|
||||||
|
]
|
||||||
|
.into_iter()
|
||||||
|
.map(|vert| vert * 10.0),
|
||||||
|
)),
|
||||||
|
world_meshes.add(Polyline2d::new(
|
||||||
|
[
|
||||||
|
Vec2::new(1.0, -0.1),
|
||||||
|
Vec2::new(1.0, 0.3),
|
||||||
|
Vec2::new(0.4, 1.0),
|
||||||
|
Vec2::new(-0.2, 1.0),
|
||||||
|
Vec2::new(-0.9, 0.3),
|
||||||
|
Vec2::new(-0.5, 0.1),
|
||||||
|
Vec2::new(-0.9, -0.1),
|
||||||
|
Vec2::new(-0.5, -1.0),
|
||||||
|
Vec2::new(0.0, 0.0),
|
||||||
|
Vec2::new(0.0, -1.0),
|
||||||
|
Vec2::new(0.5, -1.0),
|
||||||
|
Vec2::new(1.0, -0.1),
|
||||||
|
]
|
||||||
|
.into_iter()
|
||||||
|
.map(|vert| vert * 20.0),
|
||||||
|
)),
|
||||||
|
world_meshes.add(Segment2d::new(Vec2::new(-0.1, 0.0), Vec2::new(0.1, 0.0))),
|
||||||
];
|
];
|
||||||
let mut world_materials = world.resource_mut::<Assets<ColorMaterial>>();
|
let mut world_materials = world.resource_mut::<Assets<ColorMaterial>>();
|
||||||
let materials = [
|
let materials = [
|
||||||
@@ -116,10 +190,21 @@ impl FromWorld for GameAssets {
|
|||||||
world_materials.add(SHIP_THRUSTER_COLOR_ACTIVE),
|
world_materials.add(SHIP_THRUSTER_COLOR_ACTIVE),
|
||||||
world_materials.add(ASTEROID_SMALL_COLOR),
|
world_materials.add(ASTEROID_SMALL_COLOR),
|
||||||
// TODO: asteroid medium and large colors
|
// TODO: asteroid medium and large colors
|
||||||
world_materials.add(ASTEROID_SMALL_COLOR),
|
world_materials.add(ASTEROID_MEDIUM_COLOR),
|
||||||
world_materials.add(ASTEROID_SMALL_COLOR),
|
world_materials.add(ASTEROID_LARGE_COLOR),
|
||||||
world_materials.add(BULLET_COLOR),
|
world_materials.add(BULLET_COLOR),
|
||||||
];
|
];
|
||||||
GameAssets { meshes, materials }
|
let loader = world.resource_mut::<AssetServer>();
|
||||||
|
let sounds = [
|
||||||
|
loader.load("explosionCrunch_004.ogg"),
|
||||||
|
loader.load("laserSmall_001.ogg"),
|
||||||
|
loader.load("explosionCrunch_000.ogg"),
|
||||||
|
loader.load("thrusterFire_004.ogg"),
|
||||||
|
];
|
||||||
|
GameAssets {
|
||||||
|
meshes,
|
||||||
|
materials,
|
||||||
|
sounds,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ use crate::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use bevy::{
|
use bevy::{
|
||||||
color::palettes::css::{BLACK, DARK_GRAY, GREEN, LIGHT_BLUE, RED, WHITE},
|
color::palettes::css::{BLACK, DARK_GRAY, GREEN, RED, WHITE},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -124,7 +124,7 @@ fn button_bundle(text: &str) -> impl Bundle {
|
|||||||
margin: UiRect::all(Val::Px(5.0)),
|
margin: UiRect::all(Val::Px(5.0)),
|
||||||
..default()
|
..default()
|
||||||
},
|
},
|
||||||
BorderColor(Color::BLACK),
|
BorderColor::all(Color::BLACK),
|
||||||
BorderRadius::MAX,
|
BorderRadius::MAX,
|
||||||
BackgroundColor(UI_BUTTON_NORMAL),
|
BackgroundColor(UI_BUTTON_NORMAL),
|
||||||
children![(
|
children![(
|
||||||
@@ -152,7 +152,7 @@ fn spawn_menu(mut commands: Commands) {
|
|||||||
cmds.spawn((
|
cmds.spawn((
|
||||||
Text::new("Robert's Bad Asteroids Game"),
|
Text::new("Robert's Bad Asteroids Game"),
|
||||||
TextFont::from_font_size(50.0),
|
TextFont::from_font_size(50.0),
|
||||||
TextLayout::new_with_justify(JustifyText::Center),
|
TextLayout::new_with_justify(Justify::Center),
|
||||||
TextShadow::default(),
|
TextShadow::default(),
|
||||||
));
|
));
|
||||||
cmds.spawn((
|
cmds.spawn((
|
||||||
@@ -180,9 +180,9 @@ fn spawn_get_ready(mut commands: Commands, mut timer: ResMut<ReadySetGoTimer>) {
|
|||||||
height: Val::Percent(30.),
|
height: Val::Percent(30.),
|
||||||
..default()
|
..default()
|
||||||
},
|
},
|
||||||
BackgroundColor(LIGHT_BLUE.into()),
|
BackgroundColor(Color::NONE),
|
||||||
children![
|
children![
|
||||||
(Text::new("Get Ready!"), TextColor(BLACK.into())),
|
(Text::new("Get Ready!"), TextColor(WHITE.into())),
|
||||||
(
|
(
|
||||||
CountdownBar,
|
CountdownBar,
|
||||||
Node {
|
Node {
|
||||||
@@ -247,7 +247,7 @@ fn animate_get_ready_widget(
|
|||||||
bar_segment.width = Val::Percent(100.0 * (1.0 - timer.fraction()));
|
bar_segment.width = Val::Percent(100.0 * (1.0 - timer.fraction()));
|
||||||
|
|
||||||
// If the timer has expired, change state to playing.
|
// If the timer has expired, change state to playing.
|
||||||
if timer.finished() {
|
if timer.is_finished() {
|
||||||
game_state.set(GameState::Playing);
|
game_state.set(GameState::Playing);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -263,6 +263,7 @@ fn animate_get_ready_widget(
|
|||||||
/// on the HUD, this system would quit the game. The same will happen for
|
/// on the HUD, this system would quit the game. The same will happen for
|
||||||
/// returning to the title screen. This should be useful for making a pause
|
/// returning to the title screen. This should be useful for making a pause
|
||||||
/// menu, too.
|
/// menu, too.
|
||||||
|
#[allow(clippy::type_complexity)]
|
||||||
fn operate_buttons(
|
fn operate_buttons(
|
||||||
mut interactions: Query<
|
mut interactions: Query<
|
||||||
(
|
(
|
||||||
@@ -274,14 +275,14 @@ fn operate_buttons(
|
|||||||
(Changed<Interaction>, With<Button>),
|
(Changed<Interaction>, With<Button>),
|
||||||
>,
|
>,
|
||||||
mut game_state: ResMut<NextState<GameState>>,
|
mut game_state: ResMut<NextState<GameState>>,
|
||||||
mut app_exit_events: EventWriter<AppExit>,
|
mut app_exit_events: MessageWriter<AppExit>,
|
||||||
) {
|
) {
|
||||||
// TODO: Better colors. These are taken from the example and they're ugly.
|
// TODO: Better colors. These are taken from the example and they're ugly.
|
||||||
for (interaction, mut color, mut border_color, menu_action) in &mut interactions {
|
for (interaction, mut color, mut border_color, menu_action) in &mut interactions {
|
||||||
match *interaction {
|
match *interaction {
|
||||||
Interaction::Pressed => {
|
Interaction::Pressed => {
|
||||||
*color = UI_BUTTON_PRESSED.into();
|
*color = UI_BUTTON_PRESSED.into();
|
||||||
border_color.0 = DARK_GRAY.into();
|
border_color.set_all(DARK_GRAY);
|
||||||
match menu_action {
|
match menu_action {
|
||||||
ButtonMenuAction::ToMainMenu => {
|
ButtonMenuAction::ToMainMenu => {
|
||||||
game_state.set(GameState::TitleScreen);
|
game_state.set(GameState::TitleScreen);
|
||||||
@@ -296,11 +297,11 @@ fn operate_buttons(
|
|||||||
}
|
}
|
||||||
Interaction::Hovered => {
|
Interaction::Hovered => {
|
||||||
*color = UI_BUTTON_HOVERED.into();
|
*color = UI_BUTTON_HOVERED.into();
|
||||||
border_color.0 = WHITE.into();
|
border_color.set_all(WHITE);
|
||||||
}
|
}
|
||||||
Interaction::None => {
|
Interaction::None => {
|
||||||
*color = UI_BUTTON_NORMAL.into();
|
*color = UI_BUTTON_NORMAL.into();
|
||||||
border_color.0 = BLACK.into();
|
border_color.set_all(BLACK);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,15 +2,46 @@
|
|||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<style>
|
<style>
|
||||||
h1 {
|
body {
|
||||||
text-align: center;
|
background-color: hsl(200, 3%, 65%);
|
||||||
}
|
}
|
||||||
|
h1 {
|
||||||
|
color: hsl(200, 3%, 90%);
|
||||||
|
background-color: hsl(195, 5%, 17%);
|
||||||
|
text-align: center;
|
||||||
|
margin: auto;
|
||||||
|
padding: 0.5em;
|
||||||
|
}
|
||||||
|
#prestart-controls,
|
||||||
canvas {
|
canvas {
|
||||||
padding-left: 0;
|
margin-top: 1em;
|
||||||
padding-right: 0;
|
|
||||||
margin-left: auto;
|
margin-left: auto;
|
||||||
margin-right: auto;
|
margin-right: auto;
|
||||||
display: block;
|
display: block;
|
||||||
|
outline-color: hsl(200, 7%, 50%);
|
||||||
|
outline-style: outset;
|
||||||
|
border-radius: 8px;
|
||||||
|
background-color: rgb(40%, 40%, 40%);
|
||||||
|
}
|
||||||
|
#prestart-controls {
|
||||||
|
width: 800px;
|
||||||
|
height: 600px;
|
||||||
|
text-align: center;
|
||||||
|
align-content: center;
|
||||||
|
}
|
||||||
|
button {
|
||||||
|
font-size: 20px;
|
||||||
|
text-shadow: 0.2em 0.2em 0px rgba(0, 0, 0, 75%);
|
||||||
|
padding: 1em;
|
||||||
|
border-radius: 2em;
|
||||||
|
border-style: solid;
|
||||||
|
border-color: black;
|
||||||
|
color: rgb(90%, 90%, 90%);
|
||||||
|
background-color: rgb(15%, 15%, 15%);
|
||||||
|
}
|
||||||
|
button:hover {
|
||||||
|
background-color: rgb(25%, 25%, 25%);
|
||||||
|
border-color: rgb(90%, 90%, 90%);
|
||||||
}
|
}
|
||||||
main {
|
main {
|
||||||
margin-left: auto;
|
margin-left: auto;
|
||||||
@@ -18,11 +49,12 @@
|
|||||||
width: 70%;
|
width: 70%;
|
||||||
}
|
}
|
||||||
table {
|
table {
|
||||||
|
margin-bottom: 10px;
|
||||||
border-collapse: collapse;
|
border-collapse: collapse;
|
||||||
}
|
}
|
||||||
th, td {
|
th, td {
|
||||||
border: 1px solid;
|
border: 1px solid;
|
||||||
padding: 2px 4px;
|
padding: 0.1em 0.3em;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
@@ -30,13 +62,21 @@
|
|||||||
<h1>
|
<h1>
|
||||||
Robert's Bad Asteroids Game
|
Robert's Bad Asteroids Game
|
||||||
</h1>
|
</h1>
|
||||||
<canvas id="game-canvas" width="1280" height="720"></canvas>
|
<!-- <canvas id="game-canvas" width="800" height="600"></canvas> -->
|
||||||
|
<div id="prestart-controls">
|
||||||
|
<button id="gameload-button">Load Game</button>
|
||||||
|
</div>
|
||||||
<main>
|
<main>
|
||||||
<article>
|
<article>
|
||||||
<h2>Description</h2>
|
<h2>Description</h2>
|
||||||
<p>
|
<p>
|
||||||
A (work in progress) version of the Asteroids arcade game.
|
A (work in progress) version of the Asteroids arcade game.
|
||||||
</p>
|
</p>
|
||||||
|
<p>
|
||||||
|
<em>Sound Warning!</em> The game now has sound effects, but there are no controls on the page for changing the
|
||||||
|
volume (including to mute them). You can mute the browser tab by pressing <code>ctrl</code> + <code>m</code>.
|
||||||
|
Proper volume controls are coming soon.
|
||||||
|
</p>
|
||||||
</article>
|
</article>
|
||||||
<article>
|
<article>
|
||||||
<h3>Controls</h3>
|
<h3>Controls</h3>
|
||||||
@@ -78,20 +118,34 @@
|
|||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>Program Version</td>
|
<td>Program Version</td>
|
||||||
<!-- This version text is completely unchecked. I'll need to do something about that. -->
|
<td><code>#CRATE_VERSION_PLACEHOLDER#</code></td>
|
||||||
<td><code>v0.3.0</code></td>
|
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
</article>
|
</article>
|
||||||
</main>
|
</main>
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import init from './asteroids.js'
|
import init from './asteroids.js'
|
||||||
|
|
||||||
|
let button = document.getElementById("gameload-button");
|
||||||
|
button.onclick = async function loadGame() {
|
||||||
|
console.log("Game Load button was pressed!");
|
||||||
|
let canvas = document.createElement("canvas");
|
||||||
|
// <canvas id="game-canvas" width="800" height="600"></canvas>
|
||||||
|
canvas.setAttribute("id", "game-canvas");
|
||||||
|
canvas.setAttribute("width", "800");
|
||||||
|
canvas.setAttribute("height", "600");
|
||||||
|
button.parentElement.replaceWith(canvas);
|
||||||
|
|
||||||
init().catch((error) => {
|
let compressed = await fetch("./asteroids_bg.wasm.gz")
|
||||||
if (!error.message.startsWith("Using exceptions for control flow, don't mind me. This isn't actually an error!")) {
|
let wasm_stream = compressed.body.pipeThrough(new DecompressionStream("gzip"))
|
||||||
throw error;
|
let blob = await new Response(wasm_stream).blob();
|
||||||
}
|
|
||||||
});
|
init(await blob.arrayBuffer()).catch((error) => {
|
||||||
|
if (!error.message.startsWith("Using exceptions for control flow, don't mind me. This isn't actually an error!")) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user