This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
impl Vector { | |
// ... | |
pub fn dot_product(&self, other: &Vector) -> f64 { | |
self.x * other.x + self.y * other.y | |
} | |
} | |
impl<'a> Segment<'a> { | |
// ... | |
pub fn get_projected_point(&self, point: &Vector) -> Vector { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
import Storage from './storage' | |
export class GameManager { | |
// ... | |
render() { | |
this.view.render( | |
this.game.food, | |
this.game.get_snake(), | |
this.game.score, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default { | |
getBestScore: () => parseInt(localStorage.bestScore) || 0, | |
setBestScore: (bestScore) => localStorage.setItem('bestScore', bestScore) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
impl Game { | |
// ... | |
fn process_food(&mut self) { | |
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) { | |
let tail_end = &self.snake[0]; | |
let before_tail_end = &self.snake[1]; | |
let tail_segment = Segment::new(before_tail_end, &tail_end); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class GameManager { | |
// ... | |
tick() { | |
if (!this.stopTime) { | |
const lastUpdate = Date.now() | |
if (this.lastUpdate) { | |
this.game.process(lastUpdate - this.lastUpdate, this.controller.movement) | |
} | |
this.lastUpdate = lastUpdate | |
this.render() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Controller } from './controller' | |
export class GameManager { | |
constructor() { | |
// ... | |
this.controller = new Controller( | |
this.onStop.bind(this) | |
) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Movement } from "wasm-snake-game"; | |
const MOVEMENT_KEYS = { | |
[Movement.TOP]: [87, 38], | |
[Movement.RIGHT]: [68, 39], | |
[Movement.DOWN]: [83, 40], | |
[Movement.LEFT]: [65, 37] | |
} | |
const STOP_KEY = 32 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
impl Game { | |
// ... | |
fn process_movement(&mut self, timespan: f64, movement: Option<Movement>) { | |
// ... | |
let new_head = old_head.add(&self.direction.scale_by(distance)); | |
if movement.is_some() { | |
let new_direction = match movement.unwrap() { | |
Movement::TOP => Vector { | |
x: 0_f64, | |
y: -1_f64, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
impl Game { | |
fn process_movement(&mut self, timespan: f64, movement: Option<Movement>) { | |
// ... | |
} | |
pub fn process(&mut self, timespan: f64, movement: Option<Movement>) { | |
self.process_movement(timespan, movement); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
impl Vector { | |
// ... | |
pub fn equal_to(&self, other: &Vector) -> bool { | |
are_equal(self.x, other.x) && are_equal(self.y, other.y) | |
} | |
pub fn is_opposite(&self, other: &Vector) -> bool { | |
let sum = self.add(other); | |
sum.equal_to(&Vector::new(0_f64, 0_f64)) | |
} |