Skip to content

Commit f44cd42

Browse files
committed
add cost and goal to Superfarming
1 parent f4d1865 commit f44cd42

6 files changed

Lines changed: 125 additions & 26 deletions

File tree

Superfarming/Files/Component/Bring.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export class Bring extends Component {
5353
ourTransform.xpos = closestTransform.xpos;
5454
ourTransform.ypos = closestTransform.ypos;
5555
if (carrying) {
56-
closestTransform.entity.GetComponent(BringTowards).money += 1;
56+
this.State.gainedMoney += 1;
5757
this.bringingId = undefined;
5858
this.State.RemoveEntity(bringingEntity);
5959
this.State.audio_treasureget.Play();
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
import { Component } from "../Component.js";
22
export class BringTowards extends Component {
3-
constructor() {
4-
super();
5-
this.money = 0;
6-
}
73
}

Superfarming/Files/Component/Clickbox.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,21 @@ export var Spawntype;
1010
Spawntype[Spawntype["Farmer"] = 2] = "Farmer";
1111
})(Spawntype || (Spawntype = {}));
1212
export class Clickbox extends Component {
13-
constructor(width, height, spawntype) {
13+
constructor(width, height, spawntype, costMultiplier) {
1414
super();
15+
this.costMax = 50;
1516
this.width = width;
1617
this.height = height;
1718
this.active = false;
1819
this.spawntype = spawntype;
20+
this.costMultiplier = costMultiplier;
21+
this._cost = 0;
22+
}
23+
get cost() {
24+
return Math.floor(this._cost);
25+
}
26+
get canSpawn() {
27+
return this.State.availableMoney >= this.cost;
1928
}
2029
Update() {
2130
if (this.State.CurrentInputNewlyPressed.click) {
@@ -32,8 +41,8 @@ export class Clickbox extends Component {
3241
this.active = true;
3342
}
3443
}
35-
else if (this.active) {
36-
let xoff = 200;
44+
else if (this.active && this.canSpawn) {
45+
let xoff = 240;
3746
let yoff = 16;
3847
let scale = 29;
3948
let gridX = Math.floor((clickX - xoff) / scale);
@@ -53,6 +62,16 @@ export class Clickbox extends Component {
5362
else if (this.spawntype == Spawntype.Farmer) {
5463
this.State.AddEntity(new Farmer(gridX, gridY));
5564
}
65+
this.State.spentMoney += this.cost;
66+
if (this._cost == 0) {
67+
this._cost = 5;
68+
}
69+
else {
70+
this._cost *= this.costMultiplier;
71+
}
72+
if (this._cost > this.costMax) {
73+
this._cost = this.costMax;
74+
}
5675
}
5776
}
5877
}

Superfarming/Files/Entity/Spawnbox.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { Clickbox } from "../Component/Clickbox.js";
22
import { Transform } from "../Component/Transform.js";
33
import { Entity } from "../Entity.js";
44
export class Spawnbox extends Entity {
5-
constructor(xpos, ypos, width, height, spawntype) {
5+
constructor(xpos, ypos, width, height, spawntype, costMultiplier) {
66
super();
77
let transform = new Transform(xpos, ypos);
88
this.AddComponent(transform);
9-
let clickbox = new Clickbox(width, height, spawntype);
9+
let clickbox = new Clickbox(width, height, spawntype, costMultiplier);
1010
this.AddComponent(clickbox);
1111
}
1212
}

Superfarming/Files/State.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@ export class State {
55
this.entities = [];
66
this.history = [];
77
this.inputHistory = [];
8+
this.gainedMoney = 0;
9+
this.spentMoney = 0;
810
}
911
InitializeAudio(context) {
1012
this.audio_treasurespawns = [new SoundEffect(context, "Assets/treasurespawn_1.wav", 0.3), new SoundEffect(context, "Assets/treasurespawn_2.wav", 0.3), new SoundEffect(context, "Assets/treasurespawn_3.wav", 0.3)];
1113
this.audio_treasureget = new SoundEffect(context, "Assets/treasureget.wav", 0.02);
1214
}
15+
get availableMoney() {
16+
return this.gainedMoney - this.spentMoney;
17+
}
1318
get CurrentFrame() {
1419
return this.history.length - 1;
1520
}

Superfarming/Superfarming.js

Lines changed: 95 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import { BringTowards } from "./Files/Component/BringTowards.js";
21
import { Clickbox, Spawntype } from "./Files/Component/Clickbox.js";
32
import { Texture } from "./Files/Component/Texture.js";
43
import { Transform } from "./Files/Component/Transform.js";
4+
import { Farmer } from "./Files/Entity/Farmer.js";
5+
import { Home } from "./Files/Entity/Home.js";
56
import { Spawnbox } from "./Files/Entity/Spawnbox.js";
7+
import { Spot } from "./Files/Entity/Spot.js";
68
import { InputState } from "./Files/InputState.js";
79
import { SoundEffect } from "./Files/SoundEffect.js";
810
import { State } from "./Files/State.js";
@@ -28,9 +30,9 @@ class PianoRoll {
2830
// for (let i = 0; i<10; i++) {
2931
// this.state.AddEntity(new Spot(Math.random()*0x10000, Math.random()*0x10000));
3032
// }
31-
this.state.AddEntity(new Spawnbox(10, 100, 170, 50, Spawntype.Home));
32-
this.state.AddEntity(new Spawnbox(10, 160, 170, 50, Spawntype.Spot));
33-
this.state.AddEntity(new Spawnbox(10, 220, 170, 50, Spawntype.Farmer));
33+
this.state.AddEntity(new Spawnbox(10, 100, 200, 60, Spawntype.Home, 1.2));
34+
this.state.AddEntity(new Spawnbox(10, 170, 200, 60, Spawntype.Spot, 1.2));
35+
this.state.AddEntity(new Spawnbox(10, 240, 200, 60, Spawntype.Farmer, 1.7));
3436
this.state.AddToHistory();
3537
// addEventListener("keydown", (e) => this.updateInputKey(e, true));
3638
// addEventListener("keyup", (e) => this.updateInputKey(e, false));
@@ -148,34 +150,89 @@ class PianoRoll {
148150
}
149151
else {
150152
this.state.Update(this.input);
153+
if (this.startGameFrame == undefined) {
154+
if (this.state.GetComponents(Clickbox).filter(clickbox => clickbox.cost == 0).length == 0) {
155+
this.startGameFrame = this.state.CurrentFrame;
156+
}
157+
}
158+
if (this.stopGameFrame == undefined) {
159+
if (this.state.gainedMoney >= 1000) {
160+
this.stopGameFrame = this.state.CurrentFrame;
161+
}
162+
}
151163
}
152164
this.redraw();
153165
}
154166
}
167+
formatTime(seconds) {
168+
const flooredSeconds = Math.floor(seconds);
169+
const minutes = Math.floor(flooredSeconds / 60);
170+
const remainingSeconds = flooredSeconds % 60;
171+
return `${minutes}:${remainingSeconds < 10 ? '0' : ''}${remainingSeconds}`;
172+
}
173+
formatTimePreciser(seconds) {
174+
const centiSeconds = Math.floor(seconds * 100);
175+
const onlyCentiseconds = centiSeconds % 100;
176+
const flooredSeconds = Math.floor(centiSeconds / 100) % 60;
177+
const minutes = Math.floor(centiSeconds / (60 * 100));
178+
return `${minutes}:${flooredSeconds < 10 ? '0' : ''}${flooredSeconds}.${onlyCentiseconds < 10 ? '0' : ''}${onlyCentiseconds}`;
179+
}
155180
redraw() {
156181
var _a;
157182
this.context.fillStyle = "#6495ED";
158183
this.context.fillRect(0, 0, this.canvas.width, this.canvas.height);
184+
this.context.miterLimit = 1;
159185
this.context.font = "18px sans-serif";
160186
this.context.fillStyle = "#000000";
161187
//this.context.fillText(String(this.state.CurrentFrame), 10, 20);
162188
this.context.font = "14px sans-serif";
163189
// this.context.fillText("Left-click: (H)ome", 10, 40);
164190
// this.context.fillText("Right-click: (S)pot for digging", 10, 60);
165191
// this.context.fillText("Middle-click: (F)armer", 10, 80);
166-
this.context.fillText("(T)reasure", 10, 80);
167-
let money = 0;
168-
for (let component of this.state.GetComponents(BringTowards)) {
169-
money += component.money;
170-
}
192+
this.context.fillText("[T]reasure is worth $1", 10, 330);
171193
this.context.font = "18px sans-serif";
172-
this.context.fillText(`Money: $${money}`, 10, 50);
194+
this.context.fillText(`Money: $${this.state.availableMoney}`, 10, 50);
195+
this.context.font = "14px sans-serif";
196+
this.context.fillText(`Total: $${this.state.gainedMoney}`, 10, 70);
197+
this.context.strokeStyle = "#202020";
198+
this.context.lineWidth = 5;
199+
if (this.startGameFrame != undefined) {
200+
let text;
201+
if (this.stopGameFrame != undefined) {
202+
this.context.fillStyle = "#FFFFFF";
203+
text = `You reached $1000 in`;
204+
this.context.strokeText(text, 10, 400);
205+
this.context.fillText(text, 10, 400);
206+
this.context.fillStyle = "#00FF00";
207+
text = this.formatTimePreciser(((this.stopGameFrame - this.startGameFrame) * this.aimedTimeDelta) / 1000);
208+
this.context.strokeText(text, 10, 420);
209+
this.context.fillText(text, 10, 420);
210+
this.context.fillStyle = "#FFFFFF";
211+
text = `Congratulations!`;
212+
this.context.strokeText(text, 10, 450);
213+
this.context.fillText(text, 10, 450);
214+
this.context.fillStyle = "#C0C0C0";
215+
text = "Total Playtime: ";
216+
text += this.formatTime(((this.state.CurrentFrame - this.startGameFrame) * this.aimedTimeDelta) / 1000);
217+
this.context.strokeText(text, 10, 480);
218+
this.context.fillText(text, 10, 480);
219+
}
220+
else {
221+
this.context.fillStyle = "#C0C0C0";
222+
text = `Try to reach $1000 (${Math.floor(this.state.gainedMoney / 10)}%)`;
223+
this.context.strokeText(text, 10, 400);
224+
this.context.fillText(text, 10, 400);
225+
text = this.formatTime(((this.state.CurrentFrame - this.startGameFrame) * this.aimedTimeDelta) / 1000);
226+
this.context.strokeText(text, 10, 420);
227+
this.context.fillText(text, 10, 420);
228+
}
229+
}
173230
// for (let y = 0; y < 20; y++) {
174231
// for (let x = 0; x < 20; x++) {
175232
// this.context.fillText(String(x+y), 20+x*16, 20+y*16);
176233
// }
177234
// }
178-
let xoff = 200;
235+
let xoff = 240;
179236
let yoff = 16;
180237
let scale = 29;
181238
this.context.fillStyle = "#000000";
@@ -216,25 +273,47 @@ class PianoRoll {
216273
}
217274
let clickbox = transform.entity.GetComponent(Clickbox);
218275
if (clickbox) {
219-
this.context.fillStyle = "#C0C0C0";
276+
this.context.fillStyle = "#A0A0A0";
220277
this.context.strokeStyle = "#000000";
221278
this.context.lineWidth = 4;
279+
if (clickbox.canSpawn) {
280+
this.context.fillStyle = "#D0D0D0";
281+
}
222282
if (clickbox.active) {
223-
this.context.lineWidth = 8;
224-
this.context.fillStyle = "#FFFFFF";
225-
this.context.strokeStyle = "#0000FF";
283+
if (clickbox.canSpawn) {
284+
this.context.lineWidth = 8;
285+
this.context.fillStyle = "#FFFFFF";
286+
this.context.strokeStyle = "#00A000";
287+
}
288+
else {
289+
this.context.strokeStyle = "#C00000";
290+
}
226291
}
227292
this.context.strokeRect(transform.xpos, transform.ypos, clickbox.width, clickbox.height);
228293
this.context.fillRect(transform.xpos, transform.ypos, clickbox.width, clickbox.height);
229294
this.context.font = "18px sans-serif";
230-
this.context.fillStyle = "#C0C0C0";
231295
this.context.strokeStyle = "#000000";
232296
this.context.lineWidth = 4;
233297
let text = clickbox.spawntype == Spawntype.Home ? "[H]ome"
234298
: clickbox.spawntype == Spawntype.Spot ? "[S]pot for digging"
235299
: clickbox.spawntype == Spawntype.Farmer ? "[F]armer" : "";
300+
let type = clickbox.spawntype == Spawntype.Home ? Home
301+
: clickbox.spawntype == Spawntype.Spot ? Spot
302+
: clickbox.spawntype == Spawntype.Farmer ? Farmer : undefined;
303+
let count = this.state.entities.filter(entity => entity instanceof type).length;
304+
if (count > 0) {
305+
text += ` (${count})`;
306+
}
236307
this.context.strokeText(text, transform.xpos + 10, transform.ypos + 30);
237308
this.context.fillText(text, transform.xpos + 10, transform.ypos + 30);
309+
this.context.font = "14px sans-serif";
310+
this.context.lineWidth = 3;
311+
text = `Cost: $${clickbox.cost}`;
312+
if (clickbox.cost == clickbox.costMax) {
313+
text += " (max)";
314+
}
315+
this.context.strokeText(text, transform.xpos + 15, transform.ypos + 50);
316+
this.context.fillText(text, transform.xpos + 15, transform.ypos + 50);
238317
continue;
239318
}
240319
// let hitbox = entity.GetComponent(Hitbox);

0 commit comments

Comments
 (0)