使用 Amethyst 打造
遊戲
Make A Shoot ‘Em
Up Game with
Amethyst
Framework
葉闆 YODALEE
<LC85301@GMAIL.COM>
MOPCON 2020
1
About Me
 葉闆 (a.k.a
YodaLee)
 Software
Engineer
 Rustacean
2
@yodalee.me
Outline
 Demo
 Rust
 Rust/Amethyst
 ECS
 Rust and WebAssembly
3
Demo
 The most dangerous thing in presentation.
 You can get source code below:
4
Rust Programming
Language
@ rust-lang.org
5
What is Rust
6
 Goal: a safer system language
 First version on 2010
 Current stable version: 1.47 (10/08)
 Getting more popular
Property of Rust
7
 Compiled, Static, LLVM backend
 Strong typed
 Zero-cost abstraction, no GC
 Personal perspective: C++ like Haskell
Property of Rust
8
Shared and Mutable state is bad.
 C: Programmer is smart
 FP: No mutable
 Rust: No shared
Property of Rust
9
 Ownership, Borrow Checker, Lifetime
 Either:
 1 mutable reference to data
 Multiple immutable references to data
Ownership
10
let v = vec![1, 2, 3];
let v2 = v;
v.push(4) // compile error !
Borrow Checker
11
fn foo(v: &Vec<i32>) {
v.push(5);
}
let v = vec![];
foo(&v); // compile Error
v.push(10);
The Cost
12
Real Project
13
Nom parser
ripgrep
actix web
Clap-rs
Amethyst Framework
@ amethyst.rs
14
What is Amethyst
 A game framework in Rust
 Learned from Are we game yet
 There are many others, like Piston,
ggez, Bevy…
15
Why Amethyst
16
 Pure Rust engine, not binding.
 ECS
 Still active
 Fun
Amethyst Internal
17
World
State
Stack
Systems
Resource Entities
Components
What is ECS
18
What is ECS
19
 Entity
 Container of components
 Component
 Properties of entity
 System
 Read and modify components
What is ECS
20
Entity
What is ECS
21
Component
What is ECS
22
System
Add Entity
23
world
.create_entity()
.build()
Add Component
24
world
.create_entity()
.with(Transform)
.with(Sprite)
.build()
Add Component
25
world
.create_entity()
// other with
.with(Ship)
.build()
What is inside “Ship”
Add Component
26
world
.create_entity()
// other with
.with(Ship)
.with(Physical)
.build()
Physical
Bullet Ship
Asteroid
Add Component
27
world
.create_entity()
// other with
.with(Collider { ColliderShip })
.build()
Entity Is Container
Property Component
Position Transform
Display Sprite
Acceleration Ship
Velocity/Rotation Physical
Collision Collider
28
System
29
Control
System
Physical
Component
Input
Resource
Time
Component
Ship
Component
System
30
Movement
System
Transform
Component
Physical
Component
Time
Component
System
31
Boundary
System
Transform
Component
Transform
Component
Bullet
Component
Entity
Systems
32
Movement
System
Boundary
System
State Play
Control
System
Collision
System
let gamedata = GameDataBuilder::default()
.with(ControlSystem, "control", &[])
.with(MovementSystem, "movement", &["control"])
.with(BoundarySystem, "boundary", &["movement"])
.with(CollisionSystem, "collision", &["boundary"])
Event Channel
 Pass data between System
 Make system simpler
33
1. Detect collision
2. Delete bullet and asteroid
3. Create explosion
4. End game if asteroid hit ship
Collision
System
Event Channel
34
Detect collision
Collision
System
Delete bullet and asteroid
DeleteSystem
Create Explosion
ExplosionSystem
End game if asteroid hit ship
OverSystem
Entity
ColliderType
State
35
Just a Simple Game
 See others demo project:
 Space-menace
 Space-shooter
 Amethyst-2d-platformer-demo
36
WebAssembly
@ webassembly.org
37
WebAssembly
38
 “Assembly language” for web
 Higher performance than Javascript
 Support by Firefox, Chrome/Edge, Safari.
 C/C++ use emscripten
 Rust has target wasm32-unknown-unknown
Rust with WebAssembly
39
 Rust toolchain: wasm-pack build
 Compile Rust project to:
 Project.wasm
 Project.js
 Project.d.ts
 Package.json
Rust with WebAssembly
40
 Hello World in Rust for WASM
#[wasm_bindgen]
extern {
fn alert(s: &str);
}
#[wasm_bindgen]
pub fn greet() {
alert("Hello, wasm from Rust!");
}
Rust with WebAssembly
41
#[wasm_bindgen]
#[repr(u8)]
pub enum Cell { Dead = 0, Alive = 1, }
#[wasm_bindgen]
pub struct Universe {
width: u32, height: u32, cells: Vec<Cell>
}
// javascript
import { Universe } from "wasm-game-of-life";
const universe = Universe.new();
Rust with WebAssembly
42
 https://royaltm.github.io/rust-fractx-
wasm-demo/
 https://sandspiel.club/
 Unfortunately, unavailable for amethyst,
yet.
Takeaway
43
 Rust is the best programming
language in the world
 Concept of ECS
 The ability of WebAssembly
Reference
44
 Amethyst Doc
Not many document available : (
 Rust and WebAssembly
Q&A
45

Make A Shoot ‘Em Up Game with Amethyst Framework