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.// w ww . jav a2s. c om * * 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.triggers; import java.util.ArrayList; import java.util.HashMap; import java.util.Hashtable; import java.util.Map.Entry; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input.Keys; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector3; import com.sidereal.dolphinoes.architecture.AbstractEvent; import com.sidereal.dolphinoes.architecture.DolphinOES; import com.sidereal.dolphinoes.architecture.GameBehavior; import com.sidereal.dolphinoes.architecture.GameObject; import com.sidereal.dolphinoes.util.Utility; /** Handles the collision between 2 activatable areas and pressing a particular * key. * * @author Claudiu Bele */ public class Activatable extends GameBehavior { // region fields private ArrayList<ActivationArea> activationAreas; private HashMap<String, ArrayList<ActivationArea>> ActivatorsMap; private Hashtable<String, AbstractEvent> events; private GameObject objectToFocus; public static Sprite debugSpriteSource; private Sprite debugSprite; public int key; /** seconds before you can activate stuff again. */ protected float defaultToggleDelay = 0.2f; protected float toggleDelay = 0f; private Activatable enemyActivator; // endregion fields // region constructors @Override protected void initialiseClass() { debugSpriteSource = new Sprite( DolphinOES.assets.get( DolphinOES.assets.frameworkAssetsFolder + "White.png", Texture.class)); debugSpriteSource.setColor(new Color(0, 0, 1, 0.5f)); } public Activatable(GameObject obj) { super(obj); this.key = Keys.F; this.events = new Hashtable<String, AbstractEvent>(); if (DolphinOES.debug.isEnabled()) this.debugSprite = new Sprite(debugSpriteSource); this.activationAreas = new ArrayList<Activatable.ActivationArea>(); this.ActivatorsMap = new HashMap<String, ArrayList<ActivationArea>>(); this.objectToFocus = this.object; GameBehavior.setDebugKeys(getClass(), new Integer[] { Keys.CONTROL_LEFT, Keys.C }); } // endregion constructors // region methods @Override public void update() { for (int i = 0; i < activationAreas.size(); i++) { activationAreas.get(i).rect.x = objectToFocus.pos.getX() + activationAreas.get(i).offsetX; activationAreas.get(i).rect.y = objectToFocus.pos.getY() + activationAreas.get(i).offsetY; } // decrease the time remaining before we can press and handle pressing // again if (toggleDelay > 0) { toggleDelay -= DolphinOES.time.getDeltaTime(); if (toggleDelay > 0) return; } if (!Gdx.input.isKeyPressed(key)) return; if (!object.enabled()) return; // getting all object types, organised in hashmaps of names and Objects for (Entry<String, HashMap<String, GameObject>> objectEntry : object.scene.objectsMap .entrySet()) { // our events don't contain the type as a key, so we don't react to // them. if (!events.containsKey(objectEntry.getKey())) { continue; } // getting all objects of a certain type for (Entry<String, GameObject> entry : objectEntry.getValue() .entrySet()) { // check that the objects have Activatorss if (entry.getValue().getBehavior(Activatable.class) != null) { enemyActivator = entry.getValue().getBehavior( Activatable.class); if (overlaps(enemyActivator)) { toggleDelay = defaultToggleDelay; events.get(objectEntry.getKey()).run(entry.getValue(), object); } } } } } @Override public void updateDebug() { for (int i = 0; i < activationAreas.size(); i++) { debugSprite.setBounds(activationAreas.get(i).rect.x, activationAreas.get(i).rect.y, activationAreas.get(i).rect.width, activationAreas.get(i).rect.height); debugSprite.draw(object.gameBatch.spriteBatch); } } public void setEvent(String key, AbstractEvent event) { events.put(key, event); } public void setEvents(String[] keys, AbstractEvent event) { for (int i = 0; i < keys.length; i++) { events.put(keys[i], event); } } // region activation areas // region adding public void addActivationArea(float width, float height, float offsetX, float offsetY, String name) { float posX = object.pos.getX() + offsetX; float posY = object.pos.getX() + offsetY; this.activationAreas.add(new ActivationArea(new Rectangle(posX-offsetX, posY-offsetY, width, height), offsetX, offsetY, name)); if (!this.ActivatorsMap.containsKey(name)) { this.ActivatorsMap.put(name, new ArrayList<ActivationArea>()); } this.ActivatorsMap.get(name).add(this.activationAreas.get(activationAreas.size()-1)); } public void addActivationArea(float width, float height, String name) { addActivationArea(width, height, -width / 2f, -height / 2f, name); } public void addActivationArea(float size, Vector3 centerPoint) { addActivationArea(size, size, "Default"); } // endregion // region activators public ArrayList<ActivationArea> getActivators() { return activationAreas; } public void setActivators(ArrayList<ActivationArea> activators) { this.activationAreas = activators; } public void eraseAreas() { this.activationAreas.clear(); } // endregion public class ActivationArea { public Rectangle rect; public float offsetX, offsetY; public String name; public ActivationArea(Rectangle rect, float offsetX, float offsetY, String name) { this.rect = rect; this.offsetX = offsetX; this.offsetY = offsetY; this.name = name; rect.x = objectToFocus.pos.getX() + offsetX; rect.y = objectToFocus.pos.getX() + offsetY; } } // endregion // region activation detection public ArrayList<String> getRectangleNamesThatActivate(Rectangle rect) { ArrayList<String> toReturn = new ArrayList<String>(); for (int i = 0; i < activationAreas.size(); i++) { if (Utility.rectanglesIntersect(activationAreas.get(i).rect, rect)) { toReturn.add(activationAreas.get(i).name); } } return toReturn; } public Rectangle getRectangleThatActivate(Rectangle rect) { for (int i = 0; i < activationAreas.size(); i++) { if (Utility.rectanglesIntersect(activationAreas.get(i).rect, rect)) return (activationAreas.get(i).rect); } return null; } // endregion // region events public Hashtable<String, AbstractEvent> getEvents() { return events; } public void setEvents(Hashtable<String, AbstractEvent> events) { this.events = events; } // endregion public void eraseEvents() { this.events.clear(); } public GameObject getObjectToFocus() { return objectToFocus; } public void setObjectToFocus(GameObject objectToFocus) { this.objectToFocus = objectToFocus; } private boolean overlaps(Activatable activatable) { for (int i = 0; i < getActivators().size(); i++) { for (int j = 0; j < activatable.getActivators().size(); j++) { if (getActivators().get(i).rect.overlaps(activatable .getActivators().get(j).rect)) { return true; } } } return false; } // endregion methods }