Back to project page DolphinOES.
The source code is released under:
Apache License
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.
/******************************************************************************* * Copyright 2014 See AUTHORS file./*from w w w . j a v a 2 s .c o m*/ * * 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.architecture; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; /** The logic that is to be attached to an {@link GameObject}. The behavior is * attached to the abstract object by passing the object in the constructor. * <p> * Behaviors can be customised using {@link #update()} and * {@link #updateDebug()} in a {@link GameBehavior} * implementation, and both inside and outside them using * {@link #setEventOnStartOfUpdate(AbstractEvent)} and * {@link #setEventOnEndOfUpdate(AbstractEvent)} which run before and after * {@link #update()}. * * @author Claudiu Bele */ public abstract class GameBehavior { // region fields /** The value used for sorting the priority . 0 by default . 1 would be * executed later. */ public int priorityLevel; /** Decides whether or not to call the update function */ public boolean enabled; /** Name of the behavior. Used for retrieving any behavior by name Used in * {@link GameObject#getBehavior(String)} */ public String name; /** The object that the behavior is attached to. Assigned in the constructor. */ public GameObject object; /** Events that can be assigned in * {@link #setEventOnEndOfUpdate(AbstractEvent)} and * {@link #setEventOnStartOfUpdate(AbstractEvent)} . The start behavior will * run before the actual behavior event, and the end behavior will run after * the actual behavior event. The events will run if {@link #enabled} is * true */ private AbstractEvent eventOnEndOfUpdate, eventOnStartOfUpdate; /** The scene that the object that runs the behavior exists and is part of */ public GameScene scene; // endregion // region constructors public GameBehavior(GameObject obj) { this.enabled = true; this.object = obj; obj.behaviors.add(this); obj.behaviorTable.put(getClass(), this); scene = obj.scene; Collections.sort(obj.behaviors, obj.behaviorsComparator); // registering the class for debugging, only first object to add a // specific behavior triggers this // for each behavior initialiseClass(); if(!DolphinOES.debug.behaviors.containsKey(getClass())) { DolphinOES.debug.behaviors.put(getClass(), new DebugHandler()); } } // endregion // region methods /** Sets the key bindings for a particular behavior for debugging. Should be * called in the contructor of a behavior if you want it to handle * debugging. It will not automatically on all behaviors without key * bindings. * * @param behavior * the class to be toggled on or off * @param keys * the keys that have to be pressed to trigger the toggling */ public static void setDebugKeys(Class<? extends GameBehavior> behavior, Integer[] keys) { if(DolphinOES.debug.behaviors.get(behavior).keysToActivate == null) { DolphinOES.debug.behaviors.get(behavior).keysToActivate = new ArrayList<Integer>(Arrays.asList(keys)); } } /** Method to be optionally overriden. It is called the first time a behavior * of a particular type is created. Called in * {@link #GameBehavior(GameObject)} */ protected void initialiseClass() { } /** Runs the behavior. Will be run from {@link GameObject#updateInternal()} * from the object's thread. * * @param info */ public final void updateInternal() { if (enabled && object.enabled()) { if (eventOnStartOfUpdate != null) eventOnStartOfUpdate.run(); update(); if (eventOnEndOfUpdate != null) eventOnEndOfUpdate.run(); } } /** Method that is customised in each individual behavior's implementation */ public abstract void update(); public AbstractEvent getEventOnEndOfUpdate() { return eventOnEndOfUpdate; } public void setEventOnEndOfUpdate(AbstractEvent eventOnEndOfUpdate) { this.eventOnEndOfUpdate = eventOnEndOfUpdate; } public AbstractEvent getEventOnStartOfUpdate() { return eventOnStartOfUpdate; } public void setEventOnStartOfUpdate(AbstractEvent eventOnStartOfUpdate) { this.eventOnStartOfUpdate = eventOnStartOfUpdate; } public void updateDebug() { } public void dispose() { } // endregion }