Skip to content

Commit 5ac4a1f

Browse files
committed
desprecate and remove all vars cached from fireFrom
1 parent b5fbd15 commit 5ac4a1f

File tree

1 file changed

+113
-65
lines changed

1 file changed

+113
-65
lines changed

flixel/addons/weapon/FlxWeapon.hx

Lines changed: 113 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -80,33 +80,29 @@ class FlxTypedWeapon<TBullet:FlxBullet> implements IFlxDestroyable
8080
/**
8181
* The parent sprite of this weapon. Only accessible when `fireFrom == PARENT`.
8282
*/
83-
public var parent(default, null):FlxSprite;
83+
@:deprecated("use FlxWeaponFireFrom.PARENT's parent param, instead")
84+
public var parent(get, set):FlxSprite;
8485

8586
/**
8687
* Whether to fire bullets in the direction of the `parent`'s angle. Only accessible when `fireFrom == PARENT`.
8788
*/
88-
public var useParentDirection(default, set):Bool;
89+
@:deprecated("use FlxWeaponFireFrom.PARENT's useParentAngle param, instead")
90+
public var useParentDirection(get, set):Bool;
8991

9092
/**
9193
* A fixed position from which to fire the weapon, like in the game Missile Command. Only accessible when `fireFrom == POSITION`.
9294
*/
93-
public var firePosition(default, null):FlxBounds<FlxPoint>;
95+
@:deprecated("use FlxWeaponFireFrom.PARENT's useParentAngle param, instead")
96+
public var firePosition(get, set):FlxBounds<FlxPoint>;
9497

9598
/**
9699
* A value used to offset a bullet's position when it is fired. Can be used to, for example, line a bullet up with the "nose" of a space ship.
97100
* Only accessible when `fireFrom == PARENT`.
98101
*/
99-
@:deprecated("Use positionOffsetBounds instead")
100-
public var positionOffset(default, null):FlxPoint;
102+
@:deprecated("use FlxWeaponFireFrom.PARENT's offset param, instead")
103+
public var positionOffset(get, set):FlxPoint;
101104

102-
/**
103-
* A value used to offset a bullet's position when it is fired. Can be used to, for example,
104-
* line a bullet up with the "nose" of a space ship. Only accessible when `fireFrom == PARENT`.
105-
* @since 3.2.0
106-
*/
107-
public var positionOffsetBounds(default, null):FlxBounds<FlxPoint>;
108-
109-
public var fireFrom(default, set):FlxWeaponFireFrom;
105+
public var fireFrom:FlxWeaponFireFrom;
110106
public var speedMode:FlxWeaponSpeedMode;
111107

