Skip to content

Instantly share code, notes, and snippets.

View radzionc's full-sized avatar

Radzion Chachura radzionc

View GitHub Profile
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 {
// ...
import Storage from './storage'
export class GameManager {
// ...
render() {
this.view.render(
this.game.food,
this.game.get_snake(),
this.game.score,
export default {
getBestScore: () => parseInt(localStorage.bestScore) || 0,
setBestScore: (bestScore) => localStorage.setItem('bestScore', bestScore)
}
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);
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()
import { Controller } from './controller'
export class GameManager {
constructor() {
// ...
this.controller = new Controller(
this.onStop.bind(this)
)
}
}
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
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,
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);
}
}
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))
}