Moved to neutrino

This commit is contained in:
Fredrik Jensen 2017-09-05 21:31:56 +02:00
parent 1342949fc5
commit 20d7cbb83f
7 changed files with 3692 additions and 308 deletions

View File

@ -1,13 +0,0 @@
{
"presets": [
[
"es2015",
{
"modules": false
}
]
],
"plugins": [
"external-helpers"
]
}

View File

@ -3,26 +3,17 @@
"version": "1.0.0", "version": "1.0.0",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "author": "Fredrik Jensen",
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "rollup -c -w"
},
"author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"babel-core": "^6.25.0", "lodash": "^4.17.4"
"babel-plugin-external-helpers": "^6.22.0", },
"babel-preset-es2015": "^6.24.1", "scripts": {
"lodash": "^4.17.4", "start": "neutrino start --use neutrino-preset-web",
"rollup": "^0.45.2", "build": "neutrino build --use neutrino-preset-web"
"rollup-plugin-babel": "^3.0.1",
"rollup-plugin-commonjs": "^8.1.0",
"rollup-plugin-livereload": "^0.4.0",
"rollup-plugin-node-resolve": "^3.0.0",
"rollup-plugin-serve": "^0.4.0",
"rollup-watch": "^4.3.1"
}, },
"devDependencies": { "devDependencies": {
"babel-preset-env": "^1.6.0" "neutrino": "^6.1.5",
"neutrino-preset-web": "^6.1.5"
} }
} }

View File

@ -1,22 +0,0 @@
import babel from 'rollup-plugin-babel';
import serve from 'rollup-plugin-serve';
import livereload from 'rollup-plugin-livereload';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
export default {
entry: 'src/index.js',
format: 'cjs',
plugins: [
babel({
exclude: 'node_modules/**',
}),
serve(),
resolve(),
livereload('build'),
commonjs({
include: 'node_modules/**',
}),
],
dest: 'build/bundle.js',
};

View File

