Skip to content
This repository has been archived by the owner on Sep 8, 2024. It is now read-only.

Commit

Permalink
WIP topdown game
Browse files Browse the repository at this point in the history
  • Loading branch information
shosanna committed Sep 19, 2023
1 parent 8ba2554 commit 3cfa00b
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
# EXAMPLE=particles
# EXAMPLE=post_processing
# EXAMPLE=shapes
EXAMPLE=ecs_sprite
# EXAMPLE=ecs_sprite
EXAMPLE=ecs_topdown_game

# default: build-examples
# default: wasm-build
Expand Down
Binary file added assets/chicken.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/grass.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
91 changes: 91 additions & 0 deletions comfy/examples/ecs_topdown_game.rs
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;
}
}

0 comments on commit 3cfa00b

Please sign in to comment.