Compare commits

..

4 Commits

6 changed files with 65 additions and 9 deletions

View File

@@ -11,20 +11,23 @@ jobs:
strategy:
fail-fast: false
matrix:
toolchain: [1.88.0]
include:
- os: ubuntu-latest
target: x86_64-unknown-linux-gnu
artifact_name: pacman
toolchain: 1.88.0
- os: macos-13
target: x86_64-apple-darwin
artifact_name: pacman
toolchain: 1.88.0
- os: macos-latest
target: aarch64-apple-darwin
artifact_name: pacman
toolchain: 1.88.0
- os: windows-latest
target: x86_64-pc-windows-gnu
artifact_name: pacman.exe
toolchain: 1.88.0
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
@@ -90,7 +93,7 @@ jobs:
uses: pyodide/setup-emsdk@v15
with:
version: 3.1.43
actions-cache-folder: "emsdk-cache"
actions-cache-folder: "emsdk-cache-b"
- name: Setup Rust (WASM32 Emscripten)
uses: dtolnay/rust-toolchain@master

View File

@@ -47,12 +47,13 @@ jobs:
- name: Generate coverage report
run: |
cargo tarpaulin \
--out Html \
--out Lcov \
--output-dir coverage \
--rustflags="-C link-arg=-lz"
- name: Upload coverage to Coveralls
uses: coverallsapp/github-action@v2
with:
files: ./coverage/tarpaulin-report.html
files: ./coverage/lcov.info
format: lcov
allow-empty: false

View File

@@ -1,13 +1,13 @@
use glam::Vec2;
use glam::{UVec2, Vec2};
use crate::constants::BOARD_PIXEL_OFFSET;
use crate::entity::direction::Direction;
use crate::entity::graph::{Graph, NodeId, Position, Traverser};
use crate::helpers::centered_with_size;
use crate::texture::animated::AnimatedTexture;
use crate::texture::directional::DirectionalAnimatedTexture;
use crate::texture::sprite::SpriteAtlas;
use sdl2::keyboard::Keycode;
use sdl2::rect::Rect;
use sdl2::render::{Canvas, RenderTarget};
use std::collections::HashMap;
@@ -71,15 +71,14 @@ impl Pacman {
Position::BetweenNodes { from, to, traversed } => {
let from_pos = graph.get_node(from).unwrap().position;
let to_pos = graph.get_node(to).unwrap().position;
let weight = from_pos.distance(to_pos);
from_pos.lerp(to_pos, traversed / weight)
from_pos.lerp(to_pos, traversed / from_pos.distance(to_pos))
}
}
}
pub fn render<T: RenderTarget>(&self, canvas: &mut Canvas<T>, atlas: &mut SpriteAtlas, graph: &Graph) {
let pixel_pos = self.get_pixel_pos(graph).round().as_ivec2() + BOARD_PIXEL_OFFSET.as_ivec2();
let dest = Rect::new(pixel_pos.x - 8, pixel_pos.y - 8, 16, 16);
let dest = centered_with_size(pixel_pos, UVec2::new(16, 16));
let is_stopped = self.traverser.position.is_stopped();
if is_stopped {

51
src/helpers.rs Normal file
View File

@@ -0,0 +1,51 @@
use glam::{IVec2, UVec2};
use sdl2::rect::Rect;
pub fn centered_with_size(pixel_pos: IVec2, size: UVec2) -> Rect {
Rect::new(
pixel_pos.x - size.x as i32 / 2,
pixel_pos.y - size.y as i32 / 2,
size.x,
size.y,
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_centered_with_size_basic() {
let rect = centered_with_size(IVec2::new(100, 100), UVec2::new(50, 30));
assert_eq!(rect.origin(), (75, 85));
assert_eq!(rect.size(), (50, 30));
}
#[test]
fn test_centered_with_size_odd_dimensions() {
let rect = centered_with_size(IVec2::new(50, 50), UVec2::new(51, 31));
assert_eq!(rect.origin(), (25, 35));
assert_eq!(rect.size(), (51, 31));
}
#[test]
fn test_centered_with_size_zero_position() {
let rect = centered_with_size(IVec2::new(0, 0), UVec2::new(100, 100));
assert_eq!(rect.origin(), (-50, -50));
assert_eq!(rect.size(), (100, 100));
}
#[test]
fn test_centered_with_size_negative_position() {
let rect = centered_with_size(IVec2::new(-100, -50), UVec2::new(80, 40));
assert_eq!(rect.origin(), (-140, -70));
assert_eq!(rect.size(), (80, 40));
}
#[test]
fn test_centered_with_size_large_dimensions() {
let rect = centered_with_size(IVec2::new(1000, 1000), UVec2::new(1000, 1000));
assert_eq!(rect.origin(), (500, 500));
assert_eq!(rect.size(), (1000, 1000));
}
}

View File

@@ -7,5 +7,6 @@ pub mod constants;
pub mod emscripten;
pub mod entity;
pub mod game;
pub mod helpers;
pub mod map;
pub mod texture;

View File

@@ -56,6 +56,7 @@ mod constants;
mod emscripten;
mod entity;
mod game;
mod helpers;
mod map;
mod texture;