From 6f433a239a85a71e587b9d6baf999bd2d3d64477 Mon Sep 17 00:00:00 2001 From: Martin Steiger Date: Tue, 28 Oct 2014 09:05:37 +0100 Subject: [PATCH] Add general world timer events --- .../terasology/world/time/OnDawnEvent.java | 28 ----- .../terasology/world/time/OnDuskEvent.java | 28 ----- .../terasology/world/time/OnMiddayEvent.java | 28 ----- .../world/time/OnMidnightEvent.java | 28 ----- .../org/terasology/world/time/WorldTime.java | 11 +- .../terasology/world/time/WorldTimeEvent.java | 104 +++++++++++++++++- .../terasology/world/time/WorldTimeImpl.java | 35 ++---- 7 files changed, 119 insertions(+), 143 deletions(-) delete mode 100644 engine/src/main/java/org/terasology/world/time/OnDawnEvent.java delete mode 100644 engine/src/main/java/org/terasology/world/time/OnDuskEvent.java delete mode 100644 engine/src/main/java/org/terasology/world/time/OnMiddayEvent.java delete mode 100644 engine/src/main/java/org/terasology/world/time/OnMidnightEvent.java diff --git a/engine/src/main/java/org/terasology/world/time/OnDawnEvent.java b/engine/src/main/java/org/terasology/world/time/OnDawnEvent.java deleted file mode 100644 index d6f22e929f3..00000000000 --- a/engine/src/main/java/org/terasology/world/time/OnDawnEvent.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 2013 MovingBlocks - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.terasology.world.time; - -/** - * Sent to a world on dawn (beginning of a new day) - * - * @author Immortius - */ -public class OnDawnEvent extends WorldTimeEvent { - - public OnDawnEvent(float worldTime, long worldTimeMS) { - super(worldTime, worldTimeMS); - } -} diff --git a/engine/src/main/java/org/terasology/world/time/OnDuskEvent.java b/engine/src/main/java/org/terasology/world/time/OnDuskEvent.java deleted file mode 100644 index bee38a043d0..00000000000 --- a/engine/src/main/java/org/terasology/world/time/OnDuskEvent.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 2013 MovingBlocks - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.terasology.world.time; - -/** - * Sent to the world on dusk (half way through the day, when the sun sets) - * - * @author Immortius - */ -public class OnDuskEvent extends WorldTimeEvent { - - public OnDuskEvent(float worldTime, long worldTimeMS) { - super(worldTime, worldTimeMS); - } -} diff --git a/engine/src/main/java/org/terasology/world/time/OnMiddayEvent.java b/engine/src/main/java/org/terasology/world/time/OnMiddayEvent.java deleted file mode 100644 index 34c5808b8ac..00000000000 --- a/engine/src/main/java/org/terasology/world/time/OnMiddayEvent.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 2013 MovingBlocks - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.terasology.world.time; - -/** - * Sent to a world in the middle of daytime - * - * @author Immortius - */ -public class OnMiddayEvent extends WorldTimeEvent { - - public OnMiddayEvent(float worldTime, long worldTimeMS) { - super(worldTime, worldTimeMS); - } -} diff --git a/engine/src/main/java/org/terasology/world/time/OnMidnightEvent.java b/engine/src/main/java/org/terasology/world/time/OnMidnightEvent.java deleted file mode 100644 index 87d247f9204..00000000000 --- a/engine/src/main/java/org/terasology/world/time/OnMidnightEvent.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 2013 MovingBlocks - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.terasology.world.time; - -/** - * Sent to a world at the middle of the night - * - * @author Immortius - */ -public class OnMidnightEvent extends WorldTimeEvent { - - public OnMidnightEvent(float worldTime, long worldTimeMS) { - super(worldTime, worldTimeMS); - } -} diff --git a/engine/src/main/java/org/terasology/world/time/WorldTime.java b/engine/src/main/java/org/terasology/world/time/WorldTime.java index a4d7d554ea3..1b0a318583c 100644 --- a/engine/src/main/java/org/terasology/world/time/WorldTime.java +++ b/engine/src/main/java/org/terasology/world/time/WorldTime.java @@ -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. diff --git a/engine/src/main/java/org/terasology/world/time/WorldTimeEvent.java b/engine/src/main/java/org/terasology/world/time/WorldTimeEvent.java index d7f35790ede..7e61b90f9bc 100644 --- a/engine/src/main/java/org/terasology/world/time/WorldTimeEvent.java +++ b/engine/src/main/java/org/terasology/world/time/WorldTimeEvent.java @@ -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"; } } diff --git a/engine/src/main/java/org/terasology/world/time/WorldTimeImpl.java b/engine/src/main/java/org/terasology/world/time/WorldTimeImpl.java index 497faca3537..abba8752435 100644 --- a/engine/src/main/java/org/terasology/world/time/WorldTimeImpl.java +++ b/engine/src/main/java/org/terasology/world/time/WorldTimeImpl.java @@ -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 @@ -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)); } } }