1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
| import { Component, Material, Color, Vec3, _decorator } from 'cc';
const { ccclass, property, menu } = _decorator;
@ccclass('ForceFieldController') @menu('Custom/ForceFieldController') export class ForceFieldController extends Component { @property({ type: Color, displayName: '力场颜色' }) public shieldColor: Color = new Color(50, 200, 255, 255); @property({ range: [0.0, 1.0], displayName: '透明�? }) public transparency: number = 0.3; @property({ range: [5.0, 100.0], displayName: '网格密度' }) public gridScale: number = 20.0; @property({ range: [0.01, 0.2], displayName: '网格厚度' }) public gridThickness: number = 0.05; @property({ range: [0.0, 10.0], displayName: '能量流�? }) public energySpeed: number = 2.0; @property({ range: [0.1, 5.0], displayName: '菲涅尔强�? }) public fresnelPower: number = 2.0; @property({ type: Vec3, displayName: '冲击点位�? }) public impactPosition: Vec3 = new Vec3(0, 0, 0); @property({ range: [0.1, 10.0], displayName: '冲击半径' }) public impactRadius: number = 2.0; @property({ range: [0.0, 10.0], displayName: '冲击强度' }) public impactIntensity: number = 3.0; @property({ displayName: '力场类型' }) public forceFieldType: ForceFieldType = ForceFieldType.Sphere; @property({ displayName: '启用多层防护' }) public enableMultiLayer: boolean = false; @property({ range: [0.0, 3.0], displayName: '护盾强度' }) public shieldStrength: number = 1.0; private _material: Material | null = null; private _isActive: boolean = false; private _impactTimer: number = 0; private _impactActive: boolean = false; public enum ForceFieldType { Sphere = 'sphere-force-field', Impact = 'impact-force-field', Magnetic = 'magnetic-field', MultiLayer = 'multi-layer-shield' } onLoad() { const renderer = this.getComponent('cc.MeshRenderer'); if (renderer && renderer.material) { this._material = renderer.material; this.updateMaterial(); } } update(deltaTime: number) { if (this._isActive) { if (this._impactActive) { this._impactTimer += deltaTime; this._impactActive = false; this._impactTimer = 0; } } this.updateMaterial(); } } private updateMaterial() { if (!this._material) return; this._material.setProperty('shieldColor', this.shieldColor); this._material.setProperty('transparency', this.transparency); this._material.setProperty('gridScale', this.gridScale); this._material.setProperty('gridThickness', this.gridThickness); this._material.setProperty('energySpeed', this.energySpeed); this._material.setProperty('fresnel_power', this.fresnelPower); this._material.setProperty('impactPosition', this.impactPosition); this._material.setProperty('impactRadius', this.impactRadius); this._material.setProperty('impactIntensity', this.impactIntensity); if (this.enableMultiLayer) { this._material.setProperty('shieldStrength', this.shieldStrength); } } public setForceFieldType(type: ForceFieldType) { this.forceFieldType = type; console.log(`切换到力场类�? ${type}`); this.updateMaterial(); } public activateShield() { this._isActive = true; this.transparency = 0; const activate = (dt: number) => { elapsed += dt; const t = Math.min(elapsed / 1.0, 1.0); this.transparency = t * 0.3; this.shieldStrength = t * 2.0; this.updateMaterial(); if (t < 1.0) { this.scheduleOnce(activate, 0); } }; this.scheduleOnce(activate, 0); } public deactivateShield() { const originalTransparency = this.transparency; const originalStrength = this.shieldStrength; let elapsed = 0; const deactivate = (dt: number) => { elapsed += dt; const t = Math.min(elapsed / 0.8, 1.0); this.transparency = originalTransparency * (1.0 - t); this.shieldStrength = originalStrength * (1.0 - t); this.updateMaterial(); if (t >= 1.0) { this._isActive = false; } else { this.scheduleOnce(deactivate, 0); } }; this.scheduleOnce(deactivate, 0); } public triggerImpact(worldPosition: Vec3, intensity: number = 3.0) { this.impactPosition.set(worldPosition); this.impactIntensity = intensity; this._impactActive = true; this._impactTimer = 0; this.updateMaterial(); } public setShieldColor(color: Color) { this.shieldColor = color; this.updateMaterial(); } public setShieldStrength(strength: number) { this.shieldStrength = strength; this.updateMaterial(); } public applyDefensiveShield() { this.shieldColor = new Color(100, 150, 255, 255); this.gridScale = 20.0; this.energySpeed = 1.0; this.transparency = 0.4; this.setForceFieldType(ForceFieldType.Sphere); } public applyAttackShield() { this.shieldColor = new Color(255, 100, 100, 255); this.gridScale = 15.0; this.energySpeed = 3.0; this.transparency = 0.6; this.setForceFieldType(ForceFieldType.Impact); } public applyAdvancedShield() { this.enableMultiLayer = true; this.shieldStrength = 2.0; this.transparency = 0.3; this.setForceFieldType(ForceFieldType.MultiLayer); } public applyMagneticField() { this.shieldColor = new Color(200, 100, 255, 255); this.energySpeed = 2.5; this.transparency = 0.5; this.setForceFieldType(ForceFieldType.Magnetic); } public shieldOverload(duration: number = 3.0) { const originalColor = this.shieldColor.clone(); const originalIntensity = this.impactIntensity; this.shieldColor = new Color(255, 255, 100, 255); this.impactIntensity = 5.0; this.energySpeed = 8.0; this.scheduleOnce(() => { this.shieldColor.set(originalColor); this.impactIntensity = originalIntensity; this.energySpeed = 2.0; this.updateMaterial(); }, duration); this.updateMaterial(); } public emergencyShield() { this.shieldStrength = 3.0; this.transparency = 0.8; this.energySpeed = 5.0; this.activateShield(); } public adaptiveShield(threatLevel: number) { threatLevel = Math.max(0, Math.min(1, threatLevel)); const baseColor = new Color(100, 200, 255, 255); const alertColor = new Color(255, 100, 100, 255); this.shieldColor.g = Math.lerp(baseColor.g, alertColor.g, threatLevel); this.shieldColor.b = Math.lerp(baseColor.b, alertColor.b, threatLevel); this.shieldStrength = 1.0 + threatLevel * 2.0; this.transparency = 0.3 + threatLevel * 0.4; this.energySpeed = 1.0 + threatLevel * 4.0; this.updateMaterial(); } public resetToDefault() { this.shieldColor = new Color(50, 200, 255, 255); this.transparency = 0.3; this.gridScale = 20.0; this.gridThickness = 0.05; this.energySpeed = 2.0; this.fresnelPower = 2.0; this.shieldStrength = 1.0; this.enableMultiLayer = false; this.updateMaterial(); } }
export { ForceFieldType } from './ForceFieldController';
|