RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
Entity.java
1package com.osroyale.game.world.entity;
2
3import com.osroyale.game.world.Interactable;
4import com.osroyale.game.world.position.Position;
5import com.osroyale.game.world.region.Region;
6
49
50public abstract class Entity implements Interactable {
51 public static final int DEFAULT_INSTANCE = 0;
52
53 private int index;
54 private int width;
55 private int length;
56 private boolean registered;
57 private boolean visible;
58 private Position position;
59 private Region currentRegion;
60 public int instance = DEFAULT_INSTANCE;
61
62 public Entity(Position position) {
63 this(position, true);
64 }
65
66 public Entity(Position position, boolean visible) {
67 setPosition(position);
68 setVisible(visible);
69 this.width = 1;
70 this.length = 1;
71 }
72
73 public int getIndex() {
74 return index;
75 }
76
78 return position;
79 }
80
81 public Position getCenterPosition() {
82 final int width = width();
83 final int length = length();
84 final Position position = getPosition();
85 return width == 1 && length == 1 ? position
86 : new Position(position.getCoordFaceX(width), position.getCoordFaceY(getHeight()), position.getHeight());
87 }
88
89 @Override
90 public int width() {
91 return width;
92 }
93
94 @Override
95 public int length() {
96 return length;
97 }
98
99 public final boolean isRegistered() {
100 return registered;
101 }
102
103 public boolean isVisible() {
104 return visible;
105 }
106
107 public Region getRegion() {
108 return currentRegion;
109 }
110
111 public int getX() {
112 return position.getX();
113 }
114
115 public int getY() {
116 return position.getY();
117 }
118
119 public int getHeight() {
120 return position.getHeight();
121 }
122
123 public void setIndex(int index) {
124 this.index = index;
125 }
126
127 public void setWidth(int width) {
128 this.width = width;
129 }
130
131 public void setLength(int length) {
132 this.length = length;
133 }
134
135 protected void setRegistered(boolean registered) {
136 this.registered = registered;
137 }
138
139 public void setVisible(boolean visible) {
140 this.visible = visible;
141 }
142
143 public void setPosition(Position position) {
144 Region region = position.getRegion();
145
146 if (!registered || (region == currentRegion && position.getHeight() == getHeight())) {
147 this.position = position;
148 return;
149 }
150
151 if (currentRegion != null) {
152 removeFromRegion(currentRegion);
153 }
154
155 this.position = position;
156 addToRegion(currentRegion = region);
157 }
158
159 public void setX(int x) {
160 position = new Position(x, getY(), getHeight());
161 }
162
163 public void setY(int y) {
164 position = new Position(getX(), y, getHeight());
165 }
166
167 public void setZ(int z) {
168 position = new Position(getX(), getY(), z);
169 }
170
171 public boolean is(EntityType type) {
172 return getType() == type;
173 }
174
180 protected Entity destroy() {
181 removeFromRegion(currentRegion);
182 setRegistered(false);
183 return this;
184 }
185
191 public boolean isValid() {
192 return isRegistered() && currentRegion != null;
193
194 }
195
197 public abstract void register();
198
200 public abstract void unregister();
201
207 public abstract void addToRegion(Region region);
208
214 public abstract void removeFromRegion(Region region);
215
216 public void onStep() {
217 }
218
224 public abstract String getName();
225
231 public abstract EntityType getType();
232
233
234 @Override
235 public abstract boolean equals(Object obj);
236
237 @Override
238 public abstract int hashCode();
239
240 @Override
241 public String toString() {
242 return String.format("Entity[registered=%s, visible=%s, position=%s, type=%s", isRegistered(), isVisible(), getPosition(), getType());
243 }
244
246 public final boolean isStaticObject() {
247 return getType() == EntityType.STATIC_OBJECT;
248 }
249
251 public final boolean isCustomObject() {
252 return getType() == EntityType.CUSTOM_OBJECT;
253 }
254
255}
abstract void removeFromRegion(Region region)
abstract void addToRegion(Region region)