Commit 920008f0 authored by haiyoucuv's avatar haiyoucuv

init

parent 1c3ec0d6
{
"ver": "1.2.0",
"importer": "directory",
"imported": true,
"uuid": "bb1f1bdf-5a9e-42dd-8fe3-7b71358284df",
"files": [],
"subMetas": {},
"userData": {}
}
import { _decorator, EventKeyboard, Input, input, KeyCode, Node, } from "cc";
import Scene from "db://assets/Module/Scene";
import { Snake } from "db://assets/Scripts/Scenes/MainGame/Snake";
const { ccclass, property } = _decorator;
export enum DIR {
UP,
DOWN,
LEFT,
RIGHT,
}
@ccclass("MainGame1")
export class MainGame1 extends Scene {
static bundle: string = "MainGame";
static skin: string = "MainGame";
@property(Node)
snake: Node = null;
@property
speed: number = 200;
onLoad() {
input.on(Input.EventType.TOUCH_START, this.onTouchStart, this);
input.on(Input.EventType.TOUCH_END, this.onTouchEnd, this);
input.on(Input.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
input.on(Input.EventType.KEY_UP, this.onKeyUp, this);
}
curDir: DIR = DIR.UP;
keyArr = [];
async start() {
// this.schedule(() => {
// this.snake.getComponent(Snake).addNode();
// }, 1);
}
onDestroy() {
input.off(Input.EventType.TOUCH_START, this.onTouchStart, this);
input.off(Input.EventType.TOUCH_END, this.onTouchEnd, this);
input.off(Input.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this);
input.off(Input.EventType.KEY_UP, this.onKeyUp, this);
}
onKeyDown(event: EventKeyboard) {
const keyArr = [
KeyCode.KEY_W,
KeyCode.KEY_S,
KeyCode.KEY_A,
KeyCode.KEY_D,
];
if (keyArr.indexOf(event.keyCode) > -1) {
this.keyArr.push(event.keyCode);
}
this.setDir();
}
onKeyUp(event: EventKeyboard) {
const index = this.keyArr.indexOf(event.keyCode);
if (index > -1) {
this.keyArr.splice(index, 1);
}
this.setDir();
}
setDir() {
if (!this.keyArr.length) return;
this.curDir = {
[KeyCode.KEY_W]: DIR.UP,
[KeyCode.KEY_S]: DIR.DOWN,
[KeyCode.KEY_A]: DIR.LEFT,
[KeyCode.KEY_D]: DIR.RIGHT,
}[this.keyArr[this.keyArr.length - 1]];
}
onTouchStart(event: any) {
}
onTouchEnd(event: any) {
}
update(dt: number) {
if (this.curDir == DIR.UP) {
const newY = this.snake.position.y + this.speed * dt;
this.snake.setPosition(this.snake.position.x, newY);
} else if (this.curDir == DIR.DOWN) {
const newY = this.snake.position.y - this.speed * dt;
this.snake.setPosition(this.snake.position.x, newY);
} else if (this.curDir == DIR.LEFT) {
const newX = this.snake.position.x - this.speed * dt;
this.snake.setPosition(newX, this.snake.position.y);
} else if (this.curDir == DIR.RIGHT) {
const newX = this.snake.position.x + this.speed * dt;
this.snake.setPosition(newX, this.snake.position.y);
}
}
}
{
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "701bec3a-f5a6-484b-92cf-5cb67aff8961",
"files": [],
"subMetas": {},
"userData": {}
}
import { _decorator, Component, instantiate, Node, Prefab, Vec3 } from "cc";
const { ccclass, property } = _decorator;
class SnakeNode {
node: Node = null;
next: SnakeNode = null;
}
const _tempPos = new Vec3();
const _tempPrePos = new Vec3();
@ccclass("Snake1")
export class Snake1 extends Component {
@property(Prefab)
nodePrefab: Prefab = null;
@property
gap: number = 50;
head: SnakeNode = null;
last: SnakeNode = null;
onLoad() {
this.last = this.head = new SnakeNode();
this.head.node = this.node;
}
addNode() {
const node = instantiate(this.nodePrefab);
this.node.parent.addChild(node);
node.position = this.last.node.position;
const snakeNode = new SnakeNode();
snakeNode.node = node;
this.last.next = snakeNode;
this.last = snakeNode;
}
update(dt: number) {
let pre: SnakeNode = this.head;
let current: SnakeNode = this.head.next;
while (current) {
const posDir = current.node.getPosition(_tempPos)
.subtract(pre.node.position)
.normalize();
const pos = pre.node.getPosition(_tempPrePos)
.add(posDir.multiplyScalar(this.gap));
current.node.setPosition(pos);
pre = current;
current = current.next;
}
}
}
\ No newline at end of file
{
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "917a4745-8677-4e4a-83f1-07545cfd07ef",
"files": [],
"subMetas": {},
"userData": {}
}
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