Skip to content

Instantly share code, notes, and snippets.

View nicolasdelfino's full-sized avatar
💼

Nicolás Delfino nicolasdelfino

💼
View GitHub Profile
@nicolasdelfino
nicolasdelfino / rfr-thunk
Created July 11, 2017 22:06
Picture router thunk
HOME: '/',
ABOUT: '/about',
PICTURES: {
path: '/pictures',
thunk: (dispatch, getState) => {
fetch(`https://jsonplaceholder.typicode.com/photos`)
.then(payload => {
return payload.json();
})
.then(data => {
@nicolasdelfino
nicolasdelfino / redux-first-router
Created June 16, 2017 18:37
redux-first-router
LIST: {
path: '/list/:category',
thunk: (dispatch, getState) => {
const { category } = getState().location.payload
fetchData(`/api/videos/${category}`)
.then(payload => dispatch({ type: 'VIDEOS_FETCHED', payload }))
}
},
@nicolasdelfino
nicolasdelfino / collisions.js
Last active April 9, 2017 20:38
Roadkill collisions Retanx
let collideUnit
movingUnits.forEach((unit, index) => {
movingUnits.forEach((compare, index) => {
if(unit.active
&& !compare.active
&& compare.id !== unit.id
&& compare.x === unit.x
&& compare.y === unit.y) {
collideUnit = compare
}
@nicolasdelfino
nicolasdelfino / unittracker.js
Last active April 14, 2017 22:44
Tracking mechanism for Retanx
liveUnits.forEach((unit, index) => {
let element = document.querySelectorAll('#unit_' + unit.id + ' .position')
let rect = element[0].getBoundingClientRect();
let offsetTop = rect.top + document.body.scrollTop
let offsetLeft = rect.left + document.body.scrollLeft
let xx = Math.floor(offsetLeft) - Math.floor(mainX)
let yy = Math.floor(offsetTop) - Math.floor(mainY)
//https://en.wikipedia.org/wiki/Rounding#Round_half_to_even
let x = getBankersRounding(xx / 100)
@nicolasdelfino
nicolasdelfino / map.js
Last active April 9, 2017 21:36
A basic tile map for Retanx
export const Map = function() {
this.getTiles = () => {
return [
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 0, 0, 0, 0, 0, 0, 0, 0, 2,
2, 1, 0, 0, 0, 2, 1, 0, 0, 2,
0, 0, 0, 0, 0, 0, 1, 0, 0, 2,
0, 0, 0, 0, 1, 1, 1, 0, 0, 2,
0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
2, 2, 0, 2, 2, 2, 0, 0, 0, 2,
@nicolasdelfino
nicolasdelfino / astar.js
Last active February 3, 2018 13:38
AStar algorithm
export let AStar = (_grid, start, end, units, currentSelectionID) => {
// Reset score of cells
_grid.resetCells()
// pass tanks to grid and make them obstacles
_grid.makeObstaclesOfUnitsWithHigherMass(units[currentSelectionID], units, currentSelectionID)
// Add possible cell neighbors, pass unit A* style
let aStarStyle = units[currentSelectionID].aStarStyle
_grid.addCellNeighbors(aStarStyle)
let openSet = []
let closedSet = []
@nicolasdelfino
nicolasdelfino / grid.js
Last active April 17, 2017 20:03
Grid singleton
import { Cell } from './Cell'
import { Map } from './maps/Map'
// Grid singleton
export const Dimensions = () => ({
width: 1000,
height: 2000,
tileSize: 100
})
@nicolasdelfino
nicolasdelfino / tank.js
Last active April 9, 2017 21:00
Tank component hierarchy
<BasePosition moveSpeed={tankUnit.moveSpeed} position={this.coordinates(position, cellWidth, cellHeight)} >
<Body specs={tankUnit} speed={this.getSpeed(position)} rotate={shouldRotate} rotation={angle}>
<Tracks specs={tankUnit}/>
</Body>
<Cannon debugAim={this.props.aimMode}
specs={tankUnit} rotate={shouldRotate}
rotation={angle}
shooting={this.getIsThisUnitShooting(tankUnit, index)}/>
<HP specs={tankUnit} />
</BasePosition>