This commit is contained in:
Martin
2022-11-03 16:55:49 +01:00
commit f250c24f90
28 changed files with 10923 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
import { Component, _decorator } from "cc";
import { Player } from "./Player";
import { VirtualJoystic } from "./VirtualJoystic";
const { ccclass, property } = _decorator;
@ccclass("GameBootstrapper")
export class GameBootstrapper extends Component {
@property(VirtualJoystic) private virtualJoystic: VirtualJoystic;
@property(Player) private player: Player;
@property(Number) private strikeDelay = 0;
public start(): void {
this.virtualJoystic.init();
this.player.init(this.virtualJoystic, this.strikeDelay);
}
public update(deltaTime: number): void {
this.player.gameTick(deltaTime);
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "5faa9913-e167-4014-92de-7061e103e557",
"files": [],
"subMetas": {},
"userData": {}
}

31
assets/Scripts/Player.ts Normal file
View File

@@ -0,0 +1,31 @@
import { Component, Vec2, Vec3, _decorator } from "cc";
import { VirtualJoystic } from "./VirtualJoystic";
import { Weapon } from "./Weapon";
const { ccclass, property } = _decorator;
@ccclass("Player")
export class Player extends Component {
private virtualJoystic: VirtualJoystic;
@property private speed = 0;
@property(Weapon) private weapon: Weapon;
public init(virtualJoystic: VirtualJoystic, strikeDelay: number): void {
this.virtualJoystic = virtualJoystic;
this.weapon.init(strikeDelay);
}
public gameTick(deltaTime: number): void {
const movement: Vec2 = this.virtualJoystic.getAxis();
movement.x *= deltaTime * this.speed;
movement.y *= deltaTime * this.speed;
const newPosition: Vec3 = this.node.worldPosition;
newPosition.x += movement.x;
newPosition.y += movement.y;
this.node.setWorldPosition(newPosition);
this.weapon.gameTick(deltaTime, movement);
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "b570abb8-1bcd-4ef0-85e8-4d783531001d",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -0,0 +1,79 @@
import { _decorator, Component, Node, Vec3, input, Input, EventMouse, Vec2, EventTouch } from "cc";
const { ccclass, property } = _decorator;
@ccclass("VirtualJoystic")
export class VirtualJoystic extends Component {
@property(Number) private maxDistance = 10;
@property(Node) private knob: Node;
#isUsingJoystic = false;
#defaultPosition: Vec2 = new Vec2();
public init(): void {
input.on(Input.EventType.MOUSE_DOWN, this.activateMouseJoystic, this);
input.on(Input.EventType.MOUSE_UP, this.deactivateJoystic, this);
input.on(Input.EventType.MOUSE_MOVE, this.moveKnobMouse, this);
input.on(Input.EventType.TOUCH_START, this.activateTouchJoystic, this);
input.on(Input.EventType.TOUCH_END, this.deactivateJoystic, this);
input.on(Input.EventType.TOUCH_MOVE, this.moveKnobTouch, this);
this.deactivateJoystic();
}
public getAxis(): Vec2 {
if (this.#isUsingJoystic) {
return new Vec2(this.knob.position.x / this.maxDistance, this.knob.position.y / this.maxDistance);
} else {
return new Vec2();
}
}
private activateTouchJoystic(e: EventTouch): void {
this.activateJoystic(e.getUILocation());
}
private activateMouseJoystic(e: EventMouse): void {
this.activateJoystic(e.getUILocation());
}
private activateJoystic(location: Vec2): void {
this.#isUsingJoystic = true;
this.node.active = true;
this.#defaultPosition = location;
this.node.setWorldPosition(new Vec3(this.#defaultPosition.x, this.#defaultPosition.y, 0));
this.knob.position = new Vec3();
}
private deactivateJoystic(): void {
this.#isUsingJoystic = false;
this.node.active = false;
}
private moveKnobTouch(e: EventTouch): void {
this.moveKnob(e.getUILocation());
}
private moveKnobMouse(e: EventMouse): void {
this.moveKnob(e.getUILocation());
}
private moveKnob(location: Vec2): void {
if (!this.#isUsingJoystic) return;
const posDelta: Vec2 = location.subtract(this.#defaultPosition);
let x: number = posDelta.x;
let y: number = posDelta.y;
const length: number = Math.sqrt(posDelta.x ** 2 + posDelta.y ** 2);
if (this.maxDistance < length) {
const multiplier: number = this.maxDistance / length;
x *= multiplier;
y *= multiplier;
}
this.knob.position = new Vec3(x, y, 0);
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "a51b28bc-4ef0-4324-97fc-44f964ce087a",
"files": [],
"subMetas": {},
"userData": {}
}

35
assets/Scripts/Weapon.ts Normal file
View File

@@ -0,0 +1,35 @@
import { Animation, BoxCollider2D, Component, Contact2DType, Vec2, Vec3, _decorator } from "cc";
const { ccclass, property } = _decorator;
@ccclass("Weapon")
export class Weapon extends Component {
@property(Animation) private weaponAnimation: Animation;
@property(BoxCollider2D) private weaponCollider: BoxCollider2D;
private strikeDelay: number;
private currentDelay = 0;
public init(strikeDelay: number): void {
this.strikeDelay = strikeDelay;
this.weaponCollider.on(Contact2DType.BEGIN_CONTACT, () => {
console.log("Begin Contact!");
});
}
public gameTick(deltaTime: number, movement: Vec2): void {
this.currentDelay += deltaTime;
if (this.strikeDelay <= this.currentDelay) {
this.currentDelay = 0;
this.strike(movement);
}
}
private strike(movement: Vec2): void {
const direction: Vec2 = movement.normalize();
const angle: number = (Math.atan2(direction.y, direction.x) * 180) / Math.PI;
this.node.eulerAngles = new Vec3(0, 0, angle);
this.weaponAnimation.play("WeaponSwing");
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "2391b8b7-f9fa-42a8-b046-55ff57b07d02",
"files": [],
"subMetas": {},
"userData": {}
}