mirror of
https://gitee.com/devil_root/snake.git
synced 2025-10-10 00:56:28 +00:00
the demo
This commit is contained in:
239
assets/Scripts/rolectl/player.ts
Normal file
239
assets/Scripts/rolectl/player.ts
Normal file
@@ -0,0 +1,239 @@
|
||||
import { Component, instantiate, Node, Prefab, tween, UITransform, v2, v3, Vec2, Vec3, _decorator } from 'cc';
|
||||
import WaveMng from '../logics/mng/WaveMng';
|
||||
import { monsterCtl } from '../Test/monsterCtl';
|
||||
import { snakebody } from './snakebody';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('player')
|
||||
export class player extends Component {
|
||||
|
||||
@property(Prefab) bodyPrefab: Prefab = null
|
||||
@property(Node) tmpWeapen: Node = null
|
||||
@property(WaveMng) waveMng: WaveMng = null
|
||||
|
||||
_tmpV2: Vec2 = new Vec2()
|
||||
_tmpV3: Vec3 = new Vec3(0, 1, 0)
|
||||
_tmpV32: Vec3 = new Vec3()
|
||||
dir: Vec3 = new Vec3(0, 1, 0)
|
||||
bodyNum: number = 8
|
||||
sectionLen: number = 38
|
||||
speed: number = 3
|
||||
snakeArray: Node[];
|
||||
|
||||
//蛇头走过的点数量
|
||||
headPointsNum: number = 0
|
||||
// record all points
|
||||
pointsArray = [];
|
||||
|
||||
_isMove: boolean = false
|
||||
|
||||
monsterList: Array<Node> = []
|
||||
|
||||
|
||||
start() {
|
||||
|
||||
this.tmpWeapen.active = false
|
||||
this.headPointsNum = 0
|
||||
this.snakeArray = [];
|
||||
this.pointsArray = []
|
||||
this.snakeArray.push(this.node);
|
||||
|
||||
this.initSnake()
|
||||
}
|
||||
|
||||
initSnake() {
|
||||
this.rotateHead()
|
||||
// return
|
||||
for (let i = 1; i <= this.bodyNum; i++) {
|
||||
this.getNewBody(i);
|
||||
this.recordPoints()
|
||||
}
|
||||
console.log("初始化的节点...", this.pointsArray.length, this._recordTimes)
|
||||
this._recordTimes = 0
|
||||
|
||||
}
|
||||
|
||||
|
||||
rotateHead(headPos: Vec2 = null) {
|
||||
if (!headPos) {
|
||||
headPos = new Vec2(this.dir.x, this.dir.y)
|
||||
}
|
||||
|
||||
let angle = v2(1, 0).signAngle(headPos) * 180 / Math.PI;
|
||||
this.node.angle = angle - 90;
|
||||
}
|
||||
|
||||
getNewBody(bodyIdx: number) {
|
||||
// console.log("this.snakeArray.length ::", this.snakeArray.length)
|
||||
let newBody = instantiate(this.bodyPrefab);
|
||||
let snakeCtl = newBody.getComponent(snakebody)
|
||||
snakeCtl.snakeBodyIdx = 0
|
||||
snakeCtl.preSnakeBody = this.snakeArray[this.snakeArray.length - 1]
|
||||
|
||||
// set new body's position //这是第一个蛇身
|
||||
if (this.snakeArray.length == 1) {
|
||||
let dir = this.dir.normalize()
|
||||
let pos = this.node.getPosition().subtract(dir.multiplyScalar(this.sectionLen))
|
||||
newBody.setPosition(pos);
|
||||
}
|
||||
else {
|
||||
let lastBody = this.snakeArray[this.snakeArray.length - 1];
|
||||
let lastBOBody = this.snakeArray[this.snakeArray.length - 2];
|
||||
lastBody.getPosition(this._tmpV32)
|
||||
lastBOBody.getPosition(this._tmpV3)
|
||||
let dir = this._tmpV3.subtract(this._tmpV32).normalize();
|
||||
let tmpPos = lastBody.getPosition().subtract(dir.multiplyScalar(this.sectionLen))
|
||||
// console.log("tmpPos ::", tmpPos, this.snakeArray.length)
|
||||
newBody.setPosition(tmpPos);
|
||||
}
|
||||
|
||||
this.node.parent.insertChild(newBody, 0)
|
||||
this.snakeArray.push(newBody);
|
||||
//修改蛇身zidx
|
||||
// newBody.setSiblingIndex(zIdx)
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录每两节之间的向量
|
||||
*/
|
||||
_tmpDir: Vec3 = new Vec3()
|
||||
_recordTimes: number = 0
|
||||
recordPoints(idx: number = null) {
|
||||
// record points between bodies (head is a special body)
|
||||
if (!idx) {
|
||||
idx = this.snakeArray.length - 1
|
||||
}
|
||||
let len = 0;
|
||||
let index = 0;
|
||||
this._recordTimes++
|
||||
let pointNum = Math.ceil(this.sectionLen / this.speed) //当前速度移动完一节身体需要的坐标点数
|
||||
let lastBody: Node = this.snakeArray[idx];
|
||||
let lastBOBody: Node = this.snakeArray[idx - 1];
|
||||
let dir: Vec3 = lastBOBody.getPosition().subtract(lastBody.getPosition()).normalize();
|
||||
for (let pIdx = 0; pIdx < pointNum; pIdx++) {
|
||||
len += this.speed;
|
||||
this._tmpDir = dir.clone()
|
||||
let pos: Vec3 = lastBody.getPosition().add(this._tmpDir.multiplyScalar(len));
|
||||
this.pointsArray.splice(index, 0, pos); // 每次从数组头部插入(pointNum个)坐标
|
||||
index += 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
changeSpeed(sp: number) {
|
||||
this.speed = sp
|
||||
this.pointsArray = []
|
||||
this.headPointsNum = 0
|
||||
for (let i = 1; i <= this.bodyNum; i++) {
|
||||
if (this.snakeArray[i].getComponent(snakebody))
|
||||
this.snakeArray[i].getComponent(snakebody).reset()
|
||||
this.recordPoints(i)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
_time: number = 0
|
||||
_hasChan: boolean = false
|
||||
update(deltaTime: number) {
|
||||
// return
|
||||
this._time += deltaTime
|
||||
if (this._time > 0.5) {
|
||||
this.checkAtt()
|
||||
this._time = 0
|
||||
// this.changeSpeed(6)
|
||||
}
|
||||
|
||||
this._tmpV2.set(this.dir.x, this.dir.y)
|
||||
this.rotateHead(this._tmpV2)
|
||||
if (this.dir && this._isMove) {
|
||||
this.move()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
getTmpAttBox(){
|
||||
let rect = this.node.getComponent(UITransform).getBoundingBoxToWorld()
|
||||
//稍微扩大点
|
||||
let biger:number = 100
|
||||
rect.x -= biger
|
||||
rect.y -= biger
|
||||
rect.width += 2 * biger
|
||||
rect.height += 2 * biger
|
||||
return rect
|
||||
}
|
||||
|
||||
/**
|
||||
* 临时攻击怪物
|
||||
*/
|
||||
checkAtt() {
|
||||
this.tmpWeapen.setPosition(this.node.getPosition())
|
||||
|
||||
this.tmpWeapen.scale = Vec3.ZERO
|
||||
this.tmpWeapen.active = true
|
||||
tween(this.tmpWeapen).to(0.2, { scale: v3(1, 1, 1) }).call(() => {
|
||||
this.tmpWeapen.active = false
|
||||
}).start()
|
||||
|
||||
let killArray = []
|
||||
// console.log("this.monsterList::", this.monsterList.length)
|
||||
|
||||
let checkRect = this.getTmpAttBox() //this.node.getComponent(UITransform).getBoundingBoxToWorld()
|
||||
let checkList = this.waveMng.getTreeColliderList(checkRect)
|
||||
// console.log("checkList::", checkList)
|
||||
for (let searchIdx = 0; searchIdx < checkList.length; searchIdx++) {
|
||||
if (this.node.getPosition().subtract(checkList[searchIdx].collider.getPosition()).lengthSqr() < 22500) {
|
||||
killArray.push(checkList[searchIdx].collider)
|
||||
}
|
||||
}
|
||||
|
||||
for (let rmIdx = killArray.length - 1; rmIdx >= 0; rmIdx--) {
|
||||
let tMonsterCtl: monsterCtl = killArray[rmIdx].getComponent(monsterCtl)
|
||||
let uuid = tMonsterCtl.node.uuid
|
||||
if (tMonsterCtl.hurt()) {
|
||||
for (let idx = 0; idx < this.monsterList.length; idx++) {
|
||||
if (this.monsterList[idx].uuid == uuid) {
|
||||
this.monsterList.splice(idx, 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
setDir(dir) {
|
||||
// return
|
||||
this.dir = dir
|
||||
}
|
||||
|
||||
move(dir: Vec3 = null) {
|
||||
// return
|
||||
if (!dir) {
|
||||
dir = this.dir
|
||||
}
|
||||
|
||||
let moveV2 = dir.normalize().multiplyScalar(this.speed)
|
||||
// console.log("蛇头移动距离...", moveV2.length())
|
||||
|
||||
|
||||
//最后动蛇头
|
||||
// this.node.getPosition(this._tmpV3)
|
||||
this.node.setPosition(this.node.getPosition().add(moveV2))
|
||||
this.pointsArray.push(this.node.getPosition());
|
||||
this.headPointsNum += 1;
|
||||
|
||||
//从第一个蛇身开始运动
|
||||
let lastPosIdx = -1
|
||||
for (let i = 1; i < this.snakeArray.length; i++) {
|
||||
let num = Math.floor((this.pointsArray.length - this.headPointsNum) / (this.snakeArray.length - 1) * (this.snakeArray.length - 1 - i));
|
||||
let posIdx = num + this.snakeArray[i].getComponent(snakebody).snakeBodyIdx
|
||||
this.snakeArray[i].setPosition(this.pointsArray[posIdx]);
|
||||
this.snakeArray[i].getComponent(snakebody).addIdx();
|
||||
lastPosIdx = posIdx
|
||||
// console.log("use idx :: ", num + this.snakeArray[i].getComponent(snakebody).snakeBodyIdx)
|
||||
}
|
||||
// console.log("this.pointsArray::", this.pointsArray)
|
||||
|
||||
}
|
||||
}
|
||||
|
9
assets/Scripts/rolectl/player.ts.meta
Normal file
9
assets/Scripts/rolectl/player.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "39b2efb2-9835-4908-b520-6d72d1e9a275",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
136
assets/Scripts/rolectl/rolejoystick.ts
Normal file
136
assets/Scripts/rolectl/rolejoystick.ts
Normal file
@@ -0,0 +1,136 @@
|
||||
import { Component, EventTouch, Input, Node, Vec2, Vec3, _decorator } from 'cc';
|
||||
import Game from '../Test/Test2';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
|
||||
const TOUCH_RADIUS = 180;
|
||||
const ROLE_MOVE_FRAME = 0.2;
|
||||
const _tempPos = new Vec3();
|
||||
const _tempDelta = new Vec2();
|
||||
const Horizontal = new Vec2(1, 0);
|
||||
const MOVE_DELTA = 0.2;
|
||||
|
||||
@ccclass('rolejoystick')
|
||||
export class rolejoystick extends Component {
|
||||
|
||||
@property(Node)
|
||||
ctrlSprite: Node = null!;
|
||||
@property(Node)
|
||||
moveTagSp: Node = null!;
|
||||
@property(Node)
|
||||
role: Node = null!;
|
||||
@property(Vec3)
|
||||
originPos = new Vec3();
|
||||
|
||||
private _isTouch = false;
|
||||
private _touchPos = new Vec2();
|
||||
private _startPos = new Vec2();
|
||||
private _movePos = new Vec2();
|
||||
private _animComp: Animation = null!;
|
||||
private _animState = 'idle';
|
||||
|
||||
// _tempPos: Vec3 = new Vec3()
|
||||
start() {
|
||||
this.ctrlSprite.setPosition(this.originPos);
|
||||
// _tempPos.set(0, 90, 0);
|
||||
// this.role.eulerAngles = _tempPos;
|
||||
// this._animComp = this.role.getComponentInChildren(Animation)!;
|
||||
this.node.on(Input.EventType.TOUCH_START, this.touchStart, this);
|
||||
this.node.on(Input.EventType.TOUCH_MOVE, this.touchMove, this);
|
||||
this.node.on(Input.EventType.TOUCH_END, this.touchEnd, this);
|
||||
}
|
||||
|
||||
onDestroy() {
|
||||
this.node.off(Input.EventType.TOUCH_START, this.touchStart, this);
|
||||
this.node.off(Input.EventType.TOUCH_MOVE, this.touchMove, this);
|
||||
this.node.off(Input.EventType.TOUCH_END, this.touchEnd, this);
|
||||
}
|
||||
|
||||
touchStart(touch: EventTouch) {
|
||||
// this.changeState('cocos_anim_run');
|
||||
// touch.getUI
|
||||
touch.getUILocation(this._startPos);
|
||||
const distance = this._startPos.length();
|
||||
console.error("joystick::", touch, distance)
|
||||
// if (distance < TOUCH_RADIUS) {
|
||||
this._touchPos.set(this._startPos);
|
||||
this._movePos.set(this._startPos);
|
||||
_tempPos.set(this.ctrlSprite.position);
|
||||
this.ctrlSprite.setWorldPosition(this._startPos.x, this._startPos.y, _tempPos.z);
|
||||
this._isTouch = true;
|
||||
// }
|
||||
}
|
||||
|
||||
touchMove(touch: EventTouch) {
|
||||
if (!this._isTouch) {
|
||||
return;
|
||||
}
|
||||
|
||||
touch.getUILocation(this._movePos);
|
||||
Vec2.subtract(_tempDelta, this._movePos, this._startPos);
|
||||
// 计算角色的整体旋转值
|
||||
// const deltaRadian = _tempDelta.angle(Horizontal);
|
||||
// const angle = deltaRadian * 180 / Math.PI;
|
||||
// const rot = this.role.eulerAngles;
|
||||
// _tempPos.set(rot.x, 90 + (Math.sign(_tempDelta.y)) * angle, rot.z);
|
||||
// this.role.eulerAngles = _tempPos;
|
||||
|
||||
// 重新规划移动方向值
|
||||
// _tempDelta.multiply2f(MOVE_DELTA, MOVE_DELTA);
|
||||
// Vec2.add(this._movePos, this._startPos, _tempDelta);
|
||||
const distance = _tempDelta.length();
|
||||
|
||||
// 是否超出限制半径
|
||||
if (distance > TOUCH_RADIUS) {
|
||||
|
||||
const radian = _tempDelta.angle(Horizontal);
|
||||
// console.log("radian::", radian)
|
||||
const x = Math.cos(radian) * TOUCH_RADIUS;
|
||||
let y = Math.sin(radian) * TOUCH_RADIUS;
|
||||
if (this._startPos.y >= this._movePos.y) {
|
||||
y = -y
|
||||
}
|
||||
this._movePos.set(x, y).add(this._startPos);
|
||||
}
|
||||
|
||||
// this.ctrlSprite.setWorldPosition(this._movePos.x, this._movePos.y, 0);
|
||||
this.moveTagSp.setWorldPosition(this._movePos.x, this._movePos.y, 0);
|
||||
this._touchPos.set(this._movePos);
|
||||
}
|
||||
|
||||
touchEnd(touch: EventTouch) {
|
||||
this._isTouch = false;
|
||||
// this.changeState('cocos_anim_idle');
|
||||
// this.ctrlSprite.setPosition(this.originPos);
|
||||
// this.moveTagSp.setPosition(this.originPos)
|
||||
}
|
||||
|
||||
changeState(name: string) {
|
||||
if (this._animState === name) {
|
||||
return;
|
||||
}
|
||||
|
||||
// this._animComp.play(name);
|
||||
// this._animState = name;
|
||||
}
|
||||
|
||||
update() {
|
||||
if (!this._isTouch) {
|
||||
Game.Instance.Player._isMove = false
|
||||
return;
|
||||
}
|
||||
|
||||
// _tempPos.set(0, 0, ROLE_MOVE_FRAME);
|
||||
// this.role.translate(_tempPos);
|
||||
if (Game.Instance.Player) {
|
||||
Game.Instance.Player._isMove = true
|
||||
Game.Instance.Player.setDir(this.moveTagSp.getPosition().subtract(this.ctrlSprite.getPosition()).normalize())
|
||||
}
|
||||
// Game.Instance.Player.move(this.moveTagSp.getPosition().subtract(this.ctrlSprite.getPosition()).normalize())
|
||||
// Game.
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
9
assets/Scripts/rolectl/rolejoystick.ts.meta
Normal file
9
assets/Scripts/rolectl/rolejoystick.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "dd6eec7f-57ad-46f6-b5fc-7a4907cdc2e1",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
66
assets/Scripts/rolectl/snakebody.ts
Normal file
66
assets/Scripts/rolectl/snakebody.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import { Component, Node, Vec3, _decorator } from 'cc';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('snakebody')
|
||||
export class snakebody extends Component {
|
||||
|
||||
speed: number = 3
|
||||
|
||||
private _preSnakeBody: Node = null;
|
||||
public get preSnakeBody(): Node {
|
||||
return this._preSnakeBody;
|
||||
}
|
||||
public set preSnakeBody(value: Node) {
|
||||
this._preSnakeBody = value;
|
||||
}
|
||||
|
||||
private _snakeBodyIdx: number = 0;
|
||||
public get snakeBodyIdx(): number {
|
||||
return this._snakeBodyIdx;
|
||||
}
|
||||
public set snakeBodyIdx(value: number) {
|
||||
this._snakeBodyIdx = value;
|
||||
}
|
||||
|
||||
reset() {
|
||||
this.snakeBodyIdx = 0
|
||||
}
|
||||
|
||||
addIdx() {
|
||||
this.snakeBodyIdx++
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前蛇身指向前一个蛇身的dir
|
||||
*/
|
||||
_tmpVec: Vec3 = new Vec3(0, 1, 0)
|
||||
getPreDir() {
|
||||
// return this.preSnakeBody.getPosition().subtract(this.node.getPosition()).normalize()
|
||||
// return this.preSnakeBody.getPosition().subtract(this.node.getPosition())
|
||||
console.log("this.preSnakeBody.getPosition() ::", this.preSnakeBody.getPosition())
|
||||
return this.node.getPosition().subtract(this.preSnakeBody.getPosition())
|
||||
}
|
||||
|
||||
|
||||
start() {
|
||||
|
||||
}
|
||||
|
||||
update(deltaTime: number) {
|
||||
|
||||
}
|
||||
|
||||
_tmpV3: Vec3 = new Vec3()
|
||||
move(length: number = null) {
|
||||
let moveV2 = this.getPreDir()//.multiplyScalar(1 / this.speed)
|
||||
|
||||
|
||||
// console.log("蛇身移动的距离...", moveV2.length())
|
||||
Vec3.lerp(this._tmpV3, this.node.getPosition(), this.preSnakeBody.getPosition().add(moveV2), 0.5)
|
||||
console.log("........:", moveV2, this._tmpV3)
|
||||
this.node.setPosition(this._tmpV3)
|
||||
// this.node.setPosition(this.node.getPosition().add(moveV2))
|
||||
}
|
||||
}
|
||||
|
||||
|
9
assets/Scripts/rolectl/snakebody.ts.meta
Normal file
9
assets/Scripts/rolectl/snakebody.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "285cabaf-7adc-4f7c-9950-771d5e8abc2d",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
Reference in New Issue
Block a user