| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- import { _decorator, Component, Node, UITransform, v3 } from 'cc';
- import { DataSystem } from '../core/data/DataSystem';
- import { BattleData } from './BattleData';
- import { Monster } from './Monster';
- const { ccclass, property } = _decorator;
- @ccclass('SightGlass')
- export class SightGlass extends Component {
- @property({ type: Node, tooltip: "瞄准镜" }) sightGlass: Node;
- private data: BattleData;
- private fouceTarget: Node;
- private lastMonsterID: number = -1;
- start() {
- this.data = DataSystem.getData(BattleData);
- }
- update(): void {
- DataSystem.watch(BattleData, "_monsters") && this.onMonsterChange();
- DataSystem.watch(BattleData, "_target") && this.targetChange();
- DataSystem.watch(BattleData, "_deleteMonster") && this.deleteMonster();
- this.setGlass();
- }
- /** 当怪物发生变化时 */
- private onMonsterChange(): void {
- if (this.data._target != -1 && this.lastMonsterID != -1) {
- for (let i = 0; i < this.data._monsters.length; i++) {
- if (this.lastMonsterID == this.data._monsters[i].data._id) {
- this.data._target = i;
- return;
- }
- }
- this.data._target = -1;
- this.lastMonsterID = -1;
- }
- }
- /**强制目标改变 */
- private targetChange(): void {
- if (this.data._target != -1) {
- this.fouceTarget = this.data._monsters[this.data._target].node;
- this.lastMonsterID = this.data._monsters[this.data._target].data._id;
- } else {
- this.fouceTarget = null;
- }
- }
- /**设置瞄准镜 */
- private setGlass(): void {
- if (this.fouceTarget) {
- this.sightGlass.position = this.fouceTarget.position.clone().add(v3(0, this.fouceTarget.getComponent(UITransform).height * this.fouceTarget.scale.y / 2, 0));
- } else {
- this.sightGlass.position = v3(1000, 1000, 0);
- }
- }
- /**删除怪物 */
- private deleteMonster(): void {
- if (this.fouceTarget && (!this.fouceTarget.getComponent(Monster).data || this.data._deleteMonster.indexOf(this.fouceTarget.getComponent(Monster).data._id) != -1)) {
- this.data._target = -1;
- this.fouceTarget = null;
- }
- }
- }
|