-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Benoit Zohar
committed
Oct 19, 2016
1 parent
c525981
commit 6eafb0a
Showing
12 changed files
with
137 additions
and
48 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
import GridModel from '../grid/grid-model' | ||
class GameModel { | ||
|
||
export default class GameModel { | ||
|
||
constructor() { | ||
this.grid = new GridModel() | ||
constructor(GridModel) { | ||
this.grid = GridModel() | ||
|
||
} | ||
|
||
addRandomTiles(count) { | ||
console.log("[debug] count", count); | ||
this.grid.addRandomTiles(count); | ||
this.grid.addRandomTiles(count) | ||
} | ||
} | ||
|
||
//This part is necessary to use our model class as a factory | ||
export default ['GridModel', (GridModel) => { | ||
return () => new GameModel(GridModel) | ||
}] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,19 @@ | ||
import angular from 'angular'; | ||
import angular from 'angular' | ||
import MapToArrayFilter from '../shared/maptoarray-filter' | ||
|
||
import GridController from './grid-controller.js'; | ||
import GridModel from './grid-model.js'; | ||
import GridTemplate from './grid-template.html'; | ||
import GridStyles from './grid-styles.scss'; | ||
import GridController from './grid-controller.js' | ||
import GridModel from './grid-model.js' | ||
import GridTemplate from './grid-template.html' | ||
import GridStyles from './grid-styles.scss' | ||
|
||
import TileComponent from '../tile/tile-component.js'; | ||
import TileComponent from '../tile/tile-component.js' | ||
|
||
export default angular.module('grid', [TileComponent.name]) | ||
export default angular.module('grid', [TileComponent.name, MapToArrayFilter.name]) | ||
.factory("GridModel", GridModel) | ||
.component("gridComponent", { | ||
bindings: { | ||
grid: '<' | ||
}, | ||
controller: GridController, | ||
templateUrl: GridTemplate | ||
}); | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,80 @@ | ||
import { | ||
randomInt | ||
} from '../helpers' | ||
import TileModel from '../tile/tile-model' | ||
import _random from 'lodash/random' | ||
|
||
export default class GridModel { | ||
class GridModel { | ||
|
||
constructor() { | ||
this.tiles = [] | ||
constructor(TileModel, TILES_PER_ROW) { | ||
this.TILES_PER_ROW = TILES_PER_ROW | ||
this.TileModel = TileModel; | ||
this.tiles = new Map() | ||
} | ||
|
||
/** | ||
* slotgetMapIndexFromPositionsIsFree(Int x, Int y) | ||
* returns a string made of the 2 axis components | ||
**/ | ||
getMapIndexFromPositions(x, y) { | ||
return `${x}-${y}` | ||
} | ||
|
||
createTile(x, y, value) { | ||
this.tiles.push( | ||
new TileModel(x, y, value) | ||
) | ||
console.log("[debug] this.tiles", this.tiles); | ||
console.log("[debug] this.TileModel", this.TileModel); | ||
this.tiles.set(this.getMapIndexFromPositions(x, y), new this.TileModel(x, y, value)) | ||
} | ||
|
||
/** | ||
* getTileAtPosition(Int x, Int y) | ||
* returns a TileModel object or null | ||
**/ | ||
getTileAtPosition(x, y) { | ||
return this.tiles.get(this.getMapIndexFromPositions(x, y)) | ||
} | ||
|
||
/** | ||
* slotIsFree(Int x, Int y) | ||
* returns a boolean: true if the slot is not taken by a tile | ||
**/ | ||
slotIsFree(x, y) { | ||
return !this.tiles.has(this.getMapIndexFromPositions(x, y)) | ||
} | ||
|
||
/** | ||
* slotsLeft() | ||
* returns the amount of free slots | ||
**/ | ||
slotsLeft() { | ||
return Math.pow(this.TILES_PER_ROW, 2) - this.tiles.size > 0; | ||
} | ||
|
||
/** | ||
* addRandomTiles(Int count) | ||
* creates a defined number of random tiles if there is free slots | ||
* returns a boolean defining if all the tiles could have been added or not | ||
**/ | ||
addRandomTiles(count) { | ||
for (let i = 0; i < count; i++) { | ||
this.createTile(randomInt(0, 4), randomInt(0, 4), 2) //TODO: add TILES_PER_ROW constant here. | ||
|
||
if (!this.slotsLeft()) { | ||
return false | ||
} | ||
let x, y; | ||
do { | ||
x = _random(0, this.TILES_PER_ROW - 1); | ||
y = _random(0, this.TILES_PER_ROW - 1); | ||
} while (!this.slotIsFree(x, y)) | ||
|
||
this.createTile(x, y, 2) | ||
} | ||
return true | ||
} | ||
|
||
move(axis, direction) { | ||
console.log("[debug] move", axis, direction); | ||
moveTiles(axis, direction) { | ||
console.log("[debug] moveTiles", axis, direction); | ||
} | ||
|
||
|
||
} | ||
|
||
//This part is necessary to use our model class as a factory | ||
export default ['TileModel', 'TILES_PER_ROW', (TileModel, TILES_PER_ROW) => { | ||
return () => new GridModel(TileModel, TILES_PER_ROW) | ||
}] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
<div class="grid"> | ||
<tile-component model="tile" ng-repeat="tile in $ctrl.grid.tiles"></tile-component> | ||
<tile-component model="tile" ng-repeat="tile in $ctrl.grid.tiles | mapToArray"></tile-component> | ||
</div> |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* Transforms a Map into an array | ||
* Inspired by the filter at https://github.com/petebacondarwin/angular-toArrayFilter | ||
* use this to ng-repeat on a Map | ||
**/ | ||
|
||
import angular from 'angular' | ||
|
||
export default angular.module('MapToArrayFilter', []) | ||
|
||
.filter('mapToArray', () => function(obj) { | ||
|
||
if (!angular.isObject(obj) || !obj.get) { | ||
return obj | ||
} | ||
return Array.from(obj.values()) | ||
}) |
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
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
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