Browse Source

Tweaking mechanics, fixing bugs.

master
Jeasonfire 4 years ago
parent
commit
e1a7889122
12 changed files with 283 additions and 37 deletions
  1. 1
    0
      index.html
  2. 40
    17
      js/gameState.js
  3. 19
    1
      js/person.js
  4. 57
    0
      lib/dat.gui.d.ts
  5. 95
    0
      lib/dat.gui.min.js
  6. BIN
      res/img/background.png
  7. BIN
      res/img/backgroundHouse.png
  8. BIN
      res/img/heart.png
  9. 45
    18
      src/gameState.ts
  10. 2
    0
      src/main.ts
  11. 23
    1
      src/person.ts
  12. 1
    0
      tsconfig.json

+ 1
- 0
index.html View File

@@ -8,6 +8,7 @@
<body>
<div id="game"></div>
<script src="./lib/phaser.min.js"></script>
<script src="./lib/dat.gui.min.js"></script>
<script src="./js/build.js"></script>
<script src="./js/person.js"></script>
<script src="./js/gameState.js"></script>

+ 40
- 17
js/gameState.js View File

@@ -37,6 +37,7 @@ var GameState = (function () {
this.backgroundSprite = this.game.make.sprite(0, 0, "background");
this.backgroundSprite.fixedToCamera = true;
this.backgroundGroup.add(this.backgroundSprite);
this.backgroundHouses = [];
this.ground = this.game.make.group();
this.midgroundGroup.add(this.ground);
this.tileElevation = 15;
@@ -53,17 +54,18 @@ var GameState = (function () {
this.fadedHouseSprite = this.game.add.sprite(0, 0, "house");
this.fadedHouseSprite.anchor.setTo(0.5);
this.fadedHouseSprite.alpha = 0.4;
this.fadedHouseSprite.visible = false;
this.fadedFarmSprite = this.game.add.sprite(0, 0, "farm");
this.fadedFarmSprite.anchor.setTo(0.5);
this.fadedFarmSprite.alpha = 0.4;
this.fadedFarmSprite.visible = false;
this.currentTileType = TileType.HOUSE;
this.currentTileType = TileType.FARM;
this.people = [];
this.freePeople = [];
this.createPerson(450, 300);
this.createPerson(450, 330);
this.createPerson(450, 270);
this.reproductionRate = 0.3;
this.addHouseToBackground();
};
GameState.prototype.update = function () {
var _this = this;
@@ -74,13 +76,17 @@ var GameState = (function () {
this.updateFreePeople();
for (var i = 0; i < this.freePeople.length; i++) {
var person = this.freePeople[i];
var canFarm = this.buildableFarmsAmt() > 0;
var isHungry = person.getHunger() > Person.MILDLY_HUNGRY;
person.startWorkingOn(this.getNearestBuild(this.builds.filter(function (build) {
var isFarm = build.getTileType() == TileType.FARM;
var isReachable = build.getTileType() != TileType.HOUSE || _this.houseExistsAt(build.getX(), build.getY() + 1) || _this.groundExistsAt(build.getX(), build.getY() + 1);
return !build.beingWorkedOn && isReachable && (!isHungry || !canFarm || isFarm);
}), person));
if (person.dead) {
this.freePeople.splice(i, 1);
}
else {
var hungry = person.getHunger() > Person.MILDLY_HUNGRY;
person.startWorkingOn(this.getNearestBuild(this.builds.filter(function (build) {
var notHungryOrFarm = !hungry || build.getTileType() == TileType.FARM;
var isReachable = build.getTileType() != TileType.HOUSE || _this.houseExistsAt(build.getX(), build.getY() + 1) || _this.groundExistsAt(build.getX(), build.getY() + 1);
return !build.beingWorkedOn && isReachable && notHungryOrFarm;
}), person));
}
}
var averageHungerTotal = 0;
var time = this.game.time.totalElapsedSeconds();
@@ -111,6 +117,18 @@ var GameState = (function () {
for (var i = 0; i < this.houses.length; i++) {
this.updateHouse(this.houses[i]);
}
if (this.people.length > this.backgroundHouses.length) {
var len = this.people.length - this.backgroundHouses.length;
for (var i = 0; i < len; i++) {
this.addHouseToBackground();
}
}
if (this.people.length < this.backgroundHouses.length) {
var len = this.backgroundHouses.length - this.people.length;
for (var i = 0; i < len; i++) {
this.removeHouseFromBackground();
}
}
};
GameState.prototype.getNearestBuild = function (builds, person) {
if (builds.length == 0) {
@@ -133,14 +151,19 @@ var GameState = (function () {
return bestCandidate;
}
};
GameState.prototype.buildableFarmsAmt = function () {
var result = 0;
for (var i = 0; i < this.builds.length; i++) {
if (!this.builds[i].beingWorkedOn && this.builds[i].getTileType() == TileType.FARM) {
result++;
}
}
return result;
GameState.prototype.addHouseToBackground = function () {
var sprite = this.game.make.sprite(32 + (GAME_WIDTH - 64) * Math.random(), 370 + 50 * Math.random(), "backgroundHouse");
sprite.alpha = 0;
this.game.add.tween(sprite).to({ alpha: 1 }, 2000, Phaser.Easing.Default, true);
this.backgroundGroup.add(sprite);
this.backgroundHouses.push(sprite);
};
GameState.prototype.removeHouseFromBackground = function () {
var spriteIndex = Math.floor(Math.random() * this.backgroundHouses.length);
var sprite = this.backgroundHouses[spriteIndex];
this.backgroundHouses.splice(spriteIndex, 1);
var tween = this.game.add.tween(sprite);
tween.to({ alpha: 0, exists: false }, 3000, Phaser.Easing.Default, true);
};
GameState.prototype.reproduce = function (person) {
var index = Math.floor(Math.random() * this.people.length);

+ 19
- 1
js/person.js View File

@@ -1,6 +1,5 @@
var Person = (function () {
function Person(x, y, game) {
this.hunger = 0;
this.eating = false;
this.directionChangeTimeCurrent = 0;
this.directionChangeTime = 5;
@@ -25,6 +24,7 @@ var Person = (function () {
this.sprite.animations.add("walk", [0, 1, 0, 2], 12, true);
this.sprite.animations.add("work", [3, 4], 12, true);
this.sprite.animations.add("eat", [5, 0], 14, false);
this.hunger = 0.5;
this.hungerBarEmpty = this.game.add.sprite(-this.sprite.width / 2, -14, "hungerEmpty");
this.hungerBarEmpty.parent = this.sprite;
this.hungerBarFull = this.game.add.sprite(-this.sprite.width / 2, -14, "hungerFull");
@@ -43,6 +43,7 @@ var Person = (function () {
this.currentlyWorking = false;
this.build = null;
this.dead = false;
this.status = "just created";
}
Person.prototype.startWorkingOn = function (build) {
if (build === null) {
@@ -69,6 +70,7 @@ var Person = (function () {
if (Math.abs(this.sprite.body.velocity.y) < 10) {
this.sprite.body.velocity.x = 0;
}
this.status = "not even idling!";
if (this.targetPoint !== null) {
if (this.targetPoint.x - this.sprite.x > this.sprite.width / 1.8) {
this.sprite.body.moveRight(this.moveSpeed * 1.2);
@@ -81,6 +83,7 @@ var Person = (function () {
this.heartEmitter.start(true, this.heartDuration, null, this.heartAmount);
this.reproduced = Math.random() < 0.65;
}
this.status = "searching for target";
}
else if (this.hunger > Person.VERY_HUNGRY && people.length > 1 && farms.length == 0) {
if (this.build !== null) {
@@ -114,12 +117,20 @@ var Person = (function () {
this.targetPerson = null;
this.eating = false;
}
this.status = "eating people";
}
else if (this.hunger > Person.VERY_HUNGRY && people.length == 1 && farms.length == 0 && this.targetPerson !== null) {
this.targetPerson = null;
this.status = "forgetting about target";
}
else if (this.hunger > Person.MILDLY_HUNGRY && farms.length > 0) {
if (this.build !== null && this.build.getTileType() == TileType.HOUSE) {
this.build.beingWorkedOn = false;
this.build = null;
}
if (this.targetFarm !== null && !this.targetFarm.alive) {
this.targetFarm = null;
}
if (this.targetFarm === null) {
var dist = 1000;
var bestFarm = null;
@@ -145,6 +156,11 @@ var Person = (function () {
farms.splice(farms.indexOf(this.targetFarm), 1);
this.targetFarm = null;
}
this.status = "finding a farm";
}
else if (this.hunger > Person.MILDLY_HUNGRY && farms.length == 0 && this.targetFarm !== null) {
this.targetFarm = null;
this.status = "forgetting about a farm";
}
else if (this.build !== null && (this.hunger < Person.MILDLY_HUNGRY || (this.targetFarm === null && this.targetPerson === null))) {
this.build.beingWorkedOn = true;
@@ -162,6 +178,7 @@ var Person = (function () {
if (this.build.isDoneBuilding()) {
this.build = null;
}
this.status = "building";
}
else if (this.targetPerson === null && this.targetFarm === null) {
var time = this.game.time.totalElapsedSeconds();
@@ -170,6 +187,7 @@ var Person = (function () {
this.direction = Math.round(Math.random() * 3 - 1.5);
}
this.sprite.body.velocity.x = this.direction * this.moveSpeed / 3;
this.status = "idling";
}
this.updateAnimations();
this.updateHungerBarPosition();

+ 57
- 0
lib/dat.gui.d.ts View File

@@ -0,0 +1,57 @@
// Type definitions for dat.GUI v0.5
// Project: https://github.com/dataarts/dat.gui
// Definitions by: Satoru Kimura <https://github.com/gyohk>
// Definitions: https://github.com/borisyankov/DefinitelyTyped

declare module dat {
export class GUI {
constructor(option?: GUIParams);

__controllers: GUIController[];
__folders: GUI[];
domElement: HTMLElement;

add(target: Object, propName:string): GUIController;
add(target: Object, propName:string, min: number, max: number): GUIController;
add(target: Object, propName:string, status: boolean): GUIController;
add(target: Object, propName:string, items:string[]): GUIController;
add(target: Object, propName:string, items:number[]): GUIController;
add(target: Object, propName:string, items:Object): GUIController;

addColor(target: Object, propName:string): GUIController;
addColor(target: Object, propName:string, color: string): GUIController;
addColor(target: Object, propName:string, rgba: number[]): GUIController; // rgb or rgba
addColor(target: Object, propName:string, hsv:{h:number; s:number; v:number}): GUIController;

addFolder(propName:string): GUI;

close(): void;
open(): void;
remember(target: Object): void;
}

export interface GUIParams{
autoPlace?: boolean;
closed?: boolean;
load?: any;
name?: string;
preset?: string;
width?: number;
}

export class GUIController {
destroy(): void;
fire(): GUIController;
getValue(): any;
isModified(): boolean;
listen(): GUIController;
min(n: number): GUIController;
remove(target: GUIController): void;
setValue(value: any): GUIController;
step(n: number): GUIController;
updateDisplay(): void;

onChange: (value?: any) => void;
onFinishChange: (value?: any) => void;
}
}

+ 95
- 0
lib/dat.gui.min.js
File diff suppressed because it is too large
View File


BIN
res/img/background.png View File


BIN
res/img/backgroundHouse.png View File


BIN
res/img/heart.png View File


+ 45
- 18
src/gameState.ts View File

@@ -61,6 +61,7 @@ class GameState {
this.backgroundSprite = this.game.make.sprite(0, 0, "background");
this.backgroundSprite.fixedToCamera = true;
this.backgroundGroup.add(this.backgroundSprite);
this.backgroundHouses = [];

this.ground = this.game.make.group();
this.midgroundGroup.add(this.ground);
@@ -70,9 +71,11 @@ class GameState {
}

this.builds = [];

this.houses = [];
this.farms = [];
//gui.add(this.builds, 'length').listen();
//gui.add(this.houses, 'length').listen();
//gui.add(this.farms, 'length').listen();
var house = this.startConstruction(Math.floor(Math.floor(900 / TILE_SIZE) / 2), this.tileElevation - 1, TileType.HOUSE);
house.progress = 1;
var farm = this.startConstruction(Math.floor(Math.floor(900 / TILE_SIZE) / 2) - 1, this.tileElevation - 1, TileType.FARM);
@@ -81,11 +84,11 @@ class GameState {
this.fadedHouseSprite = this.game.add.sprite(0, 0, "house");
this.fadedHouseSprite.anchor.setTo(0.5);
this.fadedHouseSprite.alpha = 0.4;
this.fadedHouseSprite.visible = false;
this.fadedFarmSprite = this.game.add.sprite(0, 0, "farm");
this.fadedFarmSprite.anchor.setTo(0.5);
this.fadedFarmSprite.alpha = 0.4;
this.fadedFarmSprite.visible = false;
this.currentTileType = TileType.HOUSE;
this.currentTileType = TileType.FARM;

this.people = [];
this.freePeople = [];
@@ -94,6 +97,8 @@ class GameState {
this.createPerson(450, 270);

this.reproductionRate = 0.3;

this.addHouseToBackground();
}

public update(): void {
@@ -105,13 +110,16 @@ class GameState {
this.updateFreePeople();
for (var i = 0; i < this.freePeople.length; i++) {
var person = this.freePeople[i];
var canFarm = this.buildableFarmsAmt() > 0;
var isHungry = person.getHunger() > Person.MILDLY_HUNGRY;
person.startWorkingOn(this.getNearestBuild(this.builds.filter((build: Build) => {
var isFarm = build.getTileType() == TileType.FARM;
var isReachable = build.getTileType() != TileType.HOUSE || this.houseExistsAt(build.getX(), build.getY() + 1) || this.groundExistsAt(build.getX(), build.getY() + 1);
return !build.beingWorkedOn && isReachable && (!isHungry || !canFarm || isFarm);
}), person));
if (person.dead) {
this.freePeople.splice(i, 1);
} else {
var hungry = person.getHunger() > Person.MILDLY_HUNGRY;
person.startWorkingOn(this.getNearestBuild(this.builds.filter((build: Build) => {
var notHungryOrFarm = !hungry || build.getTileType() == TileType.FARM;
var isReachable = build.getTileType() != TileType.HOUSE || this.houseExistsAt(build.getX(), build.getY() + 1) || this.groundExistsAt(build.getX(), build.getY() + 1);
return !build.beingWorkedOn && isReachable && notHungryOrFarm;
}), person));
}
}

var averageHungerTotal = 0;
@@ -143,6 +151,19 @@ class GameState {
for (var i = 0; i < this.houses.length; i++) {
this.updateHouse(this.houses[i]);
}

if (this.people.length > this.backgroundHouses.length) {
var len = this.people.length - this.backgroundHouses.length;
for (var i = 0; i < len; i++) {
this.addHouseToBackground();
}
}
if (this.people.length < this.backgroundHouses.length) {
var len = this.backgroundHouses.length - this.people.length;
for (var i = 0; i < len; i++) {
this.removeHouseFromBackground();
}
}
}

private getNearestBuild(builds: Build[], person: Person): Build {
@@ -166,14 +187,20 @@ class GameState {
}
}

private buildableFarmsAmt(): number {
var result = 0;
for (var i = 0; i < this.builds.length; i++) {
if (!this.builds[i].beingWorkedOn && this.builds[i].getTileType() == TileType.FARM) {
result++;
}
}
return result;
private addHouseToBackground() {
var sprite = this.game.make.sprite(32 + (GAME_WIDTH - 64) * Math.random(), 370 + 50 * Math.random(), "backgroundHouse");
sprite.alpha = 0;
this.game.add.tween(sprite).to({alpha: 1}, 2000, Phaser.Easing.Default, true);
this.backgroundGroup.add(sprite)
this.backgroundHouses.push(sprite);
}

private removeHouseFromBackground() {
var spriteIndex = Math.floor(Math.random() * this.backgroundHouses.length);
var sprite = this.backgroundHouses[spriteIndex];
this.backgroundHouses.splice(spriteIndex, 1);
var tween = this.game.add.tween(sprite);
tween.to({alpha: 0, exists: false}, 3000, Phaser.Easing.Default, true);
}

private reproduce(person: Person): boolean {

+ 2
- 0
src/main.ts View File

@@ -1,3 +1,5 @@
//var gui = new dat.GUI();
//gui.close();

var GAME_WIDTH = 900;
var GAME_HEIGHT = 600;

+ 23
- 1
src/person.ts View File

@@ -5,7 +5,7 @@ class Person {

private game: Phaser.Game;
private currentlyWorking: boolean;
private hunger: number = 0;
private hunger: number;

private eating: boolean = false;
private directionChangeTimeCurrent: number = 0;
@@ -31,6 +31,9 @@ class Person {
public targetPoint: Phaser.Point;
public reproduced: boolean = false;

public status: string;
//public guiController: dat.GUIController;

public constructor(x: number, y: number, game: Phaser.Game) {
this.game = game;
this.targetFarm = null;
@@ -53,6 +56,7 @@ class Person {
this.sprite.animations.add("work", [3, 4], 12, true);
this.sprite.animations.add("eat", [5, 0], 14, false);

this.hunger = 0.5;
this.hungerBarEmpty = this.game.add.sprite(-this.sprite.width / 2, -14, "hungerEmpty");
this.hungerBarEmpty.parent = this.sprite;
this.hungerBarFull = this.game.add.sprite(-this.sprite.width / 2, -14, "hungerFull");
@@ -74,6 +78,9 @@ class Person {
this.currentlyWorking = false;
this.build = null;
this.dead = false;

this.status = "just created";
//this.guiController = gui.add(this, "status").listen();
}

public startWorkingOn(build: Build): void {
@@ -104,6 +111,7 @@ class Person {
if (Math.abs(this.sprite.body.velocity.y) < 10) {
this.sprite.body.velocity.x = 0;
}
this.status = "not even idling!";
if (this.targetPoint !== null) {
if (this.targetPoint.x - this.sprite.x > this.sprite.width / 1.8) {
this.sprite.body.moveRight(this.moveSpeed * 1.2);
@@ -114,6 +122,7 @@ class Person {
this.heartEmitter.start(true, this.heartDuration, null, this.heartAmount);
this.reproduced = Math.random() < 0.65;
}
this.status = "searching for target";
} else if (this.hunger > Person.VERY_HUNGRY && people.length > 1 && farms.length == 0) {
if (this.build !== null) {
this.build.beingWorkedOn = false;
@@ -143,11 +152,18 @@ class Person {
this.targetPerson = null;
this.eating = false;
}
this.status = "eating people";
} else if (this.hunger > Person.VERY_HUNGRY && people.length == 1 && farms.length == 0 && this.targetPerson !== null) {
this.targetPerson = null;
this.status = "forgetting about target";
} else if (this.hunger > Person.MILDLY_HUNGRY && farms.length > 0) {
if (this.build !== null && this.build.getTileType() == TileType.HOUSE) {
this.build.beingWorkedOn = false;
this.build = null;
}
if (this.targetFarm !== null && !this.targetFarm.alive) {
this.targetFarm = null;
}
if (this.targetFarm === null) {
var dist = 1000;
var bestFarm = null;
@@ -170,6 +186,10 @@ class Person {
farms.splice(farms.indexOf(this.targetFarm), 1);
this.targetFarm = null;
}
this.status = "finding a farm";
} else if (this.hunger > Person.MILDLY_HUNGRY && farms.length == 0 && this.targetFarm !== null) {
this.targetFarm = null;
this.status = "forgetting about a farm";
} else if (this.build !== null && (this.hunger < Person.MILDLY_HUNGRY || (this.targetFarm === null && this.targetPerson === null))) {
this.build.beingWorkedOn = true;
if (this.build.getX() * TILE_SIZE - this.sprite.x > TILE_SIZE) {
@@ -184,6 +204,7 @@ class Person {
if (this.build.isDoneBuilding()) {
this.build = null;
}
this.status = "building";
} else if (this.targetPerson === null && this.targetFarm === null) {
var time = this.game.time.totalElapsedSeconds();
if (time - this.directionChangeTimeCurrent > this.directionChangeTime) {
@@ -191,6 +212,7 @@ class Person {
this.direction = Math.round(Math.random() * 3 - 1.5);
}
this.sprite.body.velocity.x = this.direction * this.moveSpeed / 3;
this.status = "idling";
}
this.updateAnimations();
this.updateHungerBarPosition();

+ 1
- 0
tsconfig.json View File

@@ -6,6 +6,7 @@
"./**/*.ts"
],
"files": [
"./lib/dat.gui.d.ts",
"./lib/p2.d.ts",
"./lib/phaser.d.ts",
"./lib/pixi.d.ts",

Loading…
Cancel
Save