RuneHive-Game
Loading...
Searching...
No Matches
CustomGameObject.java
Go to the documentation of this file.
1package com.runehive.game.world.object;
2
3import com.runehive.game.world.World;
4import com.runehive.game.world.entity.Entity;
5import com.runehive.game.world.entity.EntityType;
6import com.runehive.game.world.entity.mob.Mob;
7import com.runehive.game.world.entity.mob.player.Player;
8import com.runehive.game.world.pathfinding.TraversalMap;
9import com.runehive.game.world.position.Position;
10import com.runehive.game.world.region.Region;
11import com.runehive.net.packet.out.SendAddObject;
12import com.runehive.net.packet.out.SendRemoveObject;
13import com.runehive.util.generic.GenericAttributes;
14
15import java.util.Objects;
16
17import static com.runehive.game.world.object.ObjectDirection.NORTH;
18import static com.runehive.game.world.object.ObjectDirection.SOUTH;
19
20/**
21 * Represents a static game object loaded from the map fs.
22 *
23 * @author Michael | Chex
24 */
25public class CustomGameObject extends Entity implements GameObject {
26
27 /** The object definition. */
29
30 /** The object direction. */
32
33 /** The object type. */
35
36 /** The generic attributes. */
38
40 super(position);
41 this.definition = GameObjectDefinition.forId(id);
42 this.instance = instance;
43 this.direction = direction;
44 this.type = type;
45 }
46
50
52 this(id, instance, position, NORTH, ObjectType.GENERAL_PROP);
53 }
54
58
59 @Override
60 public int getInstancedHeight() {
61 return instance;
62 }
63
64 @Override
70
71 @Override
73 return definition;
74 }
75
76 @Override
77 public int width() {
78 if (direction == NORTH || direction == SOUTH) {
79 return definition.getLength();
80 }
81 return definition.getWidth();
82 }
83
84 @Override
85 public int length() {
86 if (direction == NORTH || direction == SOUTH) {
87 return definition.getWidth();
88 }
89 return definition.getLength();
90 }
91
92 @Override
93 public int distance() {
94 return definition.getDistance();
95 }
96
97 @Override
99 return type;
100 }
101
102 @Override
104 return direction;
105 }
106
107 @Override
108 public void register() {
109 if (!isRegistered()) {
111 setRegistered(true);
112
113 if (region == null) {
115 } else if (!region.containsObject(getHeight(), this)) {
117 }
118
119 Region.ACTIVE_OBJECT.put(getPosition(), this);
120 }
121 }
122
123 @Override
124 public void unregister() {
125 if (isRegistered()) {
126 Region.ACTIVE_OBJECT.remove(getPosition(), this);
128 destroy();
129 }
130 }
131
132 @Override
133 public boolean active() {
134 return isRegistered();
135 }
136
137 @Override
138 public void addToRegion(Region objectRegion) {
139 if (!objectRegion.containsObject(getHeight(), this)) {
140 TraversalMap.markObject(objectRegion, this, true, true);
142 for (Player other : region.getPlayers(getHeight())) {
143 if (other.instance != getInstancedHeight())
144 continue;
145 other.send(new SendAddObject(this));
146 }
147 }
148 }
149 }
150
151 @Override
152 public void removeFromRegion(Region objectRegion) {
153 if (objectRegion.containsObject(getHeight(), this)) {
154 TraversalMap.markObject(objectRegion, this, false, true);
156 for (Player other : region.getPlayers(getHeight())) {
157 if (other.instance != getInstancedHeight()) {
158 continue;
159 }
160 other.send(new SendRemoveObject(this));
161 }
162 }
163 }
164 }
165
166 @Override
167 public void transform(int id) {
168 unregister();
170 register();
171 }
172
173 @Override
175 unregister();
176 this.direction = direction;
177 register();
178 }
179
180 @Override
181 public String getName() {
182 return definition.getName();
183 }
184
185 @Override
188 }
189
190 @Override
191 public int hashCode() {
192 return Objects.hash(definition.getId(), getPosition());
193 }
194
195 @Override
196 public boolean equals(Object obj) {
197 if (obj instanceof CustomGameObject) {
199 return definition == other.definition && getPosition().equals(other.getPosition()) && getInstancedHeight() == other.getInstancedHeight();
200 }
201 return obj == this;
202 }
203
204 @Override
205 public String toString() {
206 return String.format("CustomGameObject[id=%s, loc=%s, width=%s, len=%s, rot=%s, type=%s]", getId(), getPosition(), width(), length(), getDirection(), getObjectType());
207 }
208}
Represents the game world.
Definition World.java:46
static RegionManager getRegions()
Definition World.java:552
void setRegistered(boolean registered)
Definition Entity.java:98
Entity destroy()
Destroys this entity.
Definition Entity.java:143
void setPosition(Position position)
Definition Entity.java:106
Handles the mob class.
Definition Mob.java:66
This class represents a character controlled by a player.
Definition Player.java:125
ObjectType getObjectType()
Gets the object type.
GameObjectDefinition getDefinition()
Gets the object definition.
GenericAttributes genericAttributes
The generic attributes.
void addToRegion(Region objectRegion)
Adds this entity to the specified region.
CustomGameObject(int id, int instance, Position position, ObjectDirection direction, ObjectType type)
String getName()
Gets the name of this entity.
CustomGameObject(int id, int instance, Position position)
void unregister()
Unregisters an entity from the World.
void removeFromRegion(Region objectRegion)
Removes this entity from the specified region.
boolean active()
Determines if this object is active on the world.
void register()
Registers an entity to the World.
ObjectDirection direction
The object direction.
GameObjectDefinition definition
The object definition.
CustomGameObject(int id, Position position, ObjectDirection direction, ObjectType type)
ObjectDirection getDirection()
Gets the rotation.
GenericAttributes getGenericAttributes()
Gets the generic attributes.
static GameObjectDefinition forId(int id)
Gets an object definition by its id.
Contains traversal data for a set of regions.
static void markObject(Region region, GameObject object, boolean add, boolean list)
Marks a GameObject with the specified attributes on the specified Position to the TraversalMap.
Represents a single tile on the game world.
Definition Position.java:14
Represents a single region.
Definition Region.java:21
boolean containsObject(int height, GameObject object)
Definition Region.java:168
static final Map< Position, GameObject > ACTIVE_OBJECT
Definition Region.java:25
Region[] getSurroundingRegions(Position position)
Gets the regions surrounding a position.
The enumerated type whose elements represent the directions for objects.
The enumerated type whose elements represent all of the object types.
GENERAL_PROP
Represents all kinds of objects, trees, statues, signs, fountains etc.
default int getId()
Gets the object id.