RuneHive-Game
Loading...
Searching...
No Matches
Viewport.java
Go to the documentation of this file.
1package com.runehive.game.world.entity.mob;
2
3import com.runehive.game.world.entity.mob.npc.Npc;
4import com.runehive.game.world.entity.mob.player.Player;
5
6import java.util.LinkedList;
7import java.util.List;
8import java.util.concurrent.atomic.AtomicInteger;
9
10/**
11 * Represents a viewport in which a {@link Player} can see.
12 *
13 * @author nshusa
14 */
15public final class Viewport {
16
17 /** The amount of entities that can be added to this viewport in a single tick **/
18 public static final int ADD_THRESHOLD = 15;
19
20 /** The amount of entities that can be visible at all at once 255 players and 255 npcs **/
21 public static final int CAPACITY = 100;
22
23 /** The amount of tiles out an entity can see within this viewport **/
24 public static final int VIEW_DISTANCE = 15;
25
26 /** The collection of players that are visible in this viewport **/
27 private final List<Player> playersInViewport = new LinkedList<>();
28
29 /** The collection of npcs that are visible in this viewport **/
30 private final List<Npc> npcsInViewport = new LinkedList<>();
31
32 /** The amount of tiles out this mob can see players **/
33 private final AtomicInteger playerViewingDistance = new AtomicInteger(VIEW_DISTANCE);
34
35 /** The amount of tiles out this mob can see npcs **/
36 private final AtomicInteger npcViewingDistance = new AtomicInteger(VIEW_DISTANCE);
37
38 /**
39 * The player that this viewport belongs to.
40 */
41 private final Player player;
42
44 this.player = player;
45 }
46
47 /**
48 * Adds a {@link Mob} to this viewport.
49 *
50 * {@code true} If the {@link Mob} was added, otherwise {@code false}.
51 */
52 public boolean add(Mob other) {
53 if (!canAdd(other)) {
54 return false;
55 }
56
57 if (other.isPlayer()) {
58 playersInViewport.add(other.getPlayer());
60 } else if (other.isNpc()) {
61 npcsInViewport.add(other.getNpc());
62 other.updateFlags.add(UpdateFlag.FACE_COORDINATE); // doing this for players for some reason crashes the players client
63 }
64 return true;
65 }
66
67 /**
68 * Determines if a {@code Mob} can be added to this viewport.
69 *
70 * {@code true} If this {@link Mob} can be added, otherwise {@code false}.
71 */
72 private boolean canAdd(Mob other) {
73 if (shouldRemove(other)) {
74 return false;
75 }
76
77 if (other.isPlayer()) {
78 Player player = other.getPlayer();
79 return !playersInViewport.contains(player);
80 } else if (other.isNpc()) {
81 Npc npc = other.getNpc();
82 return !npcsInViewport.contains(npc);
83 }
84
85 return false;
86 }
87
88 /**
89 * Determines if a {@link Mob} should be removed from this viewport.
90 *
91 * {@code true} If the {@link Mob} should be removed, otherwise {@code false}.
92 */
93 public boolean shouldRemove(Mob other) {
94 boolean sameEntity = player == other;
95 boolean notValid = !other.isValid();
96 boolean notVisible = !other.isVisible();
97 boolean notSameInstance = player.instance != other.instance;
98 boolean notInDistance = !player.getPosition().isWithinDistance(other.getPosition().copy(), other.isPlayer() ? player.viewport.getPlayerViewingDistance() : player.viewport.getNpcViewingDistance());
99 boolean positionChanged = other.positionChange || other.teleportRegion;
100 boolean regionChanged = other.regionChange && other.teleporting;
101 return sameEntity || notValid || notVisible || notSameInstance || notInDistance || regionChanged || positionChanged;
102 }
103
104 /**
105 * Determines how many tiles out a player can see players or npcs.
106 */
108 if (playersInViewport.size() >= Viewport.CAPACITY) {
109 if (playerViewingDistance.decrementAndGet() < 1) {
111 }
112 } else {
113 if (playerViewingDistance.incrementAndGet() > VIEW_DISTANCE) {
115 }
116 }
117
118 if (npcsInViewport.size() >= Viewport.CAPACITY) {
119 if (npcViewingDistance.decrementAndGet() < 1) {
120 npcViewingDistance.set(1);
121 }
122 } else {
123 if (npcViewingDistance.incrementAndGet() > VIEW_DISTANCE) {
125 }
126 }
127 }
128
129 /**
130 * The collection of players in this viewport
131 */
132 public List<Player> getPlayersInViewport() {
133 return playersInViewport;
134 }
135
136 /**
137 * The collection of npcs in this viewport
138 */
139 public List<Npc> getNpcsInViewport() {
140 return npcsInViewport;
141 }
142
143 /**
144 * How many tiles out this player can see other players
145 */
147 return playerViewingDistance.get();
148 }
149
150 /**
151 * This is how many tiles out this player can see npcs
152 */
154 return npcViewingDistance.get();
155 }
156
157}
boolean isValid()
Validates this npc based on its current region and registered state.
Definition Entity.java:154
Handles the mob class.
Definition Mob.java:66
final EnumSet< UpdateFlag > updateFlags
Definition Mob.java:94
final boolean isNpc()
Check if an entity is an npc.
Definition Mob.java:550
final boolean isPlayer()
Check if an entity is a player.
Definition Mob.java:564
boolean add(Mob other)
Adds a Mob to this viewport.
Definition Viewport.java:52
int getNpcViewingDistance()
This is how many tiles out this player can see npcs.
List< Npc > getNpcsInViewport()
The collection of npcs in this viewport.
final List< Player > playersInViewport
The collection of players that are visible in this viewport.
Definition Viewport.java:27
final AtomicInteger playerViewingDistance
The amount of tiles out this mob can see players.
Definition Viewport.java:33
void calculateViewingDistance()
Determines how many tiles out a player can see players or npcs.
static final int VIEW_DISTANCE
The amount of tiles out an entity can see within this viewport.
Definition Viewport.java:24
final Player player
The player that this viewport belongs to.
Definition Viewport.java:41
final AtomicInteger npcViewingDistance
The amount of tiles out this mob can see npcs.
Definition Viewport.java:36
boolean shouldRemove(Mob other)
Determines if a Mob should be removed from this viewport.
Definition Viewport.java:93
final List< Npc > npcsInViewport
The collection of npcs that are visible in this viewport.
Definition Viewport.java:30
static final int ADD_THRESHOLD
The amount of entities that can be added to this viewport in a single tick.
Definition Viewport.java:18
int getPlayerViewingDistance()
How many tiles out this player can see other players.
static final int CAPACITY
The amount of entities that can be visible at all at once 255 players and 255 npcs.
Definition Viewport.java:21
List< Player > getPlayersInViewport()
The collection of players in this viewport.
boolean canAdd(Mob other)
Determines if a Mob can be added to this viewport.
Definition Viewport.java:72
Represents a non-player character in the in-game world.
Definition Npc.java:29
This class represents a character controlled by a player.
Definition Player.java:125
Position copy()
Creates a deep copy of this location.