Skip to content

Commit

Permalink
Hopefully fixenemy view Y sorting
Browse files Browse the repository at this point in the history
and release first "version" lol
  • Loading branch information
martinstarman committed Sep 15, 2023
1 parent d31161b commit 0ec3228
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
edition = "2021"
name = "rtte"
version = "0.0.1"
version = "0.1.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
16 changes: 16 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.1.0] - 2023-09-15

### Added

- keyboard/mouse panning
- entity drawing with Y sorting
- entity selection
- entity movement
- enemy view
11 changes: 0 additions & 11 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,3 @@ I'm not game developer nor Rust developer.
- ```Arrow keys/mouse on window edge``` - panning
- ```LMB``` - select player/move player
- ```RMB``` - stop player movement

## TODO
- [x] panning
- [x] single player selection
- [x] Y sorting
- [ ] multiple player selection
- [ ] enemy POV
- [ ] enemy POV Y sorting
- [ ] pathfinding
- [ ] mission editor
- [ ] cooperation
39 changes: 23 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl Game {
});

world.spawn(EnemyBundle {
position: Position { x: 200., y: 370. },
position: Position { x: 450., y: 370. },
size: Size { w: 10., h: 23. },
renderable: Renderable {
sprite: Image::from_path(ctx, "/player.png").unwrap(), // TODO: enemy.png
Expand All @@ -125,7 +125,7 @@ impl Game {
},
view: View {
points: vec![],
x: 350.,
x: 600.,
y: 370.,
},
enemy: Enemy {
Expand Down Expand Up @@ -194,12 +194,15 @@ impl EventHandler<GameError> for Game {
// Reset canvas.
let mut canvas = Canvas::from_frame(ctx, Color::from_rgb(255, 0, 255));

// Draw entities.
draw_entity(self, ctx, &mut canvas);
// Draw non Y indexed entities.
draw_entity(self, ctx, &mut canvas, false);

// Draw view.
draw_view(self, ctx, &mut canvas);

// Draw Y indexed entities.
draw_entity(self, ctx, &mut canvas, true);

// Draw debug stuff.
if DEBUG {
draw_entity_debug(self, ctx, &mut canvas);
Expand Down Expand Up @@ -243,29 +246,33 @@ impl EventHandler<GameError> for Game {
}

/// Draw entity.
fn draw_entity(game: &mut Game, _ctx: &mut Context, canvas: &mut Canvas) {
fn draw_entity(game: &mut Game, _ctx: &mut Context, canvas: &mut Canvas, y_indexed: bool) {
let mut query = game.world.query::<(&Position, &Size, &Renderable)>();
let mut entities: Vec<_> = query.iter_mut(&mut game.world).collect();
let mut entities: Vec<_> = query
.iter_mut(&mut game.world)
.filter(|(_, _, renderable)| renderable.y_indexed == y_indexed)
.collect();

// Sort by Y index.
entities.sort_by(|(a_position, a_size, a_renderable), (b_position, b_size, b_renderable)| {
let a_y_index = if a_renderable.y_indexed { a_position.y + a_size.h } else { 0. };
let b_y_index = if b_renderable.y_indexed { b_position.y + b_size.h } else { 0. };

(a_y_index).partial_cmp(&(&b_y_index)).unwrap()
});
if y_indexed {
entities.sort_by(|(a_pos, a_size, _), (b_pos, b_size, _)| {
(a_pos.y + a_size.h).partial_cmp(&(b_pos.y + b_size.h)).unwrap()
});
}

for (position, _, renderable) in entities {
let dest = Point2 {
x: position.x - game.camera.x,
y: position.y - game.camera.y,
};

canvas.draw(&renderable.sprite, DrawParam::new().dest(dest));
}
}

/// Draw view.
fn draw_view(game: &mut Game, ctx: &mut Context, canvas: &mut Canvas) {
// TODO: show view only for 1 selected enemy
let mut query = game.world.query::<&View>();

for view in query.iter_mut(&mut game.world) {
Expand Down Expand Up @@ -396,12 +403,12 @@ fn movement(mut query: Query<(&mut Movable, &mut Position)>) {
}

/// Entity view.
// TODO: Update view position when enemy position change. Use bevy ecs change detection.
fn view(mut query: Query<(&mut View, &Position)>, query2: Query<(&Object, &Position)>) {
// TODO: update view position when enemy position change. Use bevy ecs change detection.
fn view(query1: Query<(&Object, &Position)>, mut query2: Query<(&mut View, &Position)>, ) {
// Build barriers from objects.
let mut barriers: Vec<(Point2<f32>, Point2<f32>)> = vec![];

for (object, position) in &query2 {
for (object, position) in &query1 {
if object.poly.len() >= 3 {
for i in 0..object.poly.len() - 1 {
let curr = object.poly[i];
Expand Down Expand Up @@ -438,7 +445,7 @@ fn view(mut query: Query<(&mut View, &Position)>, query2: Query<(&Object, &Posit
// TODO: limits
// TODO: slow down
// Build view.
for (mut view, position) in &mut query {
for (mut view, position) in &mut query2 {
// Move view.
let dx = view.x - position.x;
let dy = view.y - position.y;
Expand Down

0 comments on commit 0ec3228

Please sign in to comment.