Skip to content

Commit

Permalink
Added semantic timer events again and separate event types
Browse files Browse the repository at this point in the history
  • Loading branch information
msteiger committed Oct 28, 2014
1 parent 6f433a2 commit 2478aa4
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 114 deletions.
28 changes: 28 additions & 0 deletions engine/src/main/java/org/terasology/world/time/OnDawnEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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 TimeEventBase {

public OnDawnEvent(long worldTimeMS) {
super(worldTimeMS);
}
}
28 changes: 28 additions & 0 deletions engine/src/main/java/org/terasology/world/time/OnDuskEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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 TimeEventBase {

public OnDuskEvent(long worldTimeMS) {
super(worldTimeMS);
}
}
28 changes: 28 additions & 0 deletions engine/src/main/java/org/terasology/world/time/OnMiddayEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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 TimeEventBase {

public OnMiddayEvent(long worldTimeMS) {
super(worldTimeMS);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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 TimeEventBase {

public OnMidnightEvent(long worldTimeMS) {
super(worldTimeMS);
}
}
64 changes: 64 additions & 0 deletions engine/src/main/java/org/terasology/world/time/TimeEventBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2014 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;

import org.terasology.entitySystem.event.Event;

import com.google.common.math.LongMath;

/**
* A base class for different timer events
* @author Martin Steiger
*/
public abstract class TimeEventBase implements Event {

private long worldTimeMS;
private long timeInDay;

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

/**
* @return the time of day as a fraction
*/
public float getDayTime() {
return timeInDay / (float) WorldTime.DAY_LENGTH;
}

/**
* @return the time of day in milli secs
*/
public long getDayTimeInMs() {
return timeInDay;
}

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

/**
* @return the world time in milli secs
*/
public long getWorldTimeInMs() {
return worldTimeMS;
}
}
12 changes: 4 additions & 8 deletions engine/src/main/java/org/terasology/world/time/WorldTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,10 @@ public interface WorldTime extends ComponentSystem {

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;

/**
* The number of timer tick events per day
*/
long TICKS_PER_DAY = 100;

/**
* @return World time in milliseconds.
Expand Down
108 changes: 5 additions & 103 deletions engine/src/main/java/org/terasology/world/time/WorldTimeEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,121 +15,23 @@
*/
package org.terasology.world.time;

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 long worldTimeMS;
private long timeInDay;
public class WorldTimeEvent extends TimeEventBase {

public WorldTimeEvent(long worldTimeMS) {
this.worldTimeMS = worldTimeMS;
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.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;
super(worldTimeMS);
}

/**
* @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;
public boolean matchesDaily(float fraction) {
return Math.abs(getDayTime() - fraction) < 0.5f / WorldTime.TICKS_PER_DAY;
}

@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";
return String.format("WorldTimeEvent [%s ms -> %.2f days]", getWorldTimeInMs(), getWorldTime());
}
}
Loading

0 comments on commit 2478aa4

Please sign in to comment.