Skip to content

Commit

Permalink
Add general world timer events
Browse files Browse the repository at this point in the history
  • Loading branch information
msteiger committed Oct 28, 2014
1 parent c44e230 commit 6f433a2
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 143 deletions.
28 changes: 0 additions & 28 deletions engine/src/main/java/org/terasology/world/time/OnDawnEvent.java

This file was deleted.

28 changes: 0 additions & 28 deletions engine/src/main/java/org/terasology/world/time/OnDuskEvent.java

This file was deleted.

28 changes: 0 additions & 28 deletions engine/src/main/java/org/terasology/world/time/OnMiddayEvent.java

This file was deleted.

This file was deleted.

11 changes: 10 additions & 1 deletion engine/src/main/java/org/terasology/world/time/WorldTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@
*/
public interface WorldTime extends ComponentSystem {

int DAY_LENGTH = 1000 * 60 * 60 * 24;
long DAY_LENGTH = 1000 * 60 * 60 * 24;

long DAYS_TO_MS = (DAY_LENGTH);
float MS_TO_DAYS = 1.f / (DAYS_TO_MS);

long DAWN_TIME = 0;
long MIDDAY_TIME = DAY_LENGTH / 4;
long DUSK_TIME = DAY_LENGTH / 2;
long MIDNIGHT_TIME = 3 * DAY_LENGTH / 4;


/**
* @return World time in milliseconds.
Expand Down
104 changes: 100 additions & 4 deletions engine/src/main/java/org/terasology/world/time/WorldTimeEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,119 @@

import org.terasology.entitySystem.event.Event;

import com.google.common.math.LongMath;

/**
* A timer event that represents a (world-based) time instant
* @author Immortius
* @author Martin Steiger
*/
public class WorldTimeEvent implements Event {
private float worldTime;
private long worldTimeMS;
private long timeInDay;

public WorldTimeEvent(float worldTime, long worldTimeMS) {
public WorldTimeEvent(long worldTimeMS) {
this.worldTimeMS = worldTimeMS;
this.worldTime = worldTime;
this.timeInDay = LongMath.mod(worldTimeMS, WorldTime.DAY_LENGTH);
}

/**
* @return the world time in milli secs
*/
public long getWorldTimeMS() {
return worldTimeMS;
}

/**
* @return the world time in days
*/
public float getWorldTime() {
return worldTime;
return WorldTime.MS_TO_DAYS * worldTimeMS;
}

/**
* @return true if at dawn/sunrise (true exactly once per day)
*/
public boolean isDawn() {
return timeInDay == WorldTime.DAWN_TIME;
}

/**
* @return true if at midday/noon (true exactly once per day)
*/
public boolean isMidday() {
return timeInDay == WorldTime.MIDDAY_TIME;
}

/**
* @return true if at dusk/sunset (true exactly once per day)
*/
public boolean isDusk() {
return timeInDay == WorldTime.DUSK_TIME;
}

/**
* @return true if after DAWN and before MIDDAY
*/
public boolean isMorning() {
return timeInDay > WorldTime.DAWN_TIME && timeInDay < WorldTime.MIDDAY_TIME;
}

/**
* @return true if after MIDDAY and before DUSK
*/
public boolean isAfternoon() {
return timeInDay > WorldTime.MIDDAY_TIME && timeInDay < WorldTime.DUSK_TIME;
}

/**
* @return true if after DUSK and before DAWN
*/
public boolean isNight() {
return timeInDay > WorldTime.DUSK_TIME || timeInDay < WorldTime.DAWN_TIME;
}

/**
* @return true if at midnight (true exactly once per day)
*/
public boolean isMidnight() {
return timeInDay == WorldTime.MIDNIGHT_TIME;
}

@Override
public String toString() {
return String.format("WorldTimeEvent [%s ms -> %.2f days (%s)]", worldTimeMS, getWorldTime(), getDayTimeText());
}

private String getDayTimeText() {
if (isDusk()) {
return "dusk";
}

if (isMorning()) {
return "morning";
}

if (isMidday()) {
return "midday";
}

if (isAfternoon()) {
return "afternoon";
}

if (isDawn()) {
return "dawn";
}

if (isNight()) {
return "night";
}

if (isMidnight()) {
return "mignight";
}

return "UNDEFINED";
}
}
35 changes: 9 additions & 26 deletions engine/src/main/java/org/terasology/world/time/WorldTimeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,11 @@
* @author Immortius
*/
public class WorldTimeImpl extends BaseComponentSystem implements WorldTime, UpdateSubscriberSystem {
public static final long DAYS_TO_MS = (DAY_LENGTH);
public static final float MS_TO_DAYS = 1.f / (DAYS_TO_MS);

public static final long DAWN_TIME = DAY_LENGTH;
public static final long MIDDAY_TIME = DAY_LENGTH / 4;
public static final long DUSK_TIME = DAY_LENGTH / 2;
public static final long MIDNIGHT_TIME = 3 * DAY_LENGTH / 4;
private static final float WORLD_TIME_MULTIPLIER = 48f;

private static final long TICK_RATE = DAYS_TO_MS / 100;

private AtomicLong worldTime = new AtomicLong(0);

@In
Expand Down Expand Up @@ -84,26 +80,13 @@ public void update(float delta) {
deltaMs = (long) (deltaMs * WORLD_TIME_MULTIPLIER);
long startTime = worldTime.getAndAdd(deltaMs);
long endTime = startTime + deltaMs;
long timeInDay = startTime % DAY_LENGTH;
if (timeInDay < 0) {
timeInDay = DAY_LENGTH + timeInDay;
}
if (timeInDay < MIDDAY_TIME) {
if (timeInDay + deltaMs >= MIDDAY_TIME) {
getWorldEntity().send(new OnMiddayEvent(MS_TO_DAYS * endTime, endTime));
}
} else if (timeInDay < DUSK_TIME) {
if (timeInDay + deltaMs >= DUSK_TIME) {
getWorldEntity().send(new OnDuskEvent(MS_TO_DAYS * endTime, endTime));
}
} else if (timeInDay < MIDNIGHT_TIME) {
if (timeInDay + deltaMs >= MIDNIGHT_TIME) {
getWorldEntity().send(new OnMidnightEvent(MS_TO_DAYS * endTime, endTime));
}
} else if (timeInDay < DAWN_TIME) {
if (timeInDay + deltaMs >= DAWN_TIME) {
getWorldEntity().send(new OnDawnEvent(MS_TO_DAYS * endTime, endTime));
}

long startTick = startTime / TICK_RATE;
long endTick = (endTime) / TICK_RATE;

if (startTick != endTick) {
long tick = endTime - endTime % TICK_RATE;
getWorldEntity().send(new WorldTimeEvent(tick));
}
}
}
Expand Down

0 comments on commit 6f433a2

Please sign in to comment.