@ -1,11 +1,12 @@
import _ from 'lodash'; import { random, find } from 'lodash';
import config from './config'; import config from './config';
import { createDivElement, setDivColor, drawLetter } from './utils/div';
import { createDivElement } from './utils/create-div-element'; import { S, N, A, K, E } from './utils/letters';
const { dimensions, unit, snake, food } = config; const { dimensions, unit, snake, food } = config;
let GAME_STARTED = false; let GAME_STARTED = false;
let GAME_RUNNING;
/** /**
* Draw the board * Draw the board
@ -36,15 +37,11 @@ for (let i = 0; i < dimensions.height; i++) {
* Draw the snake * Draw the snake
*/ */
const setBrickColor = (color, x, y) => {
((document.getElementById(`${x}.${y}`) || {}).style || {}).background = color;
};
const showSnake = () => { const showSnake = () => {
for(var i = 0; i < snake.body; i++) { for(var i = 0; i < snake.body; i++) {
const logEvent = snake.log[snake.log.length - (1 + i)]; const logEvent = snake.log[snake.log.length - (1 + i)];
setBrickColor( setDivCOlor(
'yellow', 'yellow',
(logEvent || {}).x, (logEvent || {}).x,
(logEvent || {}).y, (logEvent || {}).y,
@ -56,7 +53,7 @@ const hideSnake = () => {
for(var i = 0; i < snake.body; i++) { for(var i = 0; i < snake.body; i++) {
const logEvent = snake.log[snake.log.length - (1 + snake.body + i)]; const logEvent = snake.log[snake.log.length - (1 + snake.body + i)];
setBrickColor( setDivCOlor(
'#000', '#000',
(logEvent || {}).x, (logEvent || {}).x,
(logEvent || {}).y, (logEvent || {}).y,
@ -148,19 +145,16 @@ document.onkeydown = function(e) {
*/ */
const makeFood = () => { const makeFood = () => {
const x = _.random(1, config.dimensions.width); const x = random(1, config.dimensions.width);
const y = _.random(1, config.dimensions.height); const y = random(1, config.dimensions.height);
if (_.find(snakeBody, { x, y })) { if (find(snakeBody, { x, y })) {
console.log('food placed under the snake...'); console.log('food placed under the snake...');
/**
* Food was placed under the snake
*/
return makeFood();
/** /**
* So we made food again... yolo * So we made food again... yolo
*/ */
return makeFood();
} }
food.x = x; food.x = x;
@ -169,18 +163,26 @@ const makeFood = () => {
const snakeLength = snake.log.length - 1; const snakeLength = snake.log.length - 1;
const snakeBody = snake.log.slice((snakeLength - snake.body), snakeLength); const snakeBody = snake.log.slice((snakeLength - snake.body), snakeLength);
setBrickColor('red', x, y); setDivCOlor('red', x, y);
}; };
/** /**
* Check if snake is eating * Check if snake is eating
*/ */
const checkIfSnakeIsEating = () => { const checkIfSnakeIsEating = (snake, food) => {
if (snake.x === food.x && snake.y === food.y) { if (snake.x === food.x && snake.y === food.y) {
makeFood();
/**
* Make snake larger and go faster
*/
snake.body = snake.body + 1; snake.body = snake.body + 1;
snake.speed = snake.speed + 23; snake.speed = snake.speed + 100;
/**
* Put out new food
*/
makeFood();
} }
}; };
@ -188,24 +190,34 @@ const checkIfSnakeIsEating = () => {
* Check collision * Check collision
*/ */
let GAME_RUNNING; const checkCollision = ({ x, y }) => {
const checkCollision = () => {
const snakeLength = snake.log.length - 2; const snakeLength = snake.log.length - 2;
const snakeBody = snake.log.slice((snakeLength - snake.body), snakeLength); const snakeBody = snake.log.slice((snakeLength - snake.body), snakeLength);
const snakeHead = { x: snake.x, y: snake.y };
if (_.find(snakeBody, snakeHead)) { if (find(snakeBody, { x, y })) {
GAME_STARTED = false; GAME_STARTED = false;
clearInterval(GAME_RUNNING); clearInterval(GAME_RUNNING);
startScreen(); startScreen();
} }
} }
/**
* Start screen
*/
const startScreen = () => {
S.forEach(drawLetter);
N.forEach(drawLetter);
A.forEach(drawLetter);
K.forEach(drawLetter);
E.forEach(drawLetter);
}
/** /**
* Run game * Run game
*/ */
const startGame = () => { const startGame = () => {
GAME_STARTED = true; GAME_STARTED = true;
makeFood(); makeFood();
@ -213,41 +225,11 @@ const startGame = () => {
GAME_RUNNING = setInterval(() => { GAME_RUNNING = setInterval(() => {
console.log('running'); console.log('running');
checkCollision(); checkCollision(snake);
moveSnake(snake.direction); moveSnake(snake.direction);
checkIfSnakeIsEating(); checkIfSnakeIsEating(snake, food);
}, snake.speed); }, snake.speed);
}; };
const drawLetter = (letter) => {
setBrickColor('#333', letter.toString().split('.')[0], letter.toString().split('.')[1]);
};
const startScreen = () => {
// S
[2.2, 2.3, 2.4, 2.5, 3.2, 4.3, 5.4, 6.5, 7.5, 7.4, 7.3, 7.2]
.forEach(drawLetter);
// N
[2.7, 3.7, 4.7, 5.7, 6.7, 7.7, 3.8, 4.9, 5.9, 6.11, 7.11, 6.11, 5.11, 4.11, 3.11, 2.11]
.forEach(drawLetter);
setBrickColor('#333', 6, 10);
// A
[2.13, 3.13, 4.13, 5.13, 6.13, 7.13, 2.14, 2.15, 2.16, 2.17, 3.17, 4.17, 5.17, 6.17, 7.17, 5.16, 5.15, 5.14]
.forEach(drawLetter);
// K
[2.19, 3.19, 4.19, 5.19, 6.19, 7.19, 6.21, 7.22, 3.21, 2.22]
.forEach(drawLetter);
setBrickColor('#333', 5, 20);
setBrickColor('#333', 4, 20);
// E
[2.24, 3.24, 4.24, 5.24, 6.24, 7.24, 2.25, 2.26, 2.27, 2.28, 7.25, 7.26, 7.27, 7.28, 4.25]
.forEach(drawLetter);
}
startScreen(); startScreen();

View File

@ -12,4 +12,20 @@ export const createDivElement = (name, styles, target) => {
} }
return document.getElementById(target).appendChild(window[name]); return document.getElementById(target).appendChild(window[name]);
};
/**
* Set the color of a div in the Grid
*/
export const setDivColor = (color, x, y) => {
((document.getElementById(`${x}.${y}`) || {}).style || {}).background = color;
};
/**
* Draw charchter
*/
export const drawLetter = (char) => {
setDivColor('#333', char.toString().split('.')[0], char.toString().split('.')[1]);
}; };

9
src/utils/letters.js Normal file
View File

@ -0,0 +1,9 @@
/**
* Letters
*/
export const S = [2.2, 2.3, 2.4, 2.5, 3.2, 4.3, 5.4, 6.5, 7.5, 7.4, 7.3, 7.2];
export const N = [2.7, 3.7, 4.7, 5.7, 6.7, 7.7, 3.8, 4.9, 5.9, 6.10, 6.11, 7.11, 6.11, 5.11, 4.11, 3.11, 2.11];
export const A = [2.13, 3.13, 4.13, 5.13, 6.13, 7.13, 2.14, 2.15, 2.16, 2.17, 3.17, 4.17, 5.17, 6.17, 7.17, 5.16, 5.15, 5.14];
export const K = [2.19, 3.19, 4.19, 4.20, 5.19, 5.20, 6.19, 7.19, 6.21, 7.22, 3.21, 2.22];
export const E = [2.24, 3.24, 4.24, 5.24, 6.24, 7.24, 2.25, 2.26, 2.27, 2.28, 7.25, 7.26, 7.27, 7.28, 4.25];

3817
yarn.lock

File diff suppressed because it is too large Load Diff