1- import { BringTowards } from "./Files/Component/BringTowards.js" ;
21import { Clickbox , Spawntype } from "./Files/Component/Clickbox.js" ;
32import { Texture } from "./Files/Component/Texture.js" ;
43import { Transform } from "./Files/Component/Transform.js" ;
4+ import { Farmer } from "./Files/Entity/Farmer.js" ;
5+ import { Home } from "./Files/Entity/Home.js" ;
56import { Spawnbox } from "./Files/Entity/Spawnbox.js" ;
7+ import { Spot } from "./Files/Entity/Spot.js" ;
68import { InputState } from "./Files/InputState.js" ;
79import { SoundEffect } from "./Files/SoundEffect.js" ;
810import { 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