feat: add gizmos in game

feature/ads-smart-display
tiendat3699 2024-06-07 15:45:52 +07:00
parent 2195918c77
commit 4474e91a64
7 changed files with 816 additions and 3490 deletions

File diff suppressed because it is too large Load Diff

View File

@ -4,6 +4,7 @@ import {
CCFloat,
CCInteger,
Collider2D,
Color,
Component,
Contact2DType,
geometry,
@ -17,6 +18,7 @@ import {
import GameState from '../Enum/GameState';
import ScoreType from '../Enum/ScoreType';
import GameEvent from '../Events/GameEvent';
import Gizmos2D from '../Gizmos/Gizmos2D';
import AudioManager from '../Manager/AudioManager';
import { EventManger } from '../Manager/EventManger';
const { ccclass, property } = _decorator;
@ -53,6 +55,16 @@ export class Enemy extends Component {
private _isActive = false;
private _currentGoal = 0;
onFocusInEditor(): void {
Gizmos2D.registerDrawGizmos(this.node);
}
onDrawGizmosSelected(): void {
Gizmos2D.beginColor(this.node, Color.RED);
Gizmos2D.drawSolidCircle(this.node, this._patrolPoint1.worldPosition, 20);
Gizmos2D.drawSolidCircle(this.node, this._patrolPoint2.worldPosition, 20);
}
protected onLoad(): void {
EventManger.instance.on(GameEvent.Score, this.onScore, this);
EventManger.instance.on(GameEvent.GameStateChange, this.onGameStateChange, this);

View File

@ -1,6 +1,19 @@
import { _decorator, AudioClip, Collider2D, Component, Contact2DType, Node, ParticleSystem, Prefab, Vec2 } from 'cc';
import {
_decorator,
AudioClip,
Collider2D,
Color,
Component,
Contact2DType,
Node,
ParticleSystem,
PolygonCollider2D,
Prefab,
Vec2,
} from 'cc';
import TimeConfig from '../Enum/TimeConfig';
import { CameraController } from '../Environments/CameraController';
import Gizmos2D from '../Gizmos/Gizmos2D';
import AudioManager from '../Manager/AudioManager';
import { GameManager } from '../Manager/GameManager';
import ObjectPool from '../Pool/ObjectPool';
@ -28,6 +41,21 @@ export class MultiBall extends Component {
private _enabled: boolean = true;
onFocusInEditor(): void {
Gizmos2D.registerDrawGizmos(this.node);
}
onDrawGizmos(): void {
Gizmos2D.beginColor(this.node, new Color(0, 255, 0, 200));
const points = (this._collider as PolygonCollider2D).points.map((p) => p.clone().add(this._collider.offset));
Gizmos2D.beginLocalPosition(this.node);
Gizmos2D.drawSolidPolygon(this.node, points);
Gizmos2D.endLocalPosition(this.node);
Gizmos2D.beginColor(this.node, new Color(0, 0, 255, 200));
Gizmos2D.drawSolidEllipse(this.node, this._portLeft.worldPosition, 20, 30);
Gizmos2D.drawSolidEllipse(this.node, this._portRight.worldPosition, 20, 30);
}
protected onLoad(): void {
this._fxPool = new ObjectPool(this._fx, 2, true);
this._collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);

View File

@ -0,0 +1,9 @@
{
"ver": "1.2.0",
"importer": "directory",
"imported": true,
"uuid": "4e8d436e-a896-448d-8a6c-cd598f2fd34a",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@ -1,10 +1,12 @@
import { NodeActivator } from 'cc';
declare module 'cc' {
interface Component {
/**
* Call per frame in editor (Only use in editor)
*/
onDrawGizmos(): void;
/**
* Call per frame in editor when node selected (Only use in editor)
*/
onDrawGizmosSelected(): void;
}
}

View File

@ -62,9 +62,15 @@ class GizmosDebugDraw extends Component {
const comps = this._parentNode.components;
for (let i = 0; i < comps.length; i++) {
const comp: Component = comps[i];
comp.onDrawGizmos?.();
comp.onDrawGizmosSelected?.();
}
}
const comps = this._parentNode.components;
for (let i = 0; i < comps.length; i++) {
const comp: Component = comps[i];
comp.onDrawGizmos?.();
}
}
}
@ -133,12 +139,13 @@ class GizmosDebugDraw extends Component {
const color = this._color.clone();
const renderer = this.getRenderer(color);
renderer.setLayer(this._layer);
const pointList = this._useLocalPosition ? points : points.map((p) => this.worldToLocal(p));
renderer.addDrawCall((g) => {
if (points.length > 0) {
const p0 = this.worldToLocal(points[0]);
const p0 = pointList[0];
g.moveTo(p0.x, p0.y);
for (let i = 1; i < points.length; i++) {
const p = this.worldToLocal(points[i]);
for (let i = 1; i < pointList.length; i++) {
const p = pointList[i];
g.lineTo(p.x, p.y);
}
if (close) g.close();
@ -180,17 +187,40 @@ class GizmosDebugDraw extends Component {
});
}
public drawSolidRect(center: IVec2Like, width: number, height: number) {
public drawSolidRect(position: IVec2Like, width: number, height: number) {
const color = this._color.clone();
const renderer = this.getRenderer(color);
renderer.setLayer(this._layer);
const c = this._useLocalPosition ? center : this.worldToLocal(center);
const p = this._useLocalPosition ? position : this.worldToLocal(position);
const topLeft = {
x: p.x - width / 2,
y: p.y - height / 2,
};
renderer.addDrawCall((g) => {
g.rect(c.x, c.y, width, height);
g.rect(topLeft.x, topLeft.y, width, height);
g.fill();
});
}
public drawSolidPolygon(points: IVec2Like[]) {
const color = this._color.clone();
const renderer = this.getRenderer(color);
renderer.setLayer(this._layer);
const pointList = this._useLocalPosition ? points : points.map((p) => this.worldToLocal(p));
renderer.addDrawCall((g) => {
if (points.length > 0) {
const p0 = pointList[0];
g.moveTo(p0.x, p0.y);
for (let i = 1; i < pointList.length; i++) {
const p = pointList[i];
g.lineTo(p.x, p.y);
}
g.close();
g.fill();
}
});
}
public drawEllipse(center: IVec2Like, radiusX: number, radiusY: number) {
const color = this._color.clone();
const renderer = this.getRenderer(color);
@ -328,12 +358,12 @@ export default class Gizmos2D {
debugNode.drawLineList(points, close);
}
public static drawCircle(node: Node, center, radius: number) {
public static drawCircle(node: Node, center: IVec2Like, radius: number) {
const debugNode = this.getDebugNode(node);
debugNode.drawCircle(center, radius);
}
public static drawSolidCircle(node: Node, center, radius: number) {
public static drawSolidCircle(node: Node, center: IVec2Like, radius: number) {
const debugNode = this.getDebugNode(node);
debugNode.drawSolidCircle(center, radius);
}
@ -343,9 +373,14 @@ export default class Gizmos2D {
debugNode.drawRect(position, width, height);
}
public static drawSolidRect(node: Node, center: IVec2Like, width: number, height: number) {
public static drawSolidRect(node: Node, position: IVec2Like, width: number, height: number) {
const debugNode = this.getDebugNode(node);
debugNode.drawSolidRect(center, width, height);
debugNode.drawSolidRect(position, width, height);
}
public static drawSolidPolygon(node: Node, positions: IVec2Like[]) {
const debugNode = this.getDebugNode(node);
debugNode.drawSolidPolygon(positions);
}
public static drawEllipse(node: Node, center: IVec2Like, radiusX: number, radiusY: number) {

View File

@ -1,11 +1,12 @@
import { _decorator, CCFloat, CCInteger, Component, Node, Prefab, randomRangeInt } from 'cc';
import ObjectPool from '../Pool/ObjectPool';
import { ScoreObject } from '../Environments/ScoreObject';
import { EventManger } from './EventManger';
import GameEvent from '../Events/GameEvent';
import { _decorator, CCFloat, CCInteger, Color, Component, Node, Prefab, randomRangeInt } from 'cc';
import { BoosterBase } from '../Booster/BoosterBase';
import GameState from '../Enum/GameState';
import ScoreType from '../Enum/ScoreType';
import { BoosterBase } from '../Booster/BoosterBase';
import { ScoreObject } from '../Environments/ScoreObject';
import GameEvent from '../Events/GameEvent';
import Gizmos2D from '../Gizmos/Gizmos2D';
import ObjectPool from '../Pool/ObjectPool';
import { EventManger } from './EventManger';
const { ccclass, property } = _decorator;
@ccclass('weightedObject')
@ -51,6 +52,17 @@ export class SpawnObjectManager extends Component {
private _boosterSpawned = false;
private _spawnTimeObject = 0;
onFocusInEditor(): void {
Gizmos2D.registerDrawGizmos(this.node);
}
onDrawGizmos(): void {
for (let i = 0; i < this._spawnPoints.length; i++) {
Gizmos2D.beginColor(this.node, new Color(0, 255, 0, 180));
Gizmos2D.drawSolidCircle(this.node, this._spawnPoints[i].worldPosition, 15);
}
}
protected onLoad(): void {
EventManger.instance.on(GameEvent.ObjectRelease, this.onObjectRelease, this);
EventManger.instance.on(GameEvent.GameStateChange, this.onGameStateChange, this);