import Hilo from "../hilo/Hilo";
console.log("#debug#🚀 ~ file: game.ts:2 ~ Hilo:", Hilo);
import Asset from "./Asset";
console.log("#debug#🚀 ~ file: game.ts:4 ~ Asset:", Asset);
let stage: any;
let stepLong = 200;
let stepTime = 1000;
class Game {
	private stage: any;
	private asset: any;
	private bg: any;
	private width: number;
	private height: number;
	private scale: number = 0.5;
	private man: any;
	private bom: any;

	constructor(container: any, width = 375, height = 667) {
		this.stage = stage = new Hilo.Stage({
			renderType: "canvas",
			container: container,
			width: width,
			height: height,
			scaleX: this.scale,
			scaleY: this.scale
		});
		this.width = width;
		this.height = height;
		Asset.on("complete", () => {
			Asset.off("complete");
			this.init();
		});
		Asset.load();

		var ticker = new Hilo.Ticker(60);
		ticker.addTick(stage);
		ticker.addTick(Hilo.Tween); //需要把Tween加到ticker里才能使用
		ticker.start();
	}
	init() {
		let bgImg: any = Asset.bg;
		let bmpWidth = 1381;
		let bmpHeight = 1043;
		let bg = new Hilo.Bitmap({
			id: "bg",
			image: bgImg,
			rect: [0, 0, bmpWidth, bmpHeight],
			x: 0,
			y: 0,
			scaleX: this.width / bmpWidth,
			scaleY: this.height / bmpHeight
		}).addTo(stage);
		//添加1个人

		this.initMan();
	}
	initMan() {
		console.log("#debug#🚀 ~ file: game.ts:65 ~ Game ~ initMan ~ Asset.getman:", Asset.man), Asset.man.getSprite("man");
		this.man = new Hilo.Sprite({
			frames: Asset.man.getSprite("man"),
			x: 600,
			y: 500,
			scaleX: 0.5,
			scaleY: 0.5,
			interval: 5,
			timeBased: false,
			loop: true,
			paused: true,
			onUpdate: function () {}
		}).addTo(stage);
		console.log("#debug#🚀 ~ file: game.ts:78 ~ Game ~ initMan ~ this.man:", this.man);
		this.bom = new Hilo.Sprite({
			frames: Asset.bom.getSprite("bom"),
			x: 400,
			y: 400,
			scaleX: 1,
			scaleY: 1,
			interval: 8,
			timeBased: false,
			loop: true,
			paused: false,
			onUpdate: function () {}
		}).addTo(stage);
		console.log("#debug#🚀 ~ file: game.ts:90 ~ Game ~ initMan ~ bom:", this.bom);
	}
	goLeft() {
		console.log("#debug#🚀 ~ file: game.ts:81 ~ Game ~ goLeft ~ goLeft:");
		console.log("#debug#🚀 ~ file: game.ts:84 ~ Game ~ goLeft ~ this.man:before", this.man, { x: this.man.x - 100 });
		let man = this.man;
		man.paused = false;
		Hilo.Tween.to(
			man,
			{ x: man.x - stepLong },
			{
				duration: stepTime,
				onComplete: function () {
					man.paused = true;
					console.log("complete");
				}
			}
		);

		console.log("#debug#🚀 ~ file: game.ts:84 ~ Game ~ goLeft ~ this.man:after", this.man);
	}
	goRight() {
		console.log("#debug#🚀 ~ file: game.ts:81 ~ Game ~ goLeft ~ goLeft:");
		console.log("#debug#🚀 ~ file: game.ts:84 ~ Game ~ goLeft ~ this.man:before", this.man, { x: this.man.x - 100 });
		let man = this.man;
		man.paused = false;
		Hilo.Tween.to(
			man,
			{ x: man.x + stepLong },
			{
				duration: stepTime,
				onComplete: function () {
					man.paused = true;
					console.log("complete");
				}
			}
		);

		console.log("#debug#🚀 ~ file: game.ts:84 ~ Game ~ goLeft ~ this.man:after", this.man);
	}
	goUp() {
		console.log("#debug#🚀 ~ file: game.ts:81 ~ Game ~ goLeft ~ goLeft:");
		console.log("#debug#🚀 ~ file: game.ts:84 ~ Game ~ goLeft ~ this.man:before", this.man, { x: this.man.x - 100 });
		let man = this.man;
		man.paused = false;
		Hilo.Tween.to(
			man,
			{ y: man.y - stepLong },
			{
				duration: stepTime,
				onComplete: function () {
					man.paused = true;
					console.log("complete");
				}
			}
		);

		console.log("#debug#🚀 ~ file: game.ts:84 ~ Game ~ goLeft ~ this.man:after", this.man);
	}
	goDown() {
		console.log("#debug#🚀 ~ file: game.ts:81 ~ Game ~ goLeft ~ goLeft:");
		console.log("#debug#🚀 ~ file: game.ts:84 ~ Game ~ goLeft ~ this.man:before", this.man, { x: this.man.x - 100 });
		let man = this.man;
		man.paused = false;
		Hilo.Tween.to(
			man,
			{ y: man.y + stepLong },
			{
				duration: stepTime,
				onComplete: function () {
					man.paused = true;
					console.log("complete");
				}
			}
		);

		console.log("#debug#🚀 ~ file: game.ts:84 ~ Game ~ goLeft ~ this.man:after", this.man);
	}
	startBom() {
		console.log("#debug#🚀 ~ file: game.ts:102 ~ Game ~ startBom ~ startBom:");
		this.bom.x = parseInt((Math.random() * (700 + 1 - 50)).toString()) + 50;
		this.bom.y = parseInt((Math.random() * (500 + 1 - 50)).toString()) + 50;
		this.bom.paused = false;
		this.bom.alpha = 1;
	}
	stopBom() {
		console.log("#debug#🚀 ~ file: game.ts:107 ~ Game ~ stopBom ~ stopBom:");
		this.bom.paused = true;
		this.bom.alpha = 0;
	}
}
export default Game;
