RuneHive-Game
Loading...
Searching...
No Matches
Entity.java
Go to the documentation of this file.
1package com.runehive.game.world.entity;
2
3import com.runehive.game.world.Interactable;
4import com.runehive.game.world.position.Position;
5import com.runehive.game.world.region.Region;
6
7/**
8 * Represents a character in the game world, i.e. a {@code Player} or an
9 * {@code NPC}.
10 *
11 * @author Michael | Chex
12 */
13public abstract class Entity implements Interactable {
14 public static final int DEFAULT_INSTANCE = 0;
15
16 private int index;
17 private int width;
18 private int length;
19 private boolean registered;
20 private boolean visible;
24
26 this(position, true);
27 }
28
29 public Entity(Position position, boolean visible) {
32 this.width = 1;
33 this.length = 1;
34 }
35
36 public int getIndex() {
37 return index;
38 }
39
41 return position;
42 }
43
45 final int width = width();
46 final int length = length();
48 return width == 1 && length == 1 ? position
49 : new Position(position.getCoordFaceX(width), position.getCoordFaceY(getHeight()), position.getHeight());
50 }
51
52 @Override
53 public int width() {
54 return width;
55 }
56
57 @Override
58 public int length() {
59 return length;
60 }
61
62 public final boolean isRegistered() {
63 return registered;
64 }
65
66 public boolean isVisible() {
67 return visible;
68 }
69
70 public Region getRegion() {
71 return currentRegion;
72 }
73
74 public int getX() {
75 return position.getX();
76 }
77
78 public int getY() {
79 return position.getY();
80 }
81
82 public int getHeight() {
83 return position.getHeight();
84 }
85
86 public void setIndex(int index) {
87 this.index = index;
88 }
89
90 public void setWidth(int width) {
91 this.width = width;
92 }
93
94 public void setLength(int length) {
95 this.length = length;
96 }
97
98 protected void setRegistered(boolean registered) {
99 this.registered = registered;
100 }
101
102 public void setVisible(boolean visible) {
103 this.visible = visible;
104 }
105
107 Region region = position.getRegion();
108
109 if (!registered || (region == currentRegion && position.getHeight() == getHeight())) {
110 this.position = position;
111 return;
112 }
113
114 if (currentRegion != null) {
116 }
117
118 this.position = position;
120 }
121
122 public void setX(int x) {
123 position = new Position(x, getY(), getHeight());
124 }
125
126 public void setY(int y) {
127 position = new Position(getX(), y, getHeight());
128 }
129
130 public void setZ(int z) {
131 position = new Position(getX(), getY(), z);
132 }
133
134 public boolean is(EntityType type) {
135 return getType() == type;
136 }
137
138 /**
139 * Destroys this entity.
140 *
141 * @return This entity.
142 */
143 protected Entity destroy() {
145 setRegistered(false);
146 return this;
147 }
148
149 /**
150 * Validates this npc based on its current region and registered state.
151 *
152 * @return {@code True} if this npc is a valid log in the game world.
153 */
154 public boolean isValid() {
155 return isRegistered() && currentRegion != null;
156
157 }
158
159 /** Registers an entity to the {@code World}. */
160 public abstract void register();
161
162 /** Unregisters an entity from the {@code World}. */
163 public abstract void unregister();
164
165 /**
166 * Adds this entity to the specified region.
167 *
168 * @param region The region.
169 */
170 public abstract void addToRegion(Region region);
171
172 /**
173 * Removes this entity from the specified region.
174 *
175 * @param region The region.
176 */
177 public abstract void removeFromRegion(Region region);
178
179 public void onStep() {
180 }
181
182 /**
183 * Gets the name of this entity.
184 *
185 * @return The entity's name.
186 */
187 public abstract String getName();
188
189 /**
190 * Gets the {@code EntityType}.
191 *
192 * @return The {@code EntityType}.
193 */
194 public abstract EntityType getType();
195
196
197 @Override
198 public abstract boolean equals(Object obj);
199
200 @Override
201 public abstract int hashCode();
202
203 @Override
204 public String toString() {
205 return String.format("Entity[registered=%s, visible=%s, position=%s, type=%s", isRegistered(), isVisible(), getPosition(), getType());
206 }
207
208 /** Check if an entity is an object */
209 public final boolean isStaticObject() {
210 return getType() == EntityType.STATIC_OBJECT;
211 }
212
213 /** Check if an entity is an object */
214 public final boolean isCustomObject() {
215 return getType() == EntityType.CUSTOM_OBJECT;
216 }
217
218}
void setVisible(boolean visible)
Definition Entity.java:102
void setRegistered(boolean registered)
Definition Entity.java:98
abstract void register()
Registers an entity to the World.
abstract void addToRegion(Region region)
Adds this entity to the specified region.
Entity(Position position, boolean visible)
Definition Entity.java:29
abstract void removeFromRegion(Region region)
Removes this entity from the specified region.
abstract EntityType getType()
Gets the EntityType.
final boolean isCustomObject()
Check if an entity is an object.
Definition Entity.java:214
boolean is(EntityType type)
Definition Entity.java:134
boolean isValid()
Validates this npc based on its current region and registered state.
Definition Entity.java:154
abstract String getName()
Gets the name of this entity.
abstract boolean equals(Object obj)
Entity destroy()
Destroys this entity.
Definition Entity.java:143
final boolean isStaticObject()
Check if an entity is an object.
Definition Entity.java:209
abstract void unregister()
Unregisters an entity from the World.
void setPosition(Position position)
Definition Entity.java:106
Represents a single tile on the game world.
Definition Position.java:14
Represents a single region.
Definition Region.java:21
An object implementing Interactable has uses.