This repository has been archived by the owner on Sep 8, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
use comfy::*; | ||
|
||
example_game!("ECS Topdown Game", setup, update); | ||
|
||
struct Player; | ||
struct Grass; | ||
|
||
fn setup(c: &mut EngineContext) { | ||
// Load the grass texture | ||
c.load_texture_from_bytes( | ||
"grass", | ||
include_bytes!(concat!( | ||
env!("CARGO_MANIFEST_DIR"), | ||
"/../assets/grass.png" | ||
)), | ||
wgpu::AddressMode::ClampToEdge, | ||
); | ||
|
||
// Load the player texture | ||
c.load_texture_from_bytes( | ||
"player", | ||
include_bytes!(concat!( | ||
env!("CARGO_MANIFEST_DIR"), | ||
"/../assets/chicken.png" | ||
)), | ||
wgpu::AddressMode::ClampToEdge, | ||
); | ||
|
||
for x in 0..30 { | ||
for y in 0..30 { | ||
let variant = random_i32(0, 2); | ||
// Tile the world with random variant of grass sprite | ||
c.commands().spawn(( | ||
Sprite::new("grass".to_string(), vec2(1.0, 1.0), 0, WHITE) | ||
.with_rect(32 * variant, 0, 32, 32), | ||
Transform::position(vec2(x as f32, y as f32)), | ||
Grass, | ||
)); | ||
} | ||
} | ||
|
||
// Spawn the player entity and make sure z-index is above the grass | ||
c.commands().spawn(( | ||
Transform::position(vec2(10.0, 10.0)), | ||
Player, | ||
AnimatedSprite::spritesheet( | ||
"player", | ||
Spritesheet { rows: 1, columns: 28 }, | ||
0.1, | ||
true, | ||
10, | ||
splat(1.0), | ||
WHITE, | ||
splat(0.0), | ||
Box::new(|_| {}), | ||
), | ||
)); | ||
} | ||
|
||
fn update(c: &mut EngineContext) { | ||
let dt = c.delta; | ||
|
||
for (entity, (player, animated_sprite, mut transform)) in c | ||
.world() | ||
.query::<(&Player, &mut AnimatedSprite, &mut Transform)>() | ||
.iter() | ||
{ | ||
// Handle movement and animation | ||
let mut moved = false; | ||
let speed = 1.0; | ||
|
||
if is_key_down(KeyCode::W) { | ||
transform.position.y += speed * dt; | ||
moved = true; | ||
} | ||
if is_key_down(KeyCode::S) { | ||
transform.position.y -= speed * dt; | ||
moved = true; | ||
} | ||
if is_key_down(KeyCode::A) { | ||
transform.position.x -= speed * dt; | ||
moved = true; | ||
} | ||
if is_key_down(KeyCode::D) { | ||
transform.position.x += speed * dt; | ||
moved = true; | ||
} | ||
|
||
main_camera_mut().center = transform.position; | ||
} | ||
} |