⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 entitymanager.java

📁 M3DEA (Mobile 3D Engine API) 手机3D引擎API
💻 JAVA
字号:
/*
 *M3DEA - MOBILE 3D ENGINE API
 *Copyright (C) 2006 Alexandre Watanabe         alexandre_sw@yahoo.com.br     
 *		     Diego de Freitas           zenon_cc@yahoo.com.br
 *		     Paulo Rodrigo Priszculnik  paulorp@paulorp.trix.net
 *		     Rodrigo Arthur Lopes       raspl@terra.com.br
 *
 * This library is free software; you can redistribute it 
 * and/or modify it under the terms of the GNU Lesser General
 * Public License as published by the Free Software
 * Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 *
 * This library is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE. See the GNU Lesser General Public License for
 * more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to
 * the Free Software Foundation, Inc., 59 Temple Place, Suite
 * 330, Boston, MA 02111-1307 USA
 *
 */

package m3dea.entities;

import java.util.*;
import javax.microedition.m3g.*;
import m3dea.io.KeyManager;
import m3dea.physics.*;

/**
 * Classe que controla todas entidades da cena e realiza opera珲es sobre elas, como checagem de colis鉶 e aplica玢o de gravidade
 */
public class EntityManager
{
    private static EntityManager entitymanager;
    
    private static KeyManager keyManager = KeyManager.getInstance();
    
    // Raiz dos objetos da Cena
    private World world = new World();
    
    private Vector entities = new Vector();
    private Player player = null;
    
    
    private EntityVisitor collision = new CheckCollisionVisitor();
    private EntityVisitor gravity = new GravityVisitor(0.5f);

    private Camera camera = new Camera();
    
    private EntityManager()
    {
    }
    
    /**
     * Adiciona uma entidade na cena
     * @param e Entidade a ser adicionada
     */
    public void addEntity(Entity e)
    {        
        if( e instanceof Player )
            addPlayer((Player)e);

        entities.addElement(e);
    }
    
    /**
     * Remove entidade da cena
     * @param entity Entidade a ser removida
     */
     public void removeEntity(Entity entity)
    {
         entities.removeElement(entity);
         world.removeChild(entity.getMesh());
         entity.getMesh().setRenderingEnable(false);
    }
     
    /**
     * Remove todas as entidades da cena
     */
     public void removeAll()
    {
         Enumeration en = entities.elements();
         while( en.hasMoreElements() )
             removeEntity( (Entity) en.nextElement() );
    }
    
    /* Adiciona o player corrente */
    private void addPlayer(Player p)
    {
        if(player != null)
            keyManager.removeKeyListener(player);

        player = p;
        keyManager.addKeyListener(player);
    }
    
    /**
     * Seta Camera utilizada para visualizar a cena
     * @param cam Camera
     */
    public void setCamera(Camera cam)
    { 
        camera = cam; 
        world.setActiveCamera(cam); 
    }
    
    /**
     * Retorna Camera utilizada
     * @return Retorna Camera
     */
    public Camera getCamera()
    { 
        return camera; 
    }    
    
    /**
     * Recupera inst鈔cia da classe
     * @return inst鈔cia da classe
     */
    public static EntityManager getInstance()
    {
        if(entitymanager == null) entitymanager = new EntityManager();
        return entitymanager;
    }
    
    /**
     * Aplica um visitor as entidades da cena
     * @param visitor Visitor
     */
    public void applyVisitor(EntityVisitor visitor)
    {
        Enumeration en = entities.elements();
        while( en.hasMoreElements() )
            ((Entity)en.nextElement()).accept(visitor);
    }
        
    /**
     * Recupera cole玢o das entidades
     * @return Enumera玢o das entidades
     */
    public Enumeration getEntities(){ return entities.elements(); }
    
    /**
     * Renderiza a cena
     * @param g3d Objeto Graphics3D utilizado para renderizar a cena
     */
   public void render(Graphics3D g3d)
    {
        Enumeration en = entities.elements();
        while( en.hasMoreElements() )
            ((Entity)en.nextElement()).update();
        
        world.align(camera);
        g3d.render(world);
    }

    /**
     * Recupera objeto da cena
     * @return objeto de cena
     */
    public World getWorld()
    {
        return world;
    }

    /**
     * Seta objeto de cena
     * @param world objeto de cena
     */
    public void setWorld(World world)
    {
        this.world = world;
        camera = world.getActiveCamera();
    }
    
    /**
     * Retorna Player corrente
     * @return Player corrente
     */
    public Player getPlayer(){ return player; }

    /**
     * Recupera Visitor de gravidade
     * @return Visitor de gravidade
     */
    public EntityVisitor getGravity()
    {
        return gravity;
    }

    /**
     * Seta Visitor para aplicar gravidade
     * @param gravity Visitor de gravidade
     */
    public void setGravity(EntityVisitor gravity)
    {
        this.gravity = gravity;
    }

    /**
     * Recupera Visitor de Colis鉶
     * @return Visitor de Colis鉶
     */
    public EntityVisitor getCollision()
    {
        return collision;
    }

    /**
     * Seta Visitor para checagem de colis鉶
     * @param collision Visitor de colis鉶
     */
    public void setCollision(EntityVisitor collision)
    {
        this.collision = collision;
    }

    /**
     * Executa o metodo da interface State para realizar opera珲es com os Sprites, como aplica玢o de Intelig阯cia Artificial (AI)
     */
    public void moveSprites()
    {
        Enumeration en = entities.elements();
        while( en.hasMoreElements() )
        {
            Entity entity = (Entity)en.nextElement();
            if(entity instanceof Sprite)
                ((Sprite)entity).execute();
        }
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -