|
??
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????Field Runner??????????????????
????????copy???????????????????????????????????????????????????????????????????????????????????????????
??????
??
?????????egret???????????????api???????????????????egret???????
??????????typescript????egret???????????
?????????????????????????????
??
????????????????????????????????????????????????????????????????????????????????????????????
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
???????????????????
??
???????????????????????????????????????????????????UI?????
- // Main.ts
- class Main extends eui.UILayer {
- //????...
- // ??????????
- // Map???????????
- protected createGameScene(): void {
- this.addChild(new Map());
- }
- }
复制代码
???????
?????????Map?????????????????????????????????????????????????
- // Map.ts
- // ??????????
- class Map extends egret.DisplayObjectContainer {
- //????...
- // ??????
- public static startPoint:[number, number] = [1, 11];
- // ???????
- public static tileWidth:number = 800;
- public static tileHeight:number = 500;
- // ?????,???? ? 20?? 10
- public static mapWidth:number = 20;
- public static mapHeight:number = 10;
- // ???????????????????
- public static weaponPoinit:[number, number] = [100, 111];
- // ????
- public static stopPoinit:[number, number] = [10, 111];
- // ??????
- public static moneyPoinit:[number, number] = [10, 10];
- // ??????
- public static scorePoinit:[number, number] = [30, 10];
- // ???????
- public static lifePoinit:[number, number] = [80, 10];
- // ????...
- }
复制代码
?????????????????????????????????????????????????????????
- // Weapon.ts
- private onAddToStage() {
- this.gatingdIcon = this.createBitmapByName("gatingdIcon_png");
- this.gatingdIcon.x = Map.weaponPoinit[0];
- this.gatingdIcon.y = Map.weaponPoinit[1];
- this.gatingdIcon.width = 100;
- this.gatingdIcon.height = 100;
- // ?????????
- this.parent.addChild(this.gatingdIcon);
- }
复制代码
???????????????????????????????????????????????????????????Tiled map????2d?????????????????tiled?????????????????????????????????????????????????????????????????
?????????????????????????????????????
???????????????????egret?????????????????????????????????
?Map?????????getMapObj ???????tiled???????????????????????????????????????????????????
- // Map.ts
- class Map extends egret.DisplayObjectContainer {
- public static tmxTileMap: tiled.TMXTilemap;
- public static getMapObj(parentName:string, targetName: string) {
- let toolMap:any = Map.tmxTileMap.getChildByName(parentName);
- let childrens = toolMap._childrens || [];
- let targetObj;
- childrens.map(child => {
- if (child.$name == targetName) {
- targetObj = child;
- }
- });
- return targetObj;
- }
- }
复制代码
???????????????tiled???????????'tool'?????????????????????'gatingdIcon'???
??????????????????????
- // Weapon.ts
- private onAddToStage() {
- const tagetMap = Map.getMapObj('tool', 'gatingdIcon');
- if (tagetMap) {
- this.gatingdIcon = this.createBitmapByName("gatingdIcon_png");
- this.gatingdIcon.x = targetMap.$x;
- this.gatingdIcon.y = targetMap.$y;
- this.gatingdIcon.width = targetMap.$width;
- this.gatingdIcon.height = targetMap.$height;
- // ?????????, this.parent ??????
- this.parent.addChild(this.gatingdIcon);
- }
- }
复制代码
???????????????????????
??????????????????????????????????????Tiled????????????????????????????????????????XY???
???????????
???????????????????????????????????1??????????????????????????????????????????????????
???????????????????????
- // x?y???????????????
- private getAvailablePositionNearby(pointX:number, pointY:number) {
- // tx?ty??????????????
- const tx = Math.round(pointX - startPointX / tileWidth);
- const tx = Math.round(pointY - startPointY / tileHeight);
- // x?y?????????????????
- const x = startPointX + tx * tileWidth;
- const y = startPointY + ty * tileHeight;
- return {x: x, y: y, tx: tx, ty: ty};
- }
复制代码
?????????????????????????????????????
- // ????
- private placeWeapon(weapon:Gatling) {
- const point = this.getAvailablePositionNearby(pointX, pointY);
- if (point) {
- this.dragWeapon.x = point.x;
- this.dragWeapon.y = point.y;
- this.dragWeapon.tx = point.tx;
- this.dragWeapon.ty = point.ty;
- }
- }
复制代码
????????????????????????????????????????????????????????????????????
- private allowBoolean(point) {
- let bool = false;
- if (point) {
- if (
- (point.tx==0 && point.ty==1) ||
- point.tx < 0 ||
- point.tx > mapWidth ||
- point.ty < mapHeight ||
- // ?????????????
- this.player.getWeaponAt(point.tx, point.ty) ||
- // ??????????????????????
- !this.player.buildPath(point.tx, point.ty)
- ) {
- bool = false;
- } else {
- bool = true;
- }
- }
- return bool;
- }
复制代码
?????????????
??????????????????????????????????????????????????????????????????????????????????????????????
??????????????
- private setPoint() {
- const startMap = Map.getMapObj('soldierBirthPool', 'pointStart');
- const endMap = Map.getMapObj('soldierBirthPool', 'pointEnd');
- this.startPoint = [parseInt(startMap.$x, 10), parseInt(startMap.$y, 10)];
- this.endPoint = [parseInt(endMap.$x, 10), parseInt(endMap.$y, 10)];
- }
复制代码
????????????????????????????????????????????????????
????????????????-??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????1????????????????Astar?????????????????????????????????????????????????????????
??????????????
- private manhattan(start, end) {
- return Math.abs(start.x - end.x) + Math.abs(start.y - end.y);
- }
复制代码
?????????????????A*????????????????????????Astar?????????????????????????????????????
???????????????????????????????????????????????????????????????????????????????????????????????????????
?????????????
- // ????????
- public buildPath(tx?, ty?, start?, end?) {
- // ?
- let map = [];
- // ????????
- let s = start;
- // ????????
- let e = end;
- if (!s || s[0] < 0) {
- s = this.startPoint;
- }
- if (!end) {
- e = this.endPoint;
- }
- // ???????????
- for (let i = 0; i < this.mapHeight; i++) {
- map = [];
- for (let j = 0; j < this.mapWidth; j++) {
- let hasWeaponAt = this.getWeaponAt(j, i);
- // ??????????????????????????? 1??????0 ?? 1??????????
- map[j] = hasWeaponAt ? 1 : 0;
- }
- }
- // ????????????????? 1, ????
- if (tx || ty) {
- map[ty][tx]= 1;
- }
- // Astar??????????????????
- let pathArr = Astar.findPath(map, s, e) || [];
- return pathArr;
- }
复制代码
????buildPath????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
?????????????????????????
- // solider ??
- private getNextDirection(solider:Soldier) {
- for(let i = 0; i < solider.path.length - 1; i++) {
- let path = solider.path;
- // ????????????????????????????? i+1
- if (path[0] == solider.tx && path[1] == solider.ty) {
- // next?????????????
- let next = solider.path[i+1];
- return [next[0]-solider.tx, next[1]-solider.ty];
- }
- }
- return null;
- }
复制代码
??????????????????????????????????????????????????????????????????????????????????????????????????????xy???
???????????????????????????????????????????
- // Soldier.ts
- // direction[0]????????????direction[0] === 1 ?????direction[0] === -1 ????
- // direction[1]????????????direction[1] === 1 ?????direction[1] === -1 ????
- // ?? 0 ??????
- public direction: [number, number] = [1,0];
- // avatar ?? ?????????????????????
- public avatar: egret.MovieClip;
- // ???????
- public speed:number = 0:
- private setDirection() {
- // ????...
- if (this.direction[0] == 1) {
- // ??????????????gotoAndPlay ?egret?????????
- this.avatar.gotoAndPlay("solider_walk_right", -1);
- }
- // ????...
- }
复制代码
???????????????????????????xy?????????
- // Player.ts
- // ?????????????xy??
- private moveByDirection(solider:Soldier) {
- if (!solider.direction) {
- return;
- }
- if (solider.direction[0] != 0) {
- solider.x += solider.speed * solider.direction[0];
- } else
- if (solider.direction[1] != 0) {
- solider.y += solider.speed * solider.direction[1];
- }
- }
- // ????
- private moveSoldier(solider:Soldier) {
- // direction[0] !=0 ?????x?????
- if (solider.direction[0] != 0) {
- // ??????
- let dx = target.x - ( this.startPoint[0] + tile[0] * this.tileWidth );
- if (dx === 0) {
- // ?????????????xy??
- solider.setDirection(this.getNextDirection(target));
- }
- }
- // ????...
- }
复制代码
?? direction[0] ????????????????????????????????????????????????????????????xy?????????
???????????????????????????????
- ???????????Astar???????????
- ??????????????????????
- ??????????????????????????????????????????????? [next[0]-solider.tx, next[1]-solider.ty]?
- ????????????
??????????
???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????360????????????????????????
????????????????????????
- private onAddToStage() {
- this.gatingdIcon = this.createBitmapByName("gatingdIcon_png");
- const targetMap = Map.getMapObj('tool', 'gatingdIcon');
- if (targetMap) {
- this.gatingdIcon.x = targetMap.$x;
- this.gatingdIcon.y = targetMap.$y;
- this.gatingdIcon.width = targetMap.$width;
- this.gatingdIcon.height = targetMap.$height;
- }
- this.parent.addChild(this.gatingdIcon);
- }
复制代码
????????????????
- private onAddToStage() {
- // ????...
- // ??
- this.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE,
- this.touchMoveHandler, this); // ????? -down?
- this.stage.addEventListener(egret.TouchEvent.TOUCH_BEGIN,
- this.touchBeginHandler, this); // ????? -up?
- this.stage.addEventListener(egret.TouchEvent.TOUCH_END,
- this.touchEndHandler, this);}
复制代码
???????????????????????????????????????????'???????????'??????
??????????????????
???
????????????????????
- public damage:number = 10;
- ???????????????????
- // ????
- public getShot(damage:number) {
- // ????????
- this.health -= damage;
- }
复制代码
?????????????????????????????????????????,????????????????????????
- private maxDamage: number = 20:
- private minDamage: number = 10;
- // ????????
- public getDamange() {
- return Math.round(Math.random()*(this.maxDamage - this.minDamage)) + this.minDamage;
- }
复制代码
????
??????????????
- public attackRadius: nunmber = 200;
复制代码
????????????????
???????????????????????????????????????????????????????????????
?????????????
- public isInAttackRadius(solider: Solider) {
- // ?????????????????????????????
- const dx = solider.x - this.x;
- const dy = solider.y - this.y;
- const distance = Math.sqrt(dx * dx + dy * dy);
- return distance <= this.attackRadius;
- }
复制代码
??????????
??????????????????????360??????????????????????????????????
?????????
???????????????????????????????????????????????????0????????????
??????????:
- // ??????????300ms??????
- private checkShot() {
- if (this.fireTime > 300) {
- this.fireTime = 0;
- this.turnTime = new Date().getTime();
- return true;
- }
- return false;
- }
- //
- public hit(soldier: Soldier) {
- let status;
- // ?????????????????(+180)
- const angle = (180 / Math.PI) * Math.atan2(dy, dx) + 180;
- // ????????10???
- const frame = Math.round(angle/10);
- const hitBool = this.isInAttackRadius(soldier);
- this.status = status;
- this.currentAngleFrame = frame;
- if (hitBool) {
- if (this.status === 'idle') {
- this.trunTime = new Date().getTime();
- this.fireTime = 0;
- } else {
- this.fireTime = new Date().getTime() - this.trunTime;
- }
- return this.checkShot();
- }
- return false;
- }
复制代码
??????????atan2?????atan???atan???(y/x)?(-y/-x)?????????????????????
????
????????????????????????????????????, ??????????????????????
- // ???????????
- private canUpgrade() {
- return this.level < 8;
- }
- public upgrade() {
- if (!this.canUpgrade()) {
- return false;
- }
- this.level ++;
- this.cost += 20;
- this.minDamage += 12;
- // ????...
- }
复制代码
????????????????????????????????????
??
??????????????????????????????????????????????????????
- public autoAttack() {
- // ????...
- if (weapon.solider === null
- || !weapon.checkInAttackRadius(solider)
- || (weapon.solider.x >= (this.stage.stageWidth + weapon.width))
- ) {
- weapon.solider = this.findSolider(weapon);
- }
- }
复制代码
?????????
??????????????????????????????????????
????????????????????????????????????????????????????????????????
???????????
- public getShot(damage:number) {
- // ????????
- this.health -= damage;
- // ????????0??????????0.?????????
- if (this.health < 0) {
- this.health = 0;
- }
- const percent = this.health / this.maxHealth;
- // ??????,????????????60
- let healthWidth = Math.round(60 * percent);
- // this.healthBar ???????
- if (this.healthBar) {
- this.healthBar.width = healthWidth;
- }
- }
复制代码
??????????????
?????
- public autoAttack() {
- if (solider.isDead()) {
- // ??
- this.score += solider.score;
- // ???
- this.money += solider.money;
- }
- }
复制代码
??
??????????????????
??
???????????????
??????????????????
- public autoAttack() {
- // ????...
- if (solider.x >= this.stage.stageWidth + solider.width) {
- this.life-- ;
- }
- }
复制代码
??????
?????????????????????????????????????????
??
??????????????????????????????
????????534591395@qq.com?
github: https://github.com/534591395
??????????
???https://mp.weixin.qq.com/s/gW-inHVEjoandKslLhqwEw
|
|