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 ww w .ja va 2 s. co 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.behaviors.triggers; import java.util.ArrayList; import java.util.HashMap; import java.util.Hashtable; import java.util.Map.Entry; 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.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 collision between objects, being able to designate what * {@link GameObject#type} the objects have to have and an {@link AbstractEvent} * to run for colliding with individual types. Multiple types can be handled and * the behavior will ignore collision with himself. * * @author Claudiu Bele */ public class Collider extends GameBehavior { public class ColliderInfo { public Rectangle rect; public float offsetX, offsetY; public String name; public ColliderInfo(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.getY() + offsetY; } } // region fields private ArrayList<ColliderInfo> colliders; private HashMap<String, ArrayList<ColliderInfo>> colliderMap; private Hashtable<String, AbstractEvent> events; private GameObject objectToFocus; private static Sprite debugSpriteSource; private Sprite debugSprite; private Collider enemyCollider; // endregion fields // region constructors public Collider(GameObject obj) { super(obj); this.events = new Hashtable<String, AbstractEvent>(); if (DolphinOES.debug.isEnabled()) this.debugSprite = new Sprite(debugSpriteSource); this.colliders = new ArrayList<Collider.ColliderInfo>(); this.colliderMap = new HashMap<String, ArrayList<ColliderInfo>>(); this.objectToFocus = this.object; GameBehavior.setDebugKeys(Collider.class, new Integer[] { Keys.SHIFT_LEFT, Keys.C }); } // endregion constructors // region methods @Override public void update() { for (int i = 0; i < colliders.size(); i++) { colliders.get(i).rect.x = objectToFocus.pos.getX() + colliders.get(i).offsetX; colliders.get(i).rect.y = objectToFocus.pos.getY() + colliders.get(i).offsetY; } // 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()) { if (entry.getValue().equals(object)) continue; // check that the objects have colliders if (entry.getValue().getBehavior(Collider.class) != null) { enemyCollider = entry.getValue() .getBehavior(Collider.class); if (Utility.doCollidersIntersect(this, enemyCollider)) { events.get(objectEntry.getKey()).run(entry.getValue(), object); } } } } } public void forceUpdate() { for (int i = 0; i < colliders.size(); i++) { colliders.get(i).rect.x = objectToFocus.pos.getX() + colliders.get(i).offsetX; colliders.get(i).rect.y = objectToFocus.pos.getY() + colliders.get(i).offsetY; } } @Override protected void initialiseClass() { if (!DolphinOES.debug.isEnabled()) return; debugSpriteSource = new Sprite( DolphinOES.assets.get( DolphinOES.assets.frameworkAssetsFolder + "White.png", Texture.class)); debugSpriteSource.setColor(new Color(0, 1, 0, 0.5f)); super.initialiseClass(); } @Override public void updateDebug() { for (int i = 0; i < colliders.size(); i++) { debugSprite.setBounds(colliders.get(i).rect.x, colliders.get(i).rect.y, colliders.get(i).rect.width, colliders.get(i).rect.height); debugSprite.draw(object.gameBatch.spriteBatch); } } public void setEvent(String key, AbstractEvent event) { events.put(key, event); } public void setEvent(String[] keys, AbstractEvent event) { for (int i = 0; i < keys.length; i++) { events.put(keys[i], event); } } // region collide area add public void addCollideArea(float width, float height, float offsetX, float offsetY) { addCollideArea(width, height, offsetX, offsetY, "Default"); } public void addCollideArea(float width, float height, float offsetX, float offsetY, String name) { this.colliders.add(new ColliderInfo(new Rectangle(object.pos.getX() - offsetX, object.pos.getY() - offsetY, width, height), offsetX, offsetY, name)); } public void addCollideArea(float width, float height) { addCollideArea(width, height, -width / 2f, -height / 2f, "Default"); } public void addCollideArea(float width, float height, String name) { addCollideArea(width, height, -width / 2f, -height / 2f, name); } public void addCollideArea(float size) { addCollideArea(size, "Default"); } public void addCollideArea(float size, String name) { addCollideArea(size, size, name); } // endregion public ArrayList<Integer> getRectanglesThatCollide(Rectangle rect) { ArrayList<Integer> toReturn = new ArrayList<Integer>(); for (int i = 0; i < colliders.size(); i++) { if (colliders.get(i).rect.overlaps(rect)) { toReturn.add(i); } } return toReturn; } public ArrayList<String> getRectangleNamesThatCollide(Rectangle rect) { ArrayList<String> toReturn = new ArrayList<String>(); toReturn.clear(); for (int i = 0; i < colliders.size(); i++) { if (colliders.get(i).rect.overlaps(rect)) { toReturn.add(colliders.get(i).name); } } return toReturn; } public Rectangle getRectangleThatCollides(Rectangle rect) { for (int i = 0; i < colliders.size(); i++) { if (Utility.rectanglesIntersect(colliders.get(i).rect, rect)) return (colliders.get(i).rect); } return null; } public Hashtable<String, AbstractEvent> getEvents() { return events; } public void setEvents(Hashtable<String, AbstractEvent> events) { this.events = events; } public ArrayList<ColliderInfo> getColliders() { return colliders; } public void setColliders(ArrayList<ColliderInfo> colliders) { this.colliders = colliders; } public void removeAreas() { this.colliders.clear(); } public void removeEvents() { this.events.clear(); } public void removeArea(String name) { boolean found = false; for (int i = 0; i < colliders.size(); i++) { if (colliders.get(i).name.equals(name)) { colliders.remove(i); found = true; break; } } if (found) { colliderMap.remove(name); } } public GameObject getObjectToFocus() { return objectToFocus; } public void setObjectToFocus(GameObject objectToFocus) { this.objectToFocus = objectToFocus; } // endregion methods }