If you think the Android project DolphinOES listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*******************************************************************************
* Copyright 2014 See AUTHORS file./*www.java2s.com*/
*
* 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 com.sidereal.dolphinoes.behaviors.events;
import java.util.ArrayList;
import com.sidereal.dolphinoes.architecture.AbstractEvent;
import com.sidereal.dolphinoes.architecture.DolphinOES;
import com.sidereal.dolphinoes.architecture.GameBehavior;
import com.sidereal.dolphinoes.architecture.GameObject;
/** Schedule {@link AbstractEvent} objects to run repeatedly over a number of
* seconds or times.
*
* @author Claudiu Bele */publicclass RecurringEvent extends GameBehavior
{
// region fields
private ArrayList<AbstractEvent> events;
protectedfloat overallTimer = 0, frequencyTimer = 0;
// endregion fields
// region constructors
public RecurringEvent(GameObject obj)
{
super(obj);
events = new ArrayList<AbstractEvent>();
}
// endregion costructors
// region methods
@Override
publicvoid update()
{
for (int i = 0; i < events.size(); i++)
{
events.get(i).run();
}
}
/** Adds an event that will be ran at a certain frequency, until the end time
* ends.
*
* @param event
* An event that will run at a certain frequency
* @param frequency
* How often (in seconds) the action will happen
* @param endTime
* The seconds it will last, if set to -1, it will be permanent. */publicvoid setEvent(final AbstractEvent event, finalfloat frequency,
finalfloat endTime, final AbstractEvent endEvent)
{
events.add(new AbstractEvent()
{
privatefloat freq = frequency, end = endTime, freqTimer = 0,
endTimer = 0;
private AbstractEvent eventToRun = event;
@Override
publicvoid run(Object... params)
{
freqTimer += DolphinOES.time.getDeltaTime();
endTimer += DolphinOES.time.getDeltaTime();
// if end is -1, we want it to run all the time
if (end != -1 && endTimer - end >= 0)
{
if (endEvent != null)
{
endEvent.run();
}
events.remove(events.indexOf(this));
}
if (freqTimer > freq)
{
eventToRun.run();
freqTimer -= freq;
}
}
});
}
publicvoid setEvent(final AbstractEvent event, finalfloat frequency,
finalint times, final AbstractEvent endEvent)
{
events.add(new AbstractEvent()
{
privatefloat freq = frequency, end = times * frequency + 0.05f,
freqTimer = 0, endTimer = 0;
private AbstractEvent eventToRun = event;
@Override
publicvoid run(Object... params)
{
freqTimer += DolphinOES.time.getDeltaTime();
endTimer += DolphinOES.time.getDeltaTime();
// if end is -1, we want it to run all the time
if (end != -1 && endTimer - end >= 0 && times != -1)
{
if (endEvent != null)
{
endEvent.run();
}
events.remove(events.indexOf(this));
}
if (freqTimer > freq)
{
eventToRun.run();
freqTimer -= freq;
}
}
});
}
public ArrayList<AbstractEvent> getEvents()
{
return events;
}
publicvoid setEvents(ArrayList<AbstractEvent> events)
{
this.events = events;
}
// endregion methods
}