Skip to content
Open
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
23 changes: 18 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ impl Vector {
self.x * other.x + self.y * other.y
}
}
#[derive(Copy, Clone)]
#[wasm_bindgen]
pub struct Food{
pub coord: Vector,
pub food_type: i8,
}

pub struct Segment<'a> {
pub start: &'a Vector,
Expand All @@ -97,6 +103,12 @@ impl<'a> Segment<'a> {
are_equal(self.length(), first.length() + second.length())
}

pub fn is_point_inside_food(&self, food: &Food) -> bool {
let first = Segment::new(self.start, &food.coord);
let second = Segment::new(&food.coord, self.end);
are_equal(self.length(), first.length() + second.length())
}

pub fn get_projected_point(&self, point: &Vector) -> Vector {
let vector = self.get_vector();
let diff = point.subtract(&self.start);
Expand All @@ -113,7 +125,7 @@ fn get_segments_from_vectors(vectors: &[Vector]) -> Vec<Segment> {
.collect::<Vec<Segment>>()
}

fn get_food(width: i32, height: i32, snake: &[Vector]) -> Vector {
fn get_food(width: i32, height: i32, snake: &[Vector]) -> Food {
let segments = get_segments_from_vectors(snake);
let mut free_positions: Vec<Vector> = Vec::new();
for x in 0..width {
Expand All @@ -125,7 +137,7 @@ fn get_food(width: i32, height: i32, snake: &[Vector]) -> Vector {
}
}
let index = rand::thread_rng().gen_range(0, free_positions.len());
free_positions[index]
Food{coord: free_positions[index], food_type: rand::thread_rng().gen_range(0, 6) % 4}
}

#[wasm_bindgen]
Expand All @@ -143,7 +155,7 @@ pub struct Game {
pub speed: f64,
snake: Vec<Vector>,
pub direction: Vector,
pub food: Vector,
pub food: Food,
pub score: i32,
}

Expand Down Expand Up @@ -268,14 +280,15 @@ impl Game {
let snake_len = self.snake.len();
let head_segment = Segment::new(&self.snake[snake_len - 2], &self.snake[snake_len - 1]);

if head_segment.is_point_inside(&self.food) {
if head_segment.is_point_inside_food(&self.food) {
let tail_end = &self.snake[0];
let before_tail_end = &self.snake[1];
let tail_segment = Segment::new(before_tail_end, &tail_end);
let new_tail_end = tail_end.add(&tail_segment.get_vector().normalize());
self.snake[0] = new_tail_end;
self.score += self.food.food_type as i32 + 1;
self.food = get_food(self.width, self.height, &self.snake);
self.score += 1;

}
}

Expand Down
4 changes: 2 additions & 2 deletions www/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"create-wasm-app": ".bin/create-wasm-app.js"
},
"scripts": {
"build": "webpack --config webpack.config.js",
"start": "webpack-dev-server",
"build": "SET NODE_OPTIONS=--openssl-legacy-provider && webpack --config webpack.config.js",
"start": "SET NODE_OPTIONS=--openssl-legacy-provider && webpack-dev-server",
"deploy": "gh-pages -d dist"
},
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion www/src/game-manager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Game, Vector } from 'wasm-snake-game'
import { Game, Vector, Food} from 'wasm-snake-game'

import CONFIG from './config'
import { View } from './view'
Expand Down
18 changes: 16 additions & 2 deletions www/src/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ export class View {
)
this.context.globalAlpha = 1

const projectedFood = this.projectPosition(food)
const projectedFood = this.projectPosition(food.coord)

this.context.beginPath()
this.context.arc(
projectedFood.x,
Expand All @@ -67,7 +68,20 @@ export class View {
0,
2 * Math.PI
)
this.context.fillStyle = '#e74c3c'
switch(food.food_type){
case 0:
this.context.fillStyle = '#e74c3c';
break;
case 1:
this.context.fillStyle = 'green';
break;
case 2:
this.context.fillStyle = 'white';
break;
case 3:
this.context.fillStyle = 'yellow';
break;
}
this.context.fill()

this.context.lineWidth = this.unitOnScreen
Expand Down