Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CI #3

Merged
merged 3 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
on:
push:
branches:
- "*"
tags:
- "!*"
name: CI
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: taiki-e/install-action@just

- uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
components: clippy rustfmt

- name: linting
run: |
cargo clippy --workspace --all-features -- -D warnings
cargo fmt --all -- --check

- name: Debug builds
run: cargo build
61 changes: 61 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
name: Release
on:
push:
tags:
- v[0-9]+.*
jobs:
build:
permissions:
contents: write
continue-on-error: false
strategy:
matrix:
include:
- target: x86_64-unknown-linux-gnu
os: ubuntu-latest
- target: x86_64-unknown-linux-musl
os: ubuntu-latest
- target: aarch64-apple-darwin
os: macos-latest
- target: x86_64-apple-darwin
os: macos-latest
- target: x86_64-pc-windows-msvc
os: windows-latest
- target: aarch64-pc-windows-msvc
os: windows-latest
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Install Target
uses: dtolnay/rust-toolchain@stable
with:
targets: ${{ matrix.target }}

- name: Install musl
if: matrix.target == 'x86_64-unknown-linux-musl'
run: |
sudo apt update
sudo apt install -y musl-tools gcc

- name: Build
run: cargo build --release --target ${{ matrix.target }}

- name: Strip Binary (linux)
if: matrix.target == 'x86_64-unknown-linux-gnu' || matrix.target == 'x86_64-unknown-linux-musl'
run: strip ./target/${{ matrix.target }}/release/ratatui-snake

- name: Rename Binary (unix)
if: runner.os != 'Windows'
run: mv ./target/${{ matrix.target }}/release/ratatui-snake ./ratatui-snake-${{ matrix.target }}

- name: Rename Binary (windows)
if: runner.os == 'Windows'
run: mv ./target/${{ matrix.target }}/release/ratatui-snake.exe ./ratatui-snake-${{ matrix.target }}.exe

- name: Upload Binary
uses: softprops/action-gh-release@v2
with:
files: "ratatui-snake*"
21 changes: 15 additions & 6 deletions src/coord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ pub struct Coord {

impl Coord {
pub fn new(x: f64, y: f64) -> Self {
Coord {
x,
y,
}
Coord { x, y }
}

pub fn move_left(&mut self, speed: f64) {
Expand Down Expand Up @@ -44,8 +41,20 @@ impl Coord {
}

pub fn move_toward(&mut self, c: &Coord) {
let x_diff = if self.x < c.x { 1.0 } else if self.x > c.x { -1.0 } else { 0.0 };
let y_diff = if self.y < c.y { 1.0 } else if self.y > c.y { -1.0 } else { 0.0 };
let x_diff = if self.x < c.x {
1.0
} else if self.x > c.x {
-1.0
} else {
0.0
};
let y_diff = if self.y < c.y {
1.0
} else if self.y > c.y {
-1.0
} else {
0.0
};

self.x += x_diff;
self.y += y_diff;
Expand Down
14 changes: 9 additions & 5 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ impl Game {

pub fn increase_frame_num(&mut self) {
self.frame_num += {
if self.frame_num > 10 / (self.speed as i32) { -self.frame_num } else { 1 }
if self.frame_num > 10 / (self.speed as i32) {
-self.frame_num
} else {
1
}
};
}

Expand Down Expand Up @@ -91,7 +95,8 @@ impl Game {

pub fn change_direction(&mut self, direction: Direction) {
self.direction = direction;
self.corners.push(Coord::new(self.head_coord.x, self.head_coord.y))
self.corners
.push(Coord::new(self.head_coord.x, self.head_coord.y))
}

pub fn restart(&mut self) {
Expand All @@ -115,9 +120,8 @@ impl Game {
let end = &pair[1];

// check if the head coord is between start and end
if
(self.head_coord.x - start.x) * (self.head_coord.x - end.x) <= 0.0 &&
(self.head_coord.y - start.y) * (self.head_coord.y - end.y) <= 0.0
if (self.head_coord.x - start.x) * (self.head_coord.x - end.x) <= 0.0
&& (self.head_coord.y - start.y) * (self.head_coord.y - end.y) <= 0.0
{
self.state = GameState::GameOver;
}
Expand Down
Loading