Created
March 13, 2023 15:14
-
-
Save tensaix2j/d2c454f02fa589de550e504b8e11c57b to your computer and use it in GitHub Desktop.
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
export class Stage extends Entity implements ISystem { | |
public progressbar; | |
public progressbar_tick = 0; | |
public progressbar_maxtick = 330; | |
public bigword:UIText ; | |
public bigword_tick = 0; | |
public smallword:UIText; | |
public smallword_tick = 0; | |
public tasks = []; | |
public taskIndex = 0; | |
public taskFailCount = 0; | |
public life = 10; | |
public ui_hearts = []; | |
public level = 1; | |
public circleTexts = []; | |
public pause = 0; | |
constructor() { | |
super(); | |
this.createUI(); | |
this.init_inputs(); | |
engine.addSystem( this ); | |
} | |
//------------ | |
createUI() { | |
let ui_2d_canvas = new UICanvas(); | |
let ui_root = new UIContainerRect( ui_2d_canvas ); | |
ui_root.vAlign = "center"; | |
ui_root.hAlign = "center"; | |
ui_root.positionX = 0; | |
ui_root.positionY = 0; | |
ui_root.visible = true; | |
let texture_circle = new Texture("images/circle.png"); | |
let texture_progress = new Texture("images/progress.png"); | |
let texture_heart = new Texture("images/heart.png"); | |
let ui_image = new UIImage( ui_root, texture_progress ); | |
ui_image.hAlign = "center"; | |
ui_image.vAlign = "center"; | |
ui_image.width = 256; | |
ui_image.height = 32; | |
ui_image.positionX = -160; | |
ui_image.positionY = -250; | |
ui_image.sourceLeft = 0; | |
ui_image.sourceTop = 0; | |
ui_image.sourceWidth = 512; | |
ui_image.sourceHeight = 64; | |
ui_image.visible = true; | |
let ui_image2 = new UIImage( ui_image , texture_circle ); | |
ui_image2.hAlign = "center"; | |
ui_image2.vAlign = "center"; | |
ui_image2.width = 16; | |
ui_image2.height = 16; | |
ui_image2.positionX = -114; | |
ui_image2.positionY = 0; | |
ui_image2.sourceLeft = 128; | |
ui_image2.sourceTop = 0; | |
ui_image2.sourceWidth = 128; | |
ui_image2.sourceHeight = 128; | |
this.progressbar = ui_image2; | |
let ui_text = new UIText( ui_root ); | |
ui_text.value = ""; | |
ui_text.fontSize = 30; | |
ui_text.positionX = -100; | |
ui_text.positionY = 0; | |
ui_text.visible = false; | |
this.bigword = ui_text; | |
ui_text = new UIText( ui_root ); | |
ui_text.value = "Press E to start"; | |
ui_text.fontSize = 16; | |
ui_text.positionX = 0; | |
ui_text.positionY = 0; | |
ui_text.visible = true; | |
this.smallword = ui_text; | |
for ( let i = 0 ; i < 20 ; i++) { | |
let ui_image = new UIImage( ui_root, texture_circle ); | |
ui_image.hAlign = "center"; | |
ui_image.vAlign = "center"; | |
ui_image.width = 50; | |
ui_image.height = 50; | |
ui_image.positionX = -200 + 51 * i; | |
ui_image.positionY = -200; | |
ui_image.sourceLeft = 0; | |
ui_image.sourceTop = 0; | |
ui_image.sourceWidth = 128; | |
ui_image.sourceHeight = 128; | |
ui_image.visible = false; | |
let ui_text = new UIText( ui_image ); | |
ui_text.value = "2"; | |
ui_text.fontSize = 30; | |
ui_text.positionX = 16; | |
ui_text.positionY = 8; | |
this.circleTexts.push( ui_text ); | |
} | |
ui_root = new UIContainerRect( ui_2d_canvas ); | |
ui_root.vAlign = "bottom"; | |
ui_root.hAlign = "right"; | |
ui_root.positionX = 0; | |
ui_root.positionY = 0; | |
ui_root.visible = true; | |
for ( let i = 0 ; i < this.life ; i++ ) { | |
let ui_image = new UIImage( ui_root, texture_heart ); | |
ui_image.hAlign = "center"; | |
ui_image.vAlign = "center"; | |
ui_image.width = 48; | |
ui_image.height = 48; | |
ui_image.positionX = 0 - i * 50; | |
ui_image.positionY = -10; | |
ui_image.sourceLeft = 0; | |
ui_image.sourceTop = 0; | |
ui_image.sourceWidth = 256; | |
ui_image.sourceHeight = 256; | |
ui_image.visible = true; | |
this.ui_hearts.push( ui_image ); | |
} | |
} | |
//-------- | |
init_inputs() { | |
const input = Input.instance; | |
// Left mouse | |
input.subscribe("BUTTON_DOWN", ActionButton.POINTER, true, (e) => { | |
}); | |
input.subscribe("BUTTON_UP", ActionButton.POINTER, false, (e) => { | |
}); | |
// E | |
input.subscribe("BUTTON_DOWN", ActionButton.PRIMARY, false, (e) => { | |
this.pause = 1 - this.pause ; | |
}); | |
input.subscribe("BUTTON_UP", ActionButton.PRIMARY, false, (e) => { | |
}); | |
// F | |
input.subscribe("BUTTON_DOWN", ActionButton.SECONDARY, false, (e) => { | |
}); | |
input.subscribe("BUTTON_UP", ActionButton.SECONDARY, false, (e) => { | |
}); | |
} | |
//------ | |
render_life() { | |
for ( let i = 0 ; i < this.ui_hearts.length ; i++ ) { | |
this.ui_hearts[i].visible = false; | |
} | |
for ( let i = 0 ; i < this.life ; i++ ) { | |
this.ui_hearts[i].visible = true; | |
} | |
} | |
//------ | |
createTask() { | |
this.tasks.length = 0; | |
this.taskIndex = 0; | |
this.progressbar_tick = 0; | |
this.taskFailCount = 0; | |
this.progressbar_maxtick = 330; | |
this.progressbar_maxtick -= this.level * 2; | |
if ( this.progressbar_maxtick < 30 ) { | |
this.progressbar_maxtick = 30; | |
} | |
let tasklen = (( Math.random() * 6 ) >> 0 ) + 4; | |
if ( this.level > 50 ) { | |
tasklen = (( Math.random() * 9 ) >> 0 ) + 8; | |
} | |
if ( this.level < 10 && tasklen > 4) { | |
tasklen = 4; | |
} else if ( this.level < 20 && tasklen > 6) { | |
tasklen = 6; | |
} else if ( this.level < 50 && tasklen > 10) { | |
tasklen = 10; | |
} | |
for ( let i = 0 ; i < tasklen ; i++) { | |
let rnd = (( Math.random() * 4 ) >> 0 ) + 1; | |
this.tasks.push( rnd ); | |
} | |
this.render_circles(); | |
} | |
//--------- | |
render_circles() { | |
for ( let i = 0; i < this.circleTexts.length ; i++ ) { | |
this.circleTexts[i].parent.visible = false; | |
this.circleTexts[i].parent.sourceLeft = 0; | |
} | |
for ( let i = 0; i < this.tasks.length ; i++ ) { | |
this.circleTexts[i].parent.visible = true; | |
this.circleTexts[i].value = this.tasks[i] ; | |
} | |
} | |
//--- | |
update(dt) { | |
if ( this.pause == 0 ) { | |
this.progressbar_tick += 1; | |
let progress_percent:number = this.progressbar_tick * 1.0 / this.progressbar_maxtick ; | |
this.progressbar.positionX = ( ( 228 * progress_percent ) - 114) >> 0; | |
this.smallword.value = this.progressbar_tick + " / " + this.progressbar_maxtick + " PosX:" + this.progressbar.positionX ; | |
this.bigword.value = ""; | |
this.bigword.visible = true; | |
if ( this.progressbar_tick >= this.progressbar_maxtick ) { | |
this.bigword.visible = true ; | |
this.bigword.fontSize = 30; | |
this.bigword.color = Color4.Red(); | |
this.smallword.value = this.progressbar_tick + " / " + this.progressbar_maxtick; | |
this.smallword.visible= true; | |
this.progressbar_tick = 0; | |
this.progressbar_maxtick = 330; | |
this.progressbar.positionX = -114; | |
for ( let i = this.taskIndex ; i < this.tasks.length ; i++ ) { | |
this.life -= 1; | |
} | |
this.render_life(); | |
//if ( this.life > 0 ) { | |
// this.createTask(); | |
//} else { | |
//this.gameover(); | |
//} | |
} | |
} else { | |
this.bigword.value = "PAUSED"; | |
this.bigword.visible = true; | |
} | |
} | |
} | |
new Stage(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment