-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add bounce on impact projectile item #952
- Loading branch information
Showing
8 changed files
with
257 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...java/bassebombecraft/item/composite/projectile/modifier/BounceProjectileModifierItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package bassebombecraft.item.composite.projectile.modifier; | ||
|
||
import static bassebombecraft.config.ModConfiguration.bounceProjectileModifierItem; | ||
import static bassebombecraft.operator.DefaultPorts.getFnGetEntities1; | ||
|
||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
import bassebombecraft.event.projectile.ProjectileModifierEventHandler; | ||
import bassebombecraft.item.composite.GenericCompositeNullItem; | ||
import bassebombecraft.operator.Operator2; | ||
import bassebombecraft.operator.Ports; | ||
import bassebombecraft.operator.entity.raytraceresult.Bounce2; | ||
import bassebombecraft.operator.projectile.modifier.TagProjectileWithProjectileModifier; | ||
import net.minecraft.entity.Entity; | ||
|
||
/** | ||
* Bounce on impact projectile modifier item. | ||
* | ||
* The tagged projectile is processed in | ||
* {@linkplain ProjectileModifierEventHandler} where the {@linkplain Bounce2} | ||
* operator is executed. | ||
*/ | ||
public class BounceProjectileModifierItem extends GenericCompositeNullItem { | ||
|
||
/** | ||
* Item identifier. | ||
*/ | ||
public static final String NAME = BounceProjectileModifierItem.class.getSimpleName(); | ||
|
||
/** | ||
* Create operators. | ||
*/ | ||
static Supplier<Operator2> splOp = () -> { | ||
Function<Ports, Entity[]> fnGetProjectiles = getFnGetEntities1(); | ||
Function<Ports, String> fnGetTag = p -> Bounce2.NAME; | ||
return new TagProjectileWithProjectileModifier(fnGetProjectiles, fnGetTag); | ||
}; | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
public BounceProjectileModifierItem() { | ||
super(NAME, bounceProjectileModifierItem); | ||
} | ||
|
||
@Override | ||
public Operator2 createOperator() { | ||
return splOp.get(); | ||
} | ||
|
||
} |
135 changes: 135 additions & 0 deletions
135
src/main/java/bassebombecraft/operator/entity/raytraceresult/Bounce2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package bassebombecraft.operator.entity.raytraceresult; | ||
|
||
import static bassebombecraft.entity.projectile.ProjectileUtils.isBlockHit; | ||
import static bassebombecraft.entity.projectile.ProjectileUtils.isNothingHit; | ||
import static bassebombecraft.entity.projectile.ProjectileUtils.isTypeBlockRayTraceResult; | ||
import static bassebombecraft.operator.DefaultPorts.getFnGetEntity1; | ||
import static bassebombecraft.operator.DefaultPorts.getFnGetRayTraceResult1; | ||
|
||
import java.util.function.Function; | ||
|
||
import bassebombecraft.operator.Operator2; | ||
import bassebombecraft.operator.Ports; | ||
import net.minecraft.entity.Entity; | ||
import net.minecraft.util.Direction; | ||
import net.minecraft.util.Direction.Axis; | ||
import net.minecraft.util.math.BlockRayTraceResult; | ||
import net.minecraft.util.math.RayTraceResult; | ||
import net.minecraft.util.math.Vec3d; | ||
|
||
/** | ||
* Implementation of the {@linkplain Operator2} interface which bounces | ||
* projectile on impact on block. | ||
*/ | ||
public class Bounce2 implements Operator2 { | ||
|
||
/** | ||
* Operator identifier. | ||
*/ | ||
public static final String NAME = Bounce2.class.getSimpleName(); | ||
|
||
/** | ||
* Null entity for block explosions. | ||
*/ | ||
static final Entity NULL_ENTITY = null; | ||
|
||
/** | ||
* Function to get ray trace result. | ||
*/ | ||
Function<Ports, RayTraceResult> fnGetRayTraceResult; | ||
|
||
/** | ||
* Function to get projectile entity. | ||
*/ | ||
Function<Ports, Entity> fnGetProjectile; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param splRayTraceResult function to get ray trace result. | ||
* @param fnGetProjectile function to get projectile entity. | ||
*/ | ||
public Bounce2(Function<Ports, RayTraceResult> fnGetRayTraceResult, | ||
Function<Ports, Entity> fnGetProjectile) { | ||
this.fnGetRayTraceResult = fnGetRayTraceResult; | ||
this.fnGetProjectile = fnGetProjectile; | ||
} | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* Instance is configured with ray tracing result #1 from ports. | ||
* | ||
* Instance is configured with entity #1 as projectile from ports. | ||
*/ | ||
public Bounce2() { | ||
this(getFnGetRayTraceResult1(), getFnGetEntity1()); | ||
} | ||
|
||
@Override | ||
public Ports run(Ports ports) { | ||
|
||
// get ray trace result | ||
RayTraceResult result = fnGetRayTraceResult.apply(ports); | ||
if (result == null) | ||
return ports; | ||
|
||
// get projectile | ||
Entity projectile = fnGetProjectile.apply(ports); | ||
if (projectile == null) | ||
return ports; | ||
|
||
// get motion vector | ||
Vec3d motionVector = projectile.getMotion(); | ||
if (motionVector == null) | ||
return ports; | ||
|
||
// exit if nothing was hit | ||
if (isNothingHit(result)) | ||
return ports; | ||
|
||
// bounce projectile motion | ||
if (isBlockHit(result)) { | ||
|
||
// exit if result isn't block ray trace result | ||
if (!isTypeBlockRayTraceResult(result)) | ||
return ports; | ||
|
||
// type cast | ||
BlockRayTraceResult blockResult = (BlockRayTraceResult) result; | ||
|
||
// get impact info | ||
Direction impactFace = blockResult.getFace(); | ||
Axis impactAxis = impactFace.getAxis(); | ||
|
||
// calculate bounced motion vector | ||
Vec3d bouncedVector = bounceMotionVector(impactAxis, motionVector); | ||
|
||
// set bounced motion | ||
projectile.setMotion(bouncedVector); | ||
} | ||
|
||
return ports; | ||
} | ||
|
||
/** | ||
* Calculate bounced motion vector. | ||
* | ||
* @param impactAxis impact axis. | ||
* @param motionVector motion vector. | ||
* | ||
* @return bounced motion vector | ||
*/ | ||
Vec3d bounceMotionVector(Axis impactAxis, Vec3d motionVector) { | ||
switch(impactAxis) { | ||
case X: | ||
return motionVector.mul(-1, 1, 1); | ||
case Y: | ||
return motionVector.mul(1, -1, 1); | ||
case Z: | ||
return motionVector.mul(1, 1, -1); | ||
} | ||
return motionVector; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/resources/assets/bassebombecraft/models/item/bounceprojectilemodifieritem.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"parent": "item/handheld", | ||
"textures": { | ||
"layer0": "bassebombecraft:item/bounceprojectilemodifieritem" | ||
}, | ||
"display": { | ||
"thirdperson_righthand": { | ||
"rotation": [ 0, -15, 0 ], | ||
"translation": [ 0, 0, 0 ], | ||
"scale": [ 0.5, 0.5, 0.5 ] | ||
}, | ||
"thirdperson_lefthand": { | ||
"rotation": [ 0, -15, 0 ], | ||
"translation": [ 0, 0, 0 ], | ||
"scale": [ 0.5, 0.5, 0.5 ] | ||
}, | ||
"firstperson_righthand": { | ||
"rotation": [ 0, -90, 25 ], | ||
"translation": [ 0, 4, 2 ], | ||
"scale": [ 0.5, 0.5, 0.5 ] | ||
}, | ||
"firstperson_lefthand": { | ||
"rotation": [ 0, 90, -25 ], | ||
"translation": [ 0, 4, 2 ], | ||
"scale": [ 0.5, 0.5, 0.5 ] | ||
} | ||
} | ||
} |
Binary file added
BIN
+441 Bytes
...resources/assets/bassebombecraft/textures/item/bounceprojectilemodifieritem.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.