/** * This method will do the actual removing and adding of GameObjects at the end of a gameloop * pass. */ private void cleanupObjectlists() { Iterator<GameObject> it = items.iterator(); while (it.hasNext()) { GameObject go = it.next(); if (!go.isActive()) { deleteObjectAlarms(go); it.remove(); } } for (int i = 0; i < newItems.size(); i++) { // note: always moving the first element of newItems ensures same // order GameObject item = newItems.remove(0); if (item.getDepth() > 0) { float d = item.getDepth(); // move index to position of first element having smaller depth int index = 0; while (index < items.size()) { if (items.get(index).getDepth() < d) { break; } index++; } items.add(index, item); } else { // just add to the back of the list items.add(item); } } }
/** * Remove all GameObject instances of given class type. * * @param type De class type of the instances to be removed */ public <T> void deleteAllGameObjectsOfType(Class<T> type) { for (int i = 0; i < items.size(); i++) { GameObject go = items.get(i); if (go.getClass() == type) { go.clearActive(); } } }
/** * Use this function to get the angle between you and another object. For example: You can use * this function to check if you approaching another object from the left or right. Direction 0 * points up, directions go clockwise, so 90 is right, etc. * * @param object an instance of another object to calculate the angle for. * @return the angle of the object or 0 if the object is null. */ public final double getAngle(GameObject object) { double deltaX = object.getFullX() - getFullX(); double deltaY = object.getFullY() - getFullY(); if (deltaX >= 0 || deltaY >= 0) { return Math.toDegrees(Math.atan2(deltaX, deltaX)) + 90; } else { return Math.toDegrees((Math.atan2(deltaY, deltaX))) + 450; } }
/** * This method is like a 'postponed constructor'. It is called automatically by the GameEngine * when all necessary Android resources are ready. At that point you can set up your game.<br> * Don't call this method yourself (or everything will be done twice!)<br> * You can perform any initialization the game needs to before starting to run, like: * * <p> * * <ul> * <li>Create instances of GameObjects. * <li>Create a tile environment * <li>Set viewport options * <li>Configure background image, background color, OnScreenButtons, TouchInput * <li>etc. * </ul> * * <p>Override this method inside your game class that extends GameEngine. Call super.initialize() * at the very start. */ protected void initialize() { printDebugInfo("GameEngine", "Intializing..."); if (Sprite.loadDelayedSprites != null) { for (Sprite sprite : Sprite.loadDelayedSprites) { sprite.initialize(); } } Sprite.loadDelayedSprites = null; for (GameObject item : items) { item.intializeGameObject(); } }
/** Calculates if a GameObject is outside the world or not. */ private void calculateOutsideWorld(GameObject go) { if (go instanceof MoveableGameObject) { if (!go.isActive()) { return; } if (go.getX() - go.getFrameWidth() < 0 || go.getX() > mapWidth || go.getY() - go.getFrameHeight() < 0 || go.getY() > mapHeight) { ((MoveableGameObject) go).outsideWorld(); } } }
/** * Add a GameObject to the game. New GameObjects will become active in the next pass of the * gameloop. <br> * Layerposition may not work very well as yet, still under construction.<br> * * @param gameObject The GameObject that will be added to the game. Should have either GameObject * or MovableGameObject as it's parent. * @param layerposition The layerposition when this object is drawed. <b>Between 0 and 1 (float). * </b> 1 front, 0 back */ public final void addGameObject(GameObject gameObject, float layerposition) { gameObject.setDepth(layerposition); newItems.add(gameObject); }
/** * Add a GameObject to the game. New GameObjects will become active in the next pass of the * gameloop. <br> * Layerposition may not work very well as yet, still under construction.<br> * * @param gameObject The GameObject that will be added to the game. Should have either GameObject * or MovableGameObject as it's parent. * @param x The X spawnlocation when this object is created * @param y The Y spawnlocation when this object is created * @param layerposition The layerposition when this object is drawed. <b>Between 0 and 1 (float). * </b> 1 front, 0 back */ public final void addGameObject(GameObject gameObject, int x, int y, float layerposition) { gameObject.setStartPosition(x, y); gameObject.jumpToStartPosition(); gameObject.setDepth(layerposition); newItems.add(gameObject); }
/** * Add a GameObject to the game. New GameObjects will become active in the next pass of the * gameloop.<br> * * @param gameObject The GameObject that will be added to the game. Should have either GameObject * or MovableGameObject as it's parent. * @param x The X spawnlocation when this object is created * @param y The Y spawnlocation when this object is created */ public final void addGameObject(GameObject gameObject, int x, int y) { gameObject.setStartPosition(x, y); gameObject.jumpToStartPosition(); newItems.add(gameObject); }
/** * * Delete a GameObject from the game. * * @param gameObject The GameObject instance to be removed */ public final void deleteGameObject(GameObject gameObject) { gameObject.clearActive(); }