RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
CustomGameObject.java
1package com.osroyale.game.world.object;
2
3import com.osroyale.game.world.World;
4import com.osroyale.game.world.entity.Entity;
5import com.osroyale.game.world.entity.EntityType;
6import com.osroyale.game.world.entity.mob.Mob;
7import com.osroyale.game.world.entity.mob.player.Player;
8import com.osroyale.game.world.pathfinding.TraversalMap;
9import com.osroyale.game.world.position.Position;
10import com.osroyale.game.world.region.Region;
11import com.osroyale.net.packet.out.SendAddObject;
12import com.osroyale.net.packet.out.SendRemoveObject;
13import com.osroyale.util.generic.GenericAttributes;
14
15import java.util.Objects;
16
17import static com.osroyale.game.world.object.ObjectDirection.NORTH;
18import static com.osroyale.game.world.object.ObjectDirection.SOUTH;
19
61
62public class CustomGameObject extends Entity implements GameObject {
63
65 private GameObjectDefinition definition;
66
68 private ObjectDirection direction;
69
71 private ObjectType type;
72
74 private GenericAttributes genericAttributes;
75
76 public CustomGameObject(int id, int instance, Position position, ObjectDirection direction, ObjectType type) {
77 super(position);
78 this.definition = GameObjectDefinition.forId(id);
79 this.instance = instance;
80 this.direction = direction;
81 this.type = type;
82 }
83
84 public CustomGameObject(int id, Position position, ObjectDirection direction, ObjectType type) {
85 this(id, Mob.DEFAULT_INSTANCE, position, direction, type);
86 }
87
88 public CustomGameObject(int id, int instance, Position position) {
89 this(id, instance, position, NORTH, ObjectType.GENERAL_PROP);
90 }
91
92 public CustomGameObject(int id, Position position) {
93 this(id, Mob.DEFAULT_INSTANCE, position, NORTH, ObjectType.GENERAL_PROP);
94 }
95
96 @Override
97 public int getInstancedHeight() {
98 return instance;
99 }
100
101 @Override
103 if (genericAttributes == null)
104 genericAttributes = new GenericAttributes();
105 return genericAttributes;
106 }
107
108 @Override
110 return definition;
111 }
112
113 @Override
114 public int width() {
115 if (direction == NORTH || direction == SOUTH) {
116 return definition.getLength();
117 }
118 return definition.getWidth();
119 }
120
121 @Override
122 public int length() {
123 if (direction == NORTH || direction == SOUTH) {
124 return definition.getWidth();
125 }
126 return definition.getLength();
127 }
128
129 @Override
130 public int distance() {
131 return definition.getDistance();
132 }
133
134 @Override
136 return type;
137 }
138
139 @Override
141 return direction;
142 }
143
144 @Override
145 public void register() {
146 if (!isRegistered()) {
147 Region region = getRegion();
148 setRegistered(true);
149
150 if (region == null) {
151 setPosition(getPosition());
152 } else if (!region.containsObject(getHeight(), this)) {
153 addToRegion(region);
154 }
155
156 Region.ACTIVE_OBJECT.put(getPosition(), this);
157 }
158 }
159
160 @Override
161 public void unregister() {
162 if (isRegistered()) {
163 Region.ACTIVE_OBJECT.remove(getPosition(), this);
164 removeFromRegion(getRegion());
165 destroy();
166 }
167 }
168
169 @Override
170 public boolean active() {
171 return isRegistered();
172 }
173
174 @Override
175 public void addToRegion(Region objectRegion) {
176 if (!objectRegion.containsObject(getHeight(), this)) {
177 TraversalMap.markObject(objectRegion, this, true, true);
178 for (Region region : World.getRegions().getSurroundingRegions(getPosition())) {
179 for (Player other : region.getPlayers(getHeight())) {
180 if (other.instance != getInstancedHeight())
181 continue;
182 other.send(new SendAddObject(this));
183 }
184 }
185 }
186 }
187
188 @Override
189 public void removeFromRegion(Region objectRegion) {
190 if (objectRegion.containsObject(getHeight(), this)) {
191 TraversalMap.markObject(objectRegion, this, false, true);
192 for (Region region : World.getRegions().getSurroundingRegions(getPosition())) {
193 for (Player other : region.getPlayers(getHeight())) {
194 if (other.instance != getInstancedHeight()) {
195 continue;
196 }
197 other.send(new SendRemoveObject(this));
198 }
199 }
200 }
201 }
202
203 @Override
204 public void transform(int id) {
205 unregister();
206 definition = GameObjectDefinition.forId(id);
207 register();
208 }
209
210 @Override
211 public void rotate(ObjectDirection direction) {
212 unregister();
213 this.direction = direction;
214 register();
215 }
216
217 @Override
218 public String getName() {
219 return definition.getName();
220 }
221
222 @Override
224 return EntityType.CUSTOM_OBJECT;
225 }
226
227 @Override
228 public int hashCode() {
229 return Objects.hash(definition.getId(), getPosition());
230 }
231
232 @Override
233 public boolean equals(Object obj) {
234 if (obj instanceof CustomGameObject) {
235 CustomGameObject other = (CustomGameObject) obj;
236 return definition == other.definition && getPosition().equals(other.getPosition()) && getInstancedHeight() == other.getInstancedHeight();
237 }
238 return obj == this;
239 }
240
241 @Override
242 public String toString() {
243 return String.format("CustomGameObject[id=%s, loc=%s, width=%s, len=%s, rot=%s, type=%s]", getId(), getPosition(), width(), length(), getDirection(), getObjectType());
244 }
245}
static void markObject(Region region, GameObject object, boolean add, boolean list)
boolean containsObject(int height, GameObject object)
Definition Region.java:205
Region[] getSurroundingRegions(Position position)