-
Notifications
You must be signed in to change notification settings - Fork 141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add custom bounds implementation for game objects #408
Open
stevesoltys
wants to merge
3
commits into
apollo-rsps:kotlin-experiments
Choose a base branch
from
stevesoltys:feature/game-object-bounds
base: kotlin-experiments
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
176 changes: 176 additions & 0 deletions
176
game/src/main/java/org/apollo/game/model/entity/obj/GameObjectBounds.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,176 @@ | ||
package org.apollo.game.model.entity.obj; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import org.apollo.game.model.Direction; | ||
import org.apollo.game.model.Position; | ||
import org.apollo.game.model.entity.EntityBounds; | ||
|
||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
/** | ||
* The bounds of a {@link GameObject}. | ||
* | ||
* @author Steve Soltys | ||
*/ | ||
public class GameObjectBounds extends EntityBounds { | ||
|
||
/** | ||
* An {@link ImmutableMap} of Directions mapped to their mask bits for a {@link GameObject} interaction mask. | ||
*/ | ||
private static final Map<Direction, Integer> INTERACTION_MASK_BITS = ImmutableMap.<Direction, Integer>builder() | ||
.put(Direction.NORTH, 0x1) | ||
.put(Direction.EAST, 0x8) | ||
.put(Direction.SOUTH, 0x4) | ||
.put(Direction.WEST, 0x2) | ||
.build(); | ||
|
||
/** | ||
* The GameObject. | ||
*/ | ||
private final GameObject gameObject; | ||
|
||
/** | ||
* The set of positions where the GameObject can be interacted with. | ||
*/ | ||
private Set<Position> interactionPositions; | ||
|
||
/** | ||
* Creates an EntityBounds. | ||
* | ||
* @param gameObject The GameObject. | ||
*/ | ||
GameObjectBounds(GameObject gameObject) { | ||
super(gameObject); | ||
|
||
this.gameObject = gameObject; | ||
} | ||
|
||
/** | ||
* Gets the set of positions where the GameObject can be interacted with. | ||
* | ||
* @return The set of positions. | ||
*/ | ||
public Set<Position> getInteractionPositions() { | ||
if (interactionPositions == null) { | ||
interactionPositions = computeInteractionPositions(); | ||
} | ||
|
||
return interactionPositions; | ||
} | ||
|
||
/** | ||
* Computes the set of positions where the GameObject can be interacted with. | ||
* | ||
* @return The set of positions. | ||
*/ | ||
private Set<Position> computeInteractionPositions() { | ||
|
||
switch (ObjectType.valueOf(gameObject.getType())) { | ||
case DIAGONAL_WALL: | ||
return getDiagonalWallInteractionPositions(); | ||
|
||
case LENGTHWISE_WALL: | ||
return getWallInteractionPositions(); | ||
|
||
case INTERACTABLE: | ||
return getInteractablePositions(); | ||
|
||
// TODO: Figure out the rest. | ||
} | ||
|
||
return Collections.emptySet(); | ||
} | ||
|
||
/** | ||
* Gets the set of positions for a diagonal wall where the GameObject can be interacted with. | ||
* | ||
* @return The set of interaction positions. | ||
*/ | ||
private Set<Position> getDiagonalWallInteractionPositions() { | ||
Set<Position> positions = new HashSet<>(); | ||
Direction direction = Direction.WNES[gameObject.getOrientation()]; | ||
Position position = gameObject.getPosition(); | ||
|
||
// TODO: I think this either is missing one, or has one too many. Need to confirm these directions. | ||
positions.add(position.step(1, direction)); | ||
positions.add(position.step(1, direction.counterClockwise())); | ||
positions.add(position.step(1, direction.counterClockwise(2))); | ||
positions.add(position.step(1, direction.clockwise())); | ||
positions.add(position.step(1, direction.clockwise(2))); | ||
positions.add(position.step(1, direction.opposite())); | ||
positions.add(position.step(1, direction.opposite().counterClockwise())); | ||
return positions; | ||
} | ||
|
||
/** | ||
* Gets the set of Directions from which the GameObject can be interacted with. | ||
* | ||
* @return The interactable directions. | ||
*/ | ||
private Set<Direction> getInteractableDirections() { | ||
Set<Direction> interactableDirections = new HashSet<>(); | ||
|
||
int interactionMask = gameObject.getDefinition().getInteractionMask(); | ||
int rotatedInteractionMask = (interactionMask << gameObject.getOrientation() & 0xf) + | ||
(interactionMask >> 4 - gameObject.getOrientation()); | ||
|
||
for (Direction direction : Direction.NESW) { | ||
boolean interactive = (rotatedInteractionMask & INTERACTION_MASK_BITS.get(direction)) == 0; | ||
|
||
if (interactive) { | ||
interactableDirections.add(direction); | ||
} | ||
} | ||
|
||
return interactableDirections; | ||
} | ||
|
||
/** | ||
* Gets the set of positions where the GameObject can be interacted with. | ||
* | ||
* @return The set of interaction positions. | ||
*/ | ||
private Set<Position> getInteractablePositions() { | ||
Set<Direction> interactableDirections = getInteractableDirections(); | ||
Set<Position> interactablePositions = new HashSet<>(); | ||
|
||
Position position = gameObject.getPosition(); | ||
int width = gameObject.getWidth(); | ||
int length = gameObject.getLength(); | ||
|
||
for (int deltaX = position.getX(); deltaX < position.getX() + width; deltaX++) { | ||
for (int deltaY = position.getY(); deltaY < position.getY() + length; deltaY++) { | ||
|
||
for(Direction direction : interactableDirections) { | ||
Position deltaPosition = new Position(deltaX, deltaY, position.getHeight()).step(1, direction); | ||
|
||
if(!contains(deltaPosition)) { | ||
interactablePositions.add(deltaPosition); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return interactablePositions; | ||
} | ||
|
||
/** | ||
* Gets the set of positions for a wall where the GameObject can be interacted with. | ||
* | ||
* @return The set of interaction positions. | ||
*/ | ||
private Set<Position> getWallInteractionPositions() { | ||
Set<Position> positions = new HashSet<>(); | ||
Direction objectDirection = Direction.WNES[gameObject.getOrientation()]; | ||
Position position = gameObject.getPosition(); | ||
|
||
positions.add(position); | ||
positions.add(position.step(1, objectDirection)); | ||
positions.add(position.step(1, objectDirection.clockwise(2))); | ||
positions.add(position.step(1, objectDirection.counterClockwise(2))); | ||
return positions; | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need an example of this in use. I'm not sure that creating this
Set
all the time is optimal. Maybe roll this up into your distanced action changes?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@garyttierney In the distanced action we provide an
Entity
and check the interaction positions of that entity to decide when to trigger the action for aMob
.Here's an example: https://gist.github.com/stevesoltys/197e7a604f8bfe495ae070c2db0c89c1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that in my example, I've made the
getInteractionPositions
method abstract inEntityBounds
.