112108
/**
@@ -223,9 +219,14 @@ class FlxTypedWeapon<TBullet:FlxBullet> implements IFlxDestroyable
223219
// Clear any velocity that may have been previously set from the pool
224220
currentBullet.velocity.zero(); // TODO is this really necessary?
225221

222+
final parentAngleOffset:Float = null
223+
final parent:FlxSprite = null;
224+
226225
switch (fireFrom)
227226
{
228-
case PARENT(parent, offset, useParentAngle, angleOffset):
227+
case PARENT(enumParent, offset, useParentAngle, angleOffset):
228+
parent = enumParent;
229+
parentAngleOffset = angleOffset;
229230
// store new offset in a new variable
230231
var actualOffset = FlxPoint.get(FlxG.random.float(offset.min.x, offset.max.x), FlxG.random.float(offset.min.y, offset.max.y));
231232
if (useParentAngle)
@@ -253,6 +254,9 @@ class FlxTypedWeapon<TBullet:FlxBullet> implements IFlxDestroyable
253254
currentBullet.elasticity = bulletElasticity;
254255
currentBullet.lifespan = FlxG.random.float(bulletLifeSpan.min, bulletLifeSpan.max);
255256

257+
inline function randomFloat(range:FlxBounds<Float>)
258+
return FlxG.random.float(range.min, range.max);
259+
256260
switch (mode)
257261
{
258262
case FIRE_AT_POSITION(x, y):
@@ -262,13 +266,25 @@ class FlxTypedWeapon<TBullet:FlxBullet> implements IFlxDestroyable
262266
internalFireAtPoint(currentBullet, target.getPosition(FlxPoint.weak()));
263267

264268
case FIRE_FROM_ANGLE(angle):
265-
internalFireFromAngle(currentBullet, FlxG.random.float(angle.min, angle.max));
269+
internalFireFromAngle(currentBullet, randomFloat(angle));
266270

267-
case FIRE_FROM_PARENT_ANGLE(angle):
268-
internalFireFromAngle(currentBullet, parent.angle + FlxG.random.float(angle.min, angle.max));
271+
case FIRE_FROM_PARENT_ANGLE(angleNoise):
272+
if (parent != null)
273+
{
274+
final angleOffset = parentAngleOffset + (angleNoise == null ? 0 : randomFloat(angleNoise));
275+
internalFireFromAngle(currentBullet, parent.angle + angleOffset);
276+
}
277+
else
278+
FlxG.log.warn("cannot use fireFromParentAngle");
269279

270-
case FIRE_FROM_PARENT_FACING(angle):
271-
internalFireFromAngle(currentBullet, parent.facing.degrees + FlxG.random.float(angle.min, angle.max));
280+
case FIRE_FROM_PARENT_FACING(angleNoise):
281+
if (parent != null)
282+
{
283+
final angleOffset = parentAngleOffset + (angleNoise == null ? 0 : randomFloat(angleNoise));
284+
internalFireFromAngle(currentBullet, parent.facing.degrees + angleOffset);
285+
}
286+
else
287+
FlxG.log.warn("cannot use fireFromParentFacing");
272288

273289
#if FLX_TOUCH
274290
case FIRE_AT_TOUCH(touch):
@@ -414,7 +430,7 @@ class FlxTypedWeapon<TBullet:FlxBullet> implements IFlxDestroyable
414430
* @return `true` if a bullet was fired, or `false` if one wasn't available.
415431
* A reference to the last fired bullet is stored in `currentBullet`.
416432
*/
417-
public inline function fireFromParentAngle(angle:FlxBounds<Float>):Bool
433+
public inline function fireFromParentAngle(?angle:FlxBounds<Float>):Bool
418434
{
419435
return runFire(FIRE_FROM_PARENT_ANGLE(angle));
420436
}
@@ -461,9 +477,15 @@ class FlxTypedWeapon<TBullet:FlxBullet> implements IFlxDestroyable
461477

