Commit ae8d4172 authored by Đặng Minh Chiến's avatar Đặng Minh Chiến

Move Player

parent 08b63531
...@@ -41351,172 +41351,18 @@ ...@@ -41351,172 +41351,18 @@
"__id__": 565 "__id__": 565
}, },
"_enabled": true, "_enabled": true,
"containerLevel": { "containerLevel": null,
"__id__": 929 "wall1": [],
}, "wall2": [],
"wall1": [ "wall3": [],
{ "stoneSF": [],
"__uuid__": "2bc2c952-7e63-4979-bcdb-dc6356c5a7d6" "appleSF": [],
}, "apple": null,
{ "stone": null,
"__uuid__": "9c83220e-7f00-4b46-9318-755e4fcfb23b" "trap": null,
}, "holeEnd": null,
{ "saw": null,
"__uuid__": "34eb87b7-174c-4690-8aeb-a147f5adf1cc" "snake": null,
},
{
"__uuid__": "79939907-3355-4886-abfc-1629a600c064"
},
{
"__uuid__": "36e12fd2-dce4-47f8-81b7-39cfa70380ea"
},
{
"__uuid__": "07593af5-2d69-41fb-a44f-6f1322e6d237"
},
{
"__uuid__": "354e994a-97eb-4c83-997a-cb6a9b07fb35"
},
{
"__uuid__": "dc7a4991-ef7f-4180-8bde-b5d41f8b9e30"
},
{
"__uuid__": "f3a4b227-850e-4581-bcf2-f22e82110391"
},
{
"__uuid__": "bf22df6f-4200-4a04-93d5-61e3af17914d"
},
{
"__uuid__": "8950a0b3-d5e9-44bf-ac29-1f427ec02fcf"
},
{
"__uuid__": "0c7e5776-fd24-4848-bfc0-c87e05c1b420"
},
{
"__uuid__": "865e80ed-76cd-45ad-acc7-3b335772a51e"
}
],
"wall2": [
{
"__uuid__": "6605df7d-630b-4c4c-b02e-fc72172a4ec8"
},
{
"__uuid__": "bfabb72b-e6f3-4a6a-8f05-fc9c29a27b9c"
},
{
"__uuid__": "872e2072-6b08-496e-8f1f-42ae6f1b375d"
},
{
"__uuid__": "d76ca9a4-4875-4975-9cb9-1bc50a025211"
},
{
"__uuid__": "cabe52da-c4d0-408c-a033-ee89a8b38f55"
},
{
"__uuid__": "c012ae0b-ab40-4cd7-a4f3-0d3b0d24fb13"
},
{
"__uuid__": "d000b288-87f7-474b-9fdb-8eb778d8d26e"
},
{
"__uuid__": "56db84de-3e75-4c36-9c0f-86d02eab1e56"
},
{
"__uuid__": "3555eab1-795b-4a4e-aa2c-1435738e1053"
},
{
"__uuid__": "8697d85e-b93c-4edc-8c22-47f506d9a217"
},
{
"__uuid__": "2da5b9d9-eb60-4583-b3ed-e8c2fa749b0b"
},
{
"__uuid__": "ff96d8ad-b7d2-489d-a794-2f774609ff7a"
},
{
"__uuid__": "0c7f9564-9908-4ccf-a0f7-0d0fe9bfcdde"
}
],
"wall3": [
{
"__uuid__": "1cc5e425-3ac5-40bc-86cc-3cddca979a47"
},
{
"__uuid__": "6af8f091-100c-4537-a6e1-931cc5739842"
},
{
"__uuid__": "9015183b-af87-4d5a-9991-58e70af7c2fa"
},
{
"__uuid__": "3c68d4cc-fac8-4076-ab9b-f2ac28905ff6"
},
{
"__uuid__": "4ebb3bc9-1131-4d0a-87e9-33198f46b2f4"
},
{
"__uuid__": "444c26c8-fe46-436b-b9d3-4744d1d1cef1"
},
{
"__uuid__": "fe8e9b88-f98a-4ea6-b753-b33bd571b66b"
},
{
"__uuid__": "b58a8932-f818-4f4c-82bf-a08b3a1456ba"
},
{
"__uuid__": "ee92c44e-fda1-4f45-a80c-5e171844eb90"
},
{
"__uuid__": "25840e45-22c7-471a-a2aa-7773a00bcd07"
},
{
"__uuid__": "d75ea4c0-5f67-4404-8e6d-0d4892239b6a"
},
{
"__uuid__": "cbd736b3-789e-463d-8020-54bf2bfa33b3"
},
{
"__uuid__": "b2cd38b8-6114-4e8a-a264-301fac9e2cd2"
}
],
"stoneSF": [
{
"__uuid__": "7a954354-170c-41b6-86c7-ceb9fd3e7418"
},
{
"__uuid__": "632ae859-c764-43f8-b056-f83d88eb5ae5"
},
{
"__uuid__": "fb7b892f-1f5c-4d07-84da-1bc7897dcfb9"
}
],
"appleSF": [
{
"__uuid__": "929a5818-51dc-417a-a7a8-6f40f300aae8"
},
{
"__uuid__": "54c3ea5b-3255-4dbf-9a87-6ce0f24d58f7"
},
{
"__uuid__": "7a2cfb18-45a7-4de9-940e-fbad5217a883"
}
],
"apple": {
"__uuid__": "69ba8df3-7c61-4497-a754-c2b025d0f54a"
},
"stone": {
"__uuid__": "063b1544-8c7f-4cd2-8271-cc1fe6b0cb02"
},
"trap": {
"__uuid__": "7dd2ad06-19a4-4f00-badd-8550dabdc87e"
},
"holeEnd": {
"__uuid__": "37809793-c141-4848-be7b-dc23f7baf907"
},
"saw": {
"__uuid__": "2fcd88ae-69bc-4fb8-8adb-c7ccf1302839"
},
"snake": {
"__id__": 563
},
"_id": "4a8YupnM5Fq7gvglCVFmO7" "_id": "4a8YupnM5Fq7gvglCVFmO7"
}, },
{ {
...@@ -64,7 +64,7 @@ export default class GameManager extends cc.Component { ...@@ -64,7 +64,7 @@ export default class GameManager extends cc.Component {
//let idBackground = this.getIDBackground(level); //let idBackground = this.getIDBackground(level);
// LocalStorage.setBackground(idBackground); // LocalStorage.setBackground(idBackground);
// this.uiManager.setBackground(idBackground); // this.uiManager.setBackground(idBackground);
MapManager.Instance.generateMapByLevel(level); MapManager.Instance.generateMapAndPlayerByLevel(level);
} }
getIDBackground(n: number): number { getIDBackground(n: number): number {
......
import Apple from "./Apple";
import Brick from "./Brick";
import DataLevel from "./DataLevel";
import { TYPEMAP } from "./NodeType";
import Saw from "./Saw";
import Stone from "./Stone";
import Trap from "./Trap";
const { ccclass, property } = cc._decorator;
@ccclass
export default class MapLevel extends cc.Component {
mapWall: number[][] = [];
listApple: { x: number, y: number, type: number, obj: Apple }[] = [];
listSaw: { x: number, y: number, type: number, obj: Saw }[] = [];
listTrap: { x: number, y: number, type: number, obj: Trap }[] = [];
listStone: { x: number, y: number, type: number, obj: Stone }[] = [];
posHoleEnd: { x: number, y: number, type: number } = null;
listSpWall: cc.SpriteFrame[] = [];
trap: cc.Prefab = null;
saw: cc.Prefab = null;
holeEnd: cc.Prefab = null;
apple: cc.Prefab = null;
stone: cc.Prefab = null;
sizeMap: number = null;
offsetMap: number = null;
stoneSF: cc.SpriteFrame = null;
appleSF: cc.SpriteFrame = null;
is18: boolean = true;
generateMapByLevel(dataLevel: DataLevel, listSpWall: cc.SpriteFrame[], stoneSF: cc.SpriteFrame, appleSF: cc.SpriteFrame, trap: cc.Prefab, saw: cc.Prefab, holeEnd: cc.Prefab, apple: cc.Prefab, stone: cc.Prefab, sizeMap: number, offsetMap: number, is18: boolean) {
this.listSpWall = listSpWall;
this.trap = trap;
this.saw = saw;
this.holeEnd = holeEnd;
this.apple = apple;
this.stone = stone;
this.stoneSF = stoneSF;
this.appleSF = appleSF;
this.sizeMap = sizeMap;
this.offsetMap = offsetMap;
this.is18 = is18;
this.mapWall = [];
for (let y = 0; y < dataLevel.height; y++) {
this.mapWall.push([]);
for (let x = 0; x < dataLevel.width; x++) {
this.mapWall[y].push(TYPEMAP.EMPTY);
}
}
let count = dataLevel.bricks.length;
for (const brick of dataLevel.bricks) {
let brickNode: cc.Node = this.instanceNodeById(brick);
if (brickNode != null) {
brickNode.zIndex = count--;
}
}
}
instanceNodeById(brickData: Brick): cc.Node {
let node: cc.Node = null;
switch (brickData.id) {
case TYPEMAP.WALL:
let brick = new cc.Node();
brick.name = "wall_" + brickData.id;
brick.parent = this.node;
brick.addComponent(cc.Sprite);
if (brickData.index) {
brick.getComponent(cc.Sprite).spriteFrame = this.listSpWall[brickData.index];
} else {
brick.getComponent(cc.Sprite).spriteFrame = this.listSpWall[12];
}
brick.width = this.sizeMap;
brick.height = this.sizeMap;
brick.position = cc.v3(this.offsetMap * brickData.x, this.offsetMap * brickData.y);
this.mapWall[brickData.y][brickData.x] = TYPEMAP.WALL;
node = brick;
break;
case TYPEMAP.STONE:
let stone: cc.Node = cc.instantiate(this.stone);
stone.parent = this.node;
stone.addComponent(cc.Sprite).spriteFrame = this.stoneSF;
stone.width = this.offsetMap;
stone.height = this.offsetMap;
stone.position = cc.v3(this.offsetMap * brickData.x, this.offsetMap * brickData.y);
node = stone;
this.listStone.push({
x: brickData.x,
y: brickData.y,
type: TYPEMAP.STONE,
obj: stone.getComponent(Stone),
})
break;
case TYPEMAP.TRAP:
let trap: cc.Node = cc.instantiate(this.trap);
trap.parent = this.node;
trap.getComponent(Trap).updateSize(this.is18);
trap.position = cc.v3(this.offsetMap * brickData.x, this.offsetMap * brickData.y);
this.listTrap.push({
x: brickData.x,
y: brickData.y,
type: TYPEMAP.TRAP,
obj: trap.getComponent(Trap),
});
node = trap;
break;
case TYPEMAP.APPLE:
let apple: cc.Node = cc.instantiate(this.apple);
apple.parent = this.node
apple.getComponent(cc.Sprite).spriteFrame = this.appleSF;
apple.width = this.offsetMap;
apple.height = this.offsetMap;
apple.position = cc.v3(this.offsetMap * brickData.x, this.offsetMap * brickData.y);
node = apple;
this.listApple.push({
x: brickData.x,
y: brickData.y,
type: TYPEMAP.APPLE,
obj: apple.getComponent(Apple),
});
break;
case TYPEMAP.SAW:
let saw: cc.Node = cc.instantiate(this.saw);
saw.parent = this.node
saw.width = this.offsetMap;
saw.height = this.offsetMap;
saw.position = cc.v3(this.offsetMap * brickData.x, this.offsetMap * brickData.y);
node = saw;
this.listSaw.push({
x: brickData.x,
y: brickData.y,
type: TYPEMAP.SAW,
obj: saw.getComponent(Saw),
});
break;
case TYPEMAP.END:
let end: cc.Node = cc.instantiate(this.holeEnd);
end.parent = this.node
end.position = cc.v3(this.offsetMap * brickData.x, this.offsetMap * brickData.y);
this.posHoleEnd = {
x: brickData.x,
y: brickData.y,
type: TYPEMAP.END,
}
break;
}
return node;
}
//Stone
findStone(posStone){
for (let index = 0; index < this.listStone.length; index++) {
if (this.listStone[index].x == posStone.x || this.listStone[index].y == posStone.y) {
return index;
}
}
}
updateAndMoveStonePos(indexStone: number, newPos){
if (indexStone) {
this.listStone[indexStone] = {...newPos, ...this.listStone[indexStone]}
this.listStone[indexStone].obj.moveStoneTo({x: newPos.x * this.offsetMap, y: newPos.x * this.offsetMap})
}
}
}
{
"ver": "1.1.0",
"uuid": "622e1abd-172c-47b4-acf0-3e814f6b337c",
"importer": "typescript",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}
\ No newline at end of file
import UIManager from "../UI/UIManager"; import UIManager from "../UI/UIManager";
import Brick from "./Brick";
import DataLevel from "./DataLevel"; import DataLevel from "./DataLevel";
import GameManager from "./GameManager"; import GameManager from "./GameManager";
import { TYPEMAP } from "./NodeType"; import MapLevel from "./MapLevel";
import Saw from "./Saw";
import Snake from "./Snake"; import Snake from "./Snake";
import Trap from "./Trap";
import Wall from "./Wall";
import Worm from "./Worm"; import Worm from "./Worm";
const { ccclass, property } = cc._decorator; const { ccclass, property } = cc._decorator;
...@@ -52,14 +48,10 @@ export default class MapManager extends cc.Component { ...@@ -52,14 +48,10 @@ export default class MapManager extends cc.Component {
@property(Snake) @property(Snake)
snake: Snake = null; snake: Snake = null;
map: number[][] = []; mapLevel: MapLevel = null;
private listLevelInstance: number[] = []; private listLevelInstance: number[] = [];
private listSpWall: cc.SpriteFrame[] = [];
private idBackground: number = 1; private idBackground: number = 1;
private nodeParentPlatform: cc.Node = null;
private is18: boolean = true; private is18: boolean = true;
private offsetMap: number = 60; private offsetMap: number = 60;
private sizeMap: number = 91 private sizeMap: number = 91
...@@ -68,21 +60,36 @@ export default class MapManager extends cc.Component { ...@@ -68,21 +60,36 @@ export default class MapManager extends cc.Component {
MapManager.Instance = this; MapManager.Instance = this;
} }
generateMapByLevel(level) { generateMapAndPlayerByLevel(level) {
let listSpWall: cc.SpriteFrame[] = [];
let spApple: cc.SpriteFrame = null;
let spStone: cc.SpriteFrame = null;
this.idBackground = this.getID(level + 1); this.idBackground = this.getID(level + 1);
UIManager.Instance.setBackground(this.idBackground); UIManager.Instance.setBackground(this.idBackground);
if (this.idBackground == 1) { if (this.idBackground == 1) {
this.listSpWall = this.wall1; listSpWall = this.wall1;
spApple = this.appleSF[0];
spStone = this.stoneSF[0];
} else if (this.idBackground == 2) { } else if (this.idBackground == 2) {
this.listSpWall = this.wall2; listSpWall = this.wall2;
spApple = this.appleSF[1];
spStone = this.stoneSF[1];
} else { } else {
this.listSpWall = this.wall3; listSpWall = this.wall3;
spApple = this.appleSF[2];
spStone = this.stoneSF[2];
} }
this.generatePlayerByLevel(level); this.generatePlayerByLevel(level);
if (this.listLevelInstance.includes(level)) { if (this.listLevelInstance.includes(level)) {
const childNodeLevel = this.containerLevel.getChildByName("NodePlatformLevel_" + level); const childNodeLevel = this.containerLevel.getChildByName("NodeMapLevel_" + level);
if (childNodeLevel) { if (childNodeLevel) {
this.mapLevel = childNodeLevel.getComponent(MapLevel);
childNodeLevel.active = true; childNodeLevel.active = true;
} }
} else { } else {
let dataLevel: DataLevel = GameManager.Instance.dataLevels[level]; let dataLevel: DataLevel = GameManager.Instance.dataLevels[level];
...@@ -99,90 +106,17 @@ export default class MapManager extends cc.Component { ...@@ -99,90 +106,17 @@ export default class MapManager extends cc.Component {
this.offsetMap = 50; this.offsetMap = 50;
this.is18 = false; this.is18 = false;
} }
for (let y = 0; y < height; y++) {
this.map.push([]);
for (let x = 0; x < width; x++) {
this.map[y].push(null);
}
}
this.nodeParentPlatform = new cc.Node(); let nodeMapLevel = new cc.Node();
this.nodeParentPlatform.name = "NodePlatformLevel_" + level; nodeMapLevel.name = "NodeMapLevel_" + level;
this.nodeParentPlatform.position = cc.v3(0, 0); nodeMapLevel.position = cc.v3(0, 0);
this.nodeParentPlatform.parent = this.containerLevel; nodeMapLevel.parent = this.containerLevel;
let count = dataLevel.bricks.length; nodeMapLevel.addComponent(MapLevel).generateMapByLevel(dataLevel, listSpWall, spStone, spApple, this.trap, this.saw, this.holeEnd, this.apple, this.stone, this.sizeMap, this.offsetMap, this.is18);
for (const brick of dataLevel.bricks) {
let brickNode: cc.Node = this.instanceNodeById(brick);
if (brickNode != null) {
brickNode.zIndex = count--;
}
this.map[brick.y][brick.x] = brick.id;
}
this.listLevelInstance.push(level); this.listLevelInstance.push(level);
this.mapLevel = nodeMapLevel.getComponent(MapLevel);
} }
} }
instanceNodeById(brickData: Brick): cc.Node {
let node: cc.Node = null;
switch (brickData.id) {
case TYPEMAP.WALL:
let brick = new cc.Node();
brick.name = "wall_" + brickData.id;
brick.parent = this.nodeParentPlatform;
brick.addComponent(cc.Sprite);
if (brickData.index) {
brick.getComponent(cc.Sprite).spriteFrame = this.listSpWall[brickData.index];
} else {
brick.getComponent(cc.Sprite).spriteFrame = this.listSpWall[12];
}
brick.width = this.sizeMap;
brick.height = this.sizeMap;
brick.position = cc.v3(this.offsetMap * brickData.x, this.offsetMap * brickData.y);
node = brick;
break;
case TYPEMAP.STONE:
let stone: cc.Node = cc.instantiate(this.stone);
stone.parent = this.nodeParentPlatform;
stone.addComponent(cc.Sprite).spriteFrame = this.stoneSF[this.idBackground - 1];
stone.width = this.offsetMap;
stone.height = this.offsetMap;
stone.position = cc.v3(this.offsetMap * brickData.x, this.offsetMap * brickData.y);
node = stone;
break;
case TYPEMAP.TRAP:
let trap: cc.Node = cc.instantiate(this.trap);
trap.parent = this.nodeParentPlatform;
trap.getComponent(Trap).updateSize(this.is18);
trap.position = cc.v3(this.offsetMap * brickData.x, this.offsetMap * brickData.y);
node = trap;
break;
case TYPEMAP.APPLE:
let apple: cc.Node = cc.instantiate(this.apple);
apple.parent = this.nodeParentPlatform;
apple.getComponent(cc.Sprite).spriteFrame = this.appleSF[this.idBackground - 1];
apple.width = this.offsetMap;
apple.height = this.offsetMap;
apple.position = cc.v3(this.offsetMap * brickData.x, this.offsetMap * brickData.y);
node = apple;
break;
case TYPEMAP.SAW:
let saw: cc.Node = cc.instantiate(this.saw);
saw.parent = this.nodeParentPlatform;
saw.width = this.offsetMap;
saw.height = this.offsetMap;
saw.position = cc.v3(this.offsetMap * brickData.x, this.offsetMap * brickData.y);
node = saw;
break;
case TYPEMAP.END:
let end: cc.Node = cc.instantiate(this.holeEnd);
end.parent = this.nodeParentPlatform;
end.position = cc.v3(this.offsetMap * brickData.x, this.offsetMap * brickData.y);
node = end;
break;
}
return node;
}
getID(n: number): number { getID(n: number): number {
if (n < 1) { if (n < 1) {
throw new Error("Số truyền vào phải lớn hơn hoặc bằng 1"); throw new Error("Số truyền vào phải lớn hơn hoặc bằng 1");
...@@ -191,8 +125,9 @@ export default class MapManager extends cc.Component { ...@@ -191,8 +125,9 @@ export default class MapManager extends cc.Component {
} }
generatePlayerByLevel(level: number) { generatePlayerByLevel(level: number) {
let worms: Worm [] = GameManager.Instance.dataLevels[level].worms; let worms: Worm[] = GameManager.Instance.dataLevels[level].worms;
this.snake.initializeSnakeView(worms, this.offsetMap, this.is18) this.snake.initializeSnakeView(worms, this.offsetMap, this.is18)
} }
} }
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
// - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html // - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html
import Worm from "./Worm"; import Worm from "./Worm";
import { TYPEMAP } from './NodeType';
import MapManager from "./MapManager";
const { ccclass, property } = cc._decorator; const { ccclass, property } = cc._decorator;
...@@ -41,21 +43,26 @@ export default class Snake extends cc.Component { ...@@ -41,21 +43,26 @@ export default class Snake extends cc.Component {
@property(cc.SpriteFrame) @property(cc.SpriteFrame)
spTailPale: cc.SpriteFrame = null; spTailPale: cc.SpriteFrame = null;
bodies: cc.Node[] = []; bodies: { x: number, y: number, type: number, obj: cc.Node }[] = [];
offset: number = 0; offset: number = 0;
isMap18: boolean = true; isMap18: boolean = true;
sizeBody: number = 60; sizeBody: number = 60;
width: number = 18;
height: number = 10;
private headerPos: cc.Vec3 = null; private headerPos: cc.Vec3 = null;
initializeSnakeView(worms: Worm[], offset: number, isMap18: boolean) { initializeSnakeView(worms: Worm[], offset: number, isMap18: boolean) {
if (isMap18) { if (isMap18) {
this.node.position.x = -535; this.node.position.x = -535;
this.width = 18;
this.height = 10;
} else { } else {
this.node.position.x = -530; this.node.position.x = -530;
this.width = 22;
this.height = 12;
} }
//this.node.removeAllChildren(); this.node.removeAllChildren();
for (let index = 0; index < worms.length; index++) { for (let index = 0; index < worms.length; index++) {
if (worms[index].stt == 0) { if (worms[index].stt == 0) {
let header: cc.Node = cc.instantiate(this.header); let header: cc.Node = cc.instantiate(this.header);
...@@ -72,8 +79,8 @@ export default class Snake extends cc.Component { ...@@ -72,8 +79,8 @@ export default class Snake extends cc.Component {
header.height = 50; header.height = 50;
} }
this.rotationHead(header, worms[index], worms[index + 1]) this.rotationHead(header, worms[index], worms[index + 1])
} this.bodies.push({ x: worms[index].x, y: worms[index].y, type: TYPEMAP.HEAD, obj: header });
if (worms[index].stt > 0 && worms[index].stt < worms[worms.length - 1].stt && worms.length > 2) { } else if (worms[index].stt > 0 && worms[index].stt < worms[worms.length - 1].stt && worms.length > 2) {
let body: cc.Node = cc.instantiate(this.body); let body: cc.Node = cc.instantiate(this.body);
body.parent = this.node; body.parent = this.node;
this.rotationBody(body, worms[index - 1], worms[index + 1], worms[index]); this.rotationBody(body, worms[index - 1], worms[index + 1], worms[index]);
...@@ -87,7 +94,7 @@ export default class Snake extends cc.Component { ...@@ -87,7 +94,7 @@ export default class Snake extends cc.Component {
body.height = 100; body.height = 100;
} }
body.position = cc.v3(offset * worms[index].x, offset * worms[index].y); body.position = cc.v3(offset * worms[index].x, offset * worms[index].y);
this.bodies.push({ x: worms[index].x, y: worms[index].y, type: TYPEMAP.HEAD, obj: body });
} else if (worms[index].stt == worms[worms.length - 1].stt) { } else if (worms[index].stt == worms[worms.length - 1].stt) {
let tail: cc.Node = cc.instantiate(this.tail); let tail: cc.Node = cc.instantiate(this.tail);
tail.parent = this.node; tail.parent = this.node;
...@@ -107,6 +114,7 @@ export default class Snake extends cc.Component { ...@@ -107,6 +114,7 @@ export default class Snake extends cc.Component {
} else if (worms[index].y == worms[index].y) { } else if (worms[index].y == worms[index].y) {
tail.rotation = 0; tail.rotation = 0;
} }
this.bodies.push({ x: worms[index].x, y: worms[index].y, type: TYPEMAP.HEAD, obj: tail });
} }
} }
} }
...@@ -175,31 +183,91 @@ export default class Snake extends cc.Component { ...@@ -175,31 +183,91 @@ export default class Snake extends cc.Component {
} }
} }
moveUp(){ moveUp() {
// this.isMoveValid(1, cc.v2(1, 0));
}
moveDown() {
} }
moveDown(){ moveLeft() {
} }
moveLeft(){ moveRight() {
} }
moveRight(){ handlePlayerInput(displcement: cc.Vec2) {
}
async moveSnakeAndPushStone(displcement: cc.Vec2) {
let posHeadMap = { x: this.headerPos.x / 60, y: this.headerPos.y / 60 };
let newPos = { x: posHeadMap.x + displcement.x, y: posHeadMap.y + displcement.y };
if (!this.isMoveValid(newPos)) {
//ToDo
return;
}
if (this.isNeedToPushStone(newPos)) {
let originaStonePos = newPos;
let newStonePos = { x: posHeadMap.x + (displcement.x * 2), y: posHeadMap.y + (displcement.y * 2) };
let indexStone = MapManager.Instance.mapLevel.findStone(originaStonePos)
MapManager.Instance.mapLevel.updateAndMoveStonePos(indexStone, newStonePos);
}
} }
handlePlayerInput(displcement: cc.Vec3){ checkAndHandleFallings() {
} }
moveSnakeAndPushStone(displcement: cc.Vec3){ isMoveValid(newPos) {
if (newPos.x > this.width || newPos.x < 0 || newPos.y > this.height || this.height < 0){
return true;
}
if (this.isSpaceEmptyFor(newPos)) {
return true;
}
if (this.isNeedToPushStone(newPos)) {
return true;
}
}
isSpaceEmptyFor(pos) {
return this.getMovementMapFor()[pos.y][pos.x] == TYPEMAP.EMPTY ? true : false;
} }
checkAndHandleFallings(){ isNeedToPushStone(pos)
{
return this.getMovementMapFor()[pos.y][pos.x] == TYPEMAP.STONE ? true : false;
}
getMovementMapFor() {
let array: number[][] = this.clone2DArray(MapManager.Instance.mapLevel.mapWall);
let arrayStone = MapManager.Instance.mapLevel.listStone;
let arrayApple = MapManager.Instance.mapLevel.listApple;
for (let i = 0; i < this.bodies.length; i++) {
if (this.bodies[i].type == TYPEMAP.HEAD) {
array[this.bodies[i].y][this.bodies[i].x] = TYPEMAP.HEAD;
} else {
array[this.bodies[i].y][this.bodies[i].x] = TYPEMAP.BODY;
}
} }
for (let i = 0; i < arrayStone.length; i++) {
array[arrayStone[i].y][arrayStone[i].x] = TYPEMAP.STONE;
}
for (let i = 0; i < arrayApple.length; i++) {
array[arrayApple[i].y][arrayApple[i].x] = TYPEMAP.APPLE;
}
return array;
}
clone2DArray(arr: number[][]): number[][] {
return arr.map((subArray) => subArray.slice());
}
} }
...@@ -3,5 +3,10 @@ const {ccclass, property} = cc._decorator; ...@@ -3,5 +3,10 @@ const {ccclass, property} = cc._decorator;
@ccclass @ccclass
export default class Stone extends cc.Component { export default class Stone extends cc.Component {
moveStoneTo(newPos){
cc.tween(this.node)
.to(0.4, { position: cc.v3(newPos.x, newPos.y)})
.start();
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment