-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle less than 1 regen and removing Regen logic #10
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
19e4e83
Add remainder for less than 1 regen
e-aakash bceb55c
Update regenSortedByTime map after iteration
e-aakash 65d447e
Add RemoveRegenEvent for handling removal of regen
e-aakash 877d4e3
Add static strings for regen event ids
e-aakash 6b98375
Update getRegenValue method of RegenComponent
e-aakash File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ | |
import org.terasology.logic.health.event.OnFullyHealedEvent; | ||
import org.terasology.protobuf.EntityData; | ||
import org.terasology.registry.In; | ||
import org.terasology.world.generation.BaseFacetedWorldGenerator; | ||
|
||
import java.util.Iterator; | ||
import java.util.LinkedList; | ||
|
@@ -43,6 +44,9 @@ | |
*/ | ||
@RegisterSystem(RegisterMode.AUTHORITY) | ||
public class RegenAuthoritySystem extends BaseComponentSystem implements UpdateSubscriberSystem { | ||
public static final String ALL_REGEN = "all"; | ||
public static final String BASE_REGEN = "baseRegen"; | ||
public static final String WAIT = "wait"; | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(RegenComponent.class); | ||
/** | ||
|
@@ -51,9 +55,9 @@ public class RegenAuthoritySystem extends BaseComponentSystem implements UpdateS | |
private static final int CHECK_INTERVAL = 200; | ||
|
||
/** | ||
* Integer storing when last time entities were regenerated. | ||
* Long storing when entities are to be regenerated again. | ||
*/ | ||
private static long lastTick = 0; | ||
private static long nextTick; | ||
|
||
// Stores when next to check for new value of regen, contains only entities which are being regenerated | ||
private SortedSetMultimap<Long, EntityRef> regenSortedByTime = TreeMultimap.create(Ordering.natural(), | ||
|
@@ -74,9 +78,9 @@ public class RegenAuthoritySystem extends BaseComponentSystem implements UpdateS | |
public void update(float delta) { | ||
final long currentTime = time.getGameTimeInMs(); | ||
// Execute regen schedule | ||
if (currentTime > lastTick + CHECK_INTERVAL) { | ||
if (currentTime > nextTick) { | ||
invokeRegenOperations(currentTime); | ||
lastTick = currentTime; | ||
nextTick = currentTime + CHECK_INTERVAL; | ||
} | ||
} | ||
|
||
|
@@ -116,7 +120,7 @@ private void regenerate(long currentTime) { | |
for (EntityRef entity : regenSortedByTime.values()) { | ||
RegenComponent regen = entity.getComponent(RegenComponent.class); | ||
HealthComponent health = entity.getComponent(HealthComponent.class); | ||
if (health.nextRegenTick < currentTime) { | ||
if (health != null && health.nextRegenTick < currentTime) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added null check because NPE is thrown when blocks are destroyed. |
||
logger.warn("regenerating " + regen.getRegenValue()); | ||
health.currentHealth += regen.getRegenValue(); | ||
health.nextRegenTick = currentTime + 1000; | ||
|
@@ -154,10 +158,10 @@ public void onRegenAddedWithoutComponent(ActivateRegenEvent event, EntityRef ent | |
private void addRegenToScheduler(ActivateRegenEvent event, EntityRef entity, RegenComponent regen, | ||
HealthComponent health) { | ||
logger.warn("Regen added " + event.id); | ||
if (event.id.equals("baseRegen")) { | ||
if (event.id.equals(BASE_REGEN)) { | ||
// setting endTime to MAX_VALUE because natural regen happens till entity fully regenerates | ||
regen.addRegen(event.id, health.regenRate, Long.MAX_VALUE); | ||
regen.addRegen("wait", -health.regenRate, | ||
regen.addRegen(BASE_REGEN, health.regenRate, Long.MAX_VALUE); | ||
regen.addRegen(WAIT, -health.regenRate, | ||
time.getGameTimeInMs() + (long) (health.waitBeforeRegen * 1000)); | ||
} else { | ||
regen.addRegen(event.id, event.value, time.getGameTimeInMs() + (long) (event.endTime * 1000)); | ||
|
@@ -169,12 +173,12 @@ private void addRegenToScheduler(ActivateRegenEvent event, EntityRef entity, Reg | |
public void onRegenRemoved(DeactivateRegenEvent event, EntityRef entity, HealthComponent health, | ||
RegenComponent regen) { | ||
regenSortedByTime.remove(regen.getLowestEndTime(), entity); | ||
if (event.id.equals("all")) { | ||
if (event.id.equals(ALL_REGEN)) { | ||
entity.removeComponent(RegenComponent.class); | ||
} else { | ||
regen.removeRegen(event.id); | ||
if (event.id.equals("baseRegen")) { | ||
regen.removeRegen("wait"); | ||
if (event.id.equals(BASE_REGEN)) { | ||
regen.removeRegen(WAIT); | ||
} | ||
if (!regen.isEmpty()) { | ||
regenSortedByTime.put(regen.getLowestEndTime(), entity); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Accidental import?