Android Open Source - android-jplay Game Object






From Project

Back to project page android-jplay.

License

The source code is released under:

Copyright (c) Nikolaj Baer All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. ...

If you think the Android project android-jplay 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

package com.nikolajbaer.game.objects;
/* w  ww  .  j  a  v  a2 s  .c  o  m*/
/* java */
import java.lang.Math;
import java.util.ArrayList;

/* jbox2d */
import org.jbox2d.dynamics.Body; 
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.common.Vec2;

/* local */
import com.nikolajbaer.game.Game;
import com.nikolajbaer.Util;
import com.nikolajbaer.render.Renderable;
import com.nikolajbaer.render.RenderObject;

// CONSIDER maybe this should be an interface?
public abstract class GameObject implements Renderable {
    protected Body m_body;
    protected RenderObject m_renderObject;
    protected float thruster;

    protected ArrayList<GameObjectEventListener> m_gameObjectEventListeners;
    protected int m_damage=0; // CONSIDER do i really need this here?

    public GameObject(Body b){
        m_body=b;
        thruster=0.0f;

        m_gameObjectEventListeners = new ArrayList<GameObjectEventListener>();
    }

    public Vec2 getDir(){
        return Util.rotate(new Vec2(0,-1),m_body.getAngle());
    }

    /* tick
     *
     * do whatever per step. return false if this game object is to be removed.
     */
    public boolean tick(){ return true; }

    // TODO shouldn't this kind of framework already be pre-packaged?
    public void addGameObjectEventListener(GameObjectEventListener l){
        m_gameObjectEventListeners.add(l);
    }

    public void removeGameObjectEventListener(GameObjectEventListener l){
        m_gameObjectEventListeners.remove(l);
    }

    protected void dispatchGameObjectCreatedEvent(GameObjectEvent e){
        for(int i=0;i<m_gameObjectEventListeners.size(); i++){
            m_gameObjectEventListeners.get(i).gameObjectCreated(e);
        }
    }  

    protected void dispatchGameObjectDestroyedEvent(GameObjectEvent e){
        for(int i=0;i<m_gameObjectEventListeners.size(); i++){
            m_gameObjectEventListeners.get(i).gameObjectDestroyed(e);
        }
    }  

    public Body getBody(){
        return m_body;
    }

    public boolean doesDamage(){
        return m_damage > 0;
    }

    public int getDamage(){
        return m_damage;
    }

    /* return true if it survives the damage done */
    public boolean applyDamage(int d){
        return true;
    }

    public boolean survivesImpact(){
        return true;
    }

    /* triggered when an object is being destroyed (e.g. make shrapnel or something) */
    public void doDestroy(){
    }

    // HACK made this public so the weapon can use the game object's emit..
    // CONSIDER should the weapon be allowed to emit its own objects? seems like something it would do
    public void emitGameObject(GameObject go){
        dispatchGameObjectCreatedEvent(new GameObjectEvent(this,go)); 
    }

    public void removeBody(){
        //System.out.println("I am removing body" + this);
        m_body=null;
    }

    public float[] getWorldTransform(){
        Vec2 p=m_body.getPosition();
        float[] v={p.x,p.y,m_body.getAngle()};
        return v;
    }

    public Vec2 getPosition(){
        return m_body.getPosition();
    }

    // CONSIDER return string array. 
    // then when rendering we can send relative position/angle of each component (e.g. tank turret)
    public String getRenderKey(){ return "default"; }

    public RenderObject getRenderObject(){
        return m_renderObject;
    }

    public void setRenderObject(RenderObject ro){
        m_renderObject=ro;
    }

    public void clearRenderObject(){
         m_renderObject=null;
    }
    
}




Java Source Code List

com.nikolajbaer.Util.java
com.nikolajbaer.androidrender.AndroidRenderObject.java
com.nikolajbaer.androidrender.DotRenderObject.java
com.nikolajbaer.androidrender.DrawableRenderObject.java
com.nikolajbaer.androidrender.JPlayActivity.java
com.nikolajbaer.androidrender.JPlayThread.java
com.nikolajbaer.androidrender.JPlayView.java
com.nikolajbaer.androidrender.PolygonRenderObject.java
com.nikolajbaer.awtrender.AWTRenderObject.java
com.nikolajbaer.awtrender.JPlay.java
com.nikolajbaer.awtrender.PNGRenderObject.java
com.nikolajbaer.awtrender.PolygonRenderObject.java
com.nikolajbaer.awtrender.RectRenderObject.java
com.nikolajbaer.game.Game.java
com.nikolajbaer.game.objects.BulletObject.java
com.nikolajbaer.game.objects.GameObjectEventListener.java
com.nikolajbaer.game.objects.GameObjectEvent.java
com.nikolajbaer.game.objects.GameObject.java
com.nikolajbaer.game.objects.ObstacleObject.java
com.nikolajbaer.game.objects.PlayerObject.java
com.nikolajbaer.game.objects.PolygonGameObject.java
com.nikolajbaer.game.objects.ShrapnelObject.java
com.nikolajbaer.game.players.GamePlayer.java
com.nikolajbaer.game.players.HunterPlayer.java
com.nikolajbaer.game.players.LambPlayer.java
com.nikolajbaer.game.players.LivePlayer.java
com.nikolajbaer.game.weapons.Blaster.java
com.nikolajbaer.game.weapons.TankCannon.java
com.nikolajbaer.game.weapons.Weapon.java
com.nikolajbaer.render.RenderObject.java
com.nikolajbaer.render.Renderable.java