462478
function shouldBulletHit(object:FlxObject, bullet:FlxObject):Bool
463479
{
464-
if (parent == object && skipParentCollision)
480+
if (skipParentCollision)
465481
{
466-
return false;
482+
switch(fireFrom)
483+
{
484+
case PARENT(parent, _, _, _):
485+
if (parent == object)
486+
return false;
487+
default:
488+
}
467489
}
468490

469491
if ((object is FlxTilemap))
@@ -529,39 +551,21 @@ class FlxTypedWeapon<TBullet:FlxBullet> implements IFlxDestroyable
529551
name = null;
530552
group = FlxDestroyUtil.destroy(group);
531553
bounds = FlxDestroyUtil.put(bounds);
532-
parent = null; // Don't destroy the parent
533-
positionOffset = FlxDestroyUtil.put(positionOffset);
534-
if (positionOffsetBounds != null)
535-
{
536-
positionOffsetBounds.min = FlxDestroyUtil.put(positionOffsetBounds.min);
537-
positionOffsetBounds.max = FlxDestroyUtil.put(positionOffsetBounds.max);
538-
positionOffsetBounds = null;
539-
}
540-
if (firePosition != null)
541-
{
542-
firePosition.min = FlxDestroyUtil.put(firePosition.min);
543-
firePosition.max = FlxDestroyUtil.put(firePosition.max);
544-
firePosition = null;
545-
}
546554
if (fireFrom != null)
547555
{
548556
switch (fireFrom)
549557
{
550558
case PARENT(parent, offset, useParentAngle, angleOffset):
551-
parent = null;
552559
if (offset != null)
553560
{
554561
offset.min = FlxDestroyUtil.put(offset.min);
555562
offset.max = FlxDestroyUtil.put(offset.max);
556-
offset = null;
557563
}
558-
angleOffset = null;
559564
case POSITION(position):
560565
if (position != null)
561566
{
562567
position.min = FlxDestroyUtil.put(position.min);
563568
position.max = FlxDestroyUtil.put(position.max);
564-
position = null;
565569
}
566570
}
567571
// fireFrom = null; // Can't do this because sending null to set_fireFrom() causes an NPE
@@ -588,45 +592,89 @@ class FlxTypedWeapon<TBullet:FlxBullet> implements IFlxDestroyable
588592
bulletFactory = null;
589593
}
590594

591-
function set_useParentDirection(v:Bool):Bool
595+
function get_parent():FlxSprite
596+
{
597+
return switch (fireFrom)
598+
{
599+
case PARENT(parent, _, _, _):parent;
600+
default: null;
601+
}
602+
}
603+
604+
function set_parent(value:FlxSprite):FlxSprite
592605
{
593606
switch (fireFrom)
594607
{
595-
case PARENT(parent, offset, useParentAngle, angleOffset):
596-
@:bypassAccessor fireFrom = PARENT(parent, offset, v, angleOffset);
608+
case PARENT(_, offset, useParentAngle, angleOffset):
609+
fireFrom = PARENT(value, offset, useParentAngle, angleOffset);
597610
default:
611+
FlxG.log.warn("Cannot set parent unless fireFrom is PARENT");
598612
}
599-
return v;
613+
return value;
600614
}
601615

602-
function set_fireFrom(v:FlxWeaponFireFrom):FlxWeaponFireFrom
616+
function get_useParentDirection():Bool
603617
{
604-
switch (v)
618+
switch (fireFrom)
605619
{
606-
case PARENT(parent, offset, useParentAngle, angleOffset):
607-
this.parent = parent;
608-
// this.positionOffset = offset;
609-
this.positionOffsetBounds = offset;
610-
this.useParentDirection = useParentAngle;
611-
if (angleOffset != null)
612-
this.angleOffset = angleOffset;
620+
case PARENT(_, _, useParentAngle, _):
621+
return useParentAngle;
622+
default:
623+
return null;
624+
}
625+
}
626+
627+
function set_useParentDirection(value:Bool):Bool
628+
{
629+
switch (fireFrom)
630+
{
631+
case PARENT(parent, offset, _, angleOffset):
632+
fireFrom = PARENT(parent, offset, value, angleOffset);
633+
default:
634+
FlxG.log.warn("Cannot set useParentDirection unless fireFrom is PARENT");
635+
}
636+
return value;
637+
}
613638

614-
this.firePosition = null;
615639

640+
function get_positionOffset():FlxPoint
641+
{
642+
switch (fireFrom)
643+
{
616644
case POSITION(position):
617-
this.firePosition = position;
645+
return position.min;
646+
default:
647+
return null;
648+
}
649+
}
618650

619-
this.parent = null;
620-
this.positionOffset = null;
621-
this.positionOffsetBounds = null;
651+
function set_positionOffset(value:FlxPoint):FlxPoint
652+
{
653+
switch (fireFrom)
654+
{
655+
case PARENT(parent, _, useParentAngle, angleOffset):
656+
fireFrom = PARENT(parent, new FlxBounds(value, value), useParentAngle, angleOffset);
657+
default:
658+
FlxG.log.warn("Cannot set positionOffset unless fireFrom is PARENT");
659+
}
660+
return value;
661+
}
622662

663+
function get_firePosition():FlxBounds<FlxPoint>
664+
{
665+
switch (fireFrom)
666+
{
667+
case POSITION(position):
668+
return position;
623669
default:
624-
this.parent = null;
625-
this.positionOffset = null;
626-
this.positionOffsetBounds = null;
627-
this.firePosition = null;
670+
return null;
628671
}
629-
return fireFrom = v;
672+
}
673+
674+
function set_firePosition(value:FlxBounds<FlxPoint>):FlxBounds<FlxPoint>
675+
{
676+
fireFrom = POSITION(value);
677+
return value;
630678
}
631679
}
632680

@@ -653,8 +701,8 @@ enum FlxWeaponFireMode
653701
FIRE_AT_POSITION(x:Float, y:Float);
654702
FIRE_AT_TARGET(target:FlxSprite);
655703
FIRE_FROM_ANGLE(angle:FlxBounds<Float>);
656-
FIRE_FROM_PARENT_ANGLE(angleNoise:FlxBounds<Float>);
657-
FIRE_FROM_PARENT_FACING(angleNoise:FlxBounds<Float>);
704+
FIRE_FROM_PARENT_ANGLE(?angleNoise:FlxBounds<Float>);
705+
FIRE_FROM_PARENT_FACING(?angleNoise:FlxBounds<Float>);
658706

659707
#if FLX_TOUCH
660708
FIRE_AT_TOUCH(touch:FlxTouch);

0 commit comments

Comments
 (0)