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

📄 game.java

📁 MegaMek is a networked Java clone of BattleTech, a turn-based sci-fi boardgame for 2+ players. Fight
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
        return vOutOfGame;    }    /**     * Swap out the current list of dead (or fled) units for a new one.     *     * @param   vOutOfGame - the new <code>Vector</code> of dead or fled units.     *          This value should <em>not</em> be <code>null</code>.     * @throws  IllegalArgumentException if the new list is     *          <code>null</code>.     */    public void setOutOfGameEntitiesVector(Vector vOutOfGame) {        megamek.debug.Assert.assertTrue(vOutOfGame != null, "New out-of-game list should not be null.");        Vector newOutOfGame = new Vector();        // Add entities for the existing players to the game.        for (Enumeration i = vOutOfGame.elements(); i.hasMoreElements();) {            final Entity entity = (Entity)i.nextElement();            int ownerId = entity.getOwnerId();            if (ownerId != Entity.NONE && getPlayer(ownerId) != null) {                entity.setGame(this);                newOutOfGame .addElement(entity);            }        }        this.vOutOfGame = newOutOfGame;        processGameEvent(new GameEntityNewOffboardEvent(this));    }    /**     * Returns an out-of-game entity.     *     * @param   id the <code>int</code> ID of the out-of-game entity.     * @return  the out-of-game <code>Entity</code> with that ID.  If no     *          out-of-game entity has that ID, returns a <code>null</code>.     */    public Entity getOutOfGameEntity (int id) {        Entity match = null;        Enumeration iter = vOutOfGame.elements();        while (null == match && iter.hasMoreElements()) {            Entity entity = (Entity) iter.nextElement();            if (id == entity.getId()) {                match = entity;            }        }        return match;    }    /**     * Returns a <code>Vector</code> containing the <code>Entity</code>s     * that are in the same C3 network as the passed-in unit.  The output     * will contain the passed-in unit, if the unit has a C3 computer.  If     * the unit has no C3 computer, the output will be empty (but it will     * never be <code>null</code>).     *     * @param   entity - the <code>Entity</code> whose C3 network co-     *          members is required.  This value may be <code>null</code>.     * @return  a <code>Vector</code> that will contain all other     *          <code>Entity</code>s that are in the same C3 network     *          as the passed-in unit.  This <code>Vector</code> may     *          be empty, but it will not be <code>null</code>.     * @see     #getC3SubNetworkMembers( Entity )     */    public Vector getC3NetworkMembers( Entity entity ){        Vector members = new Vector();        // Does the unit have a C3 computer?        if ( entity != null && (entity.hasC3() || entity.hasC3i()) ) {            // Walk throught the entities in the game, and add all            // members of the C3 network to the output Vector.            Enumeration units = entities.elements();            while ( units.hasMoreElements() ) {                Entity unit = (Entity) units.nextElement();                if ( entity.equals(unit) || entity.onSameC3NetworkAs(unit) ) {                    members.addElement( unit );                }            }        } // End entity-has-C3        return members;    }    /**     * Returns a <code>Vector</code> containing the <code>Entity</code>s     * that are in the C3 sub-network under the passed-in unit.  The output     * will contain the passed-in unit, if the unit has a C3 computer.  If     * the unit has no C3 computer, the output will be empty (but it will     * never be <code>null</code>).  If the passed-in unit is a company     * commander or a member of a C3i network, this call is the same as     * <code>getC3NetworkMembers</code>.     *     * @param   entity - the <code>Entity</code> whose C3 network sub-     *          members is required.  This value may be <code>null</code>.     * @return  a <code>Vector</code> that will contain all other     *          <code>Entity</code>s that are in the same C3 network     *          under the passed-in unit.  This <code>Vector</code> may     *          be empty, but it will not be <code>null</code>.     * @see     #getC3NetworkMembers( Entity )     */    public Vector getC3SubNetworkMembers( Entity entity ){        // Handle null, C3i, and company commander units.        if ( entity == null || entity.hasC3i() || entity.C3MasterIs(entity) ) {            return getC3NetworkMembers( entity );        }        Vector members = new Vector();        // Does the unit have a C3 computer?        if ( entity.hasC3() ) {            // Walk throught the entities in the game, and add all            // sub-members of the C3 network to the output Vector.            Enumeration units = entities.elements();            while ( units.hasMoreElements() ) {                Entity unit = (Entity) units.nextElement();                if ( entity.equals(unit) || unit.C3MasterIs(entity) ) {                    members.addElement( unit );                }            }        } // End entity-has-C3        return members;    }    /**     * Returns a <code>Hashtable</code> that maps the <code>Coords</code>     * of each unit in this <code>Game</code> to a <code>Vector</code>     * of <code>Entity</code>s at that positions.  Units that have no     * position (e.g. loaded units) will not be in the map.     *     * @return  a <code>Hashtable</code> that maps the <code>Coords</code>     *          positions or each unit in the game to a <code>Vector</code>     *          of <code>Entity</code>s at that position.     */    public Hashtable getPositionMap() {        Hashtable positionMap = new Hashtable();        Vector atPos = null;        // Walk through the entities in this game.        for (Enumeration i = entities.elements(); i.hasMoreElements();) {            final Entity entity = (Entity)i.nextElement();            // Get the vector for this entity's position.            final Coords coords = entity.getPosition();            if ( coords != null ) {                atPos = (Vector) positionMap.get( coords );                // If this is the first entity at this position,                // create the vector and add it to the map.                if ( atPos == null ) {                    atPos = new Vector();                    positionMap.put( coords, atPos );                }                // Add the entity to the vector for this position.                atPos.addElement( entity );            }        } // Handle the next entity.        // Return the map.        return positionMap;    }    /**     * Returns an enumeration of salvagable entities.     */    public Enumeration getGraveyardEntities() {        Vector graveyard = new Vector();        for (Enumeration i = vOutOfGame.elements(); i.hasMoreElements();) {            Entity entity = (Entity)i.nextElement();            if ( entity.getRemovalCondition() == IEntityRemovalConditions.REMOVE_SALVAGEABLE ||                 entity.getRemovalCondition() == IEntityRemovalConditions.REMOVE_EJECTED ) {                graveyard.addElement(entity);            }        }        return graveyard.elements();    }    /**     * Returns an enumeration of wrecked entities.     */    public Enumeration getWreckedEntities() {        Vector wrecks = new Vector();        for (Enumeration i = vOutOfGame.elements(); i.hasMoreElements();) {            Entity entity = (Entity)i.nextElement();            if ( entity.getRemovalCondition() == IEntityRemovalConditions.REMOVE_SALVAGEABLE ||                 entity.getRemovalCondition() == IEntityRemovalConditions.REMOVE_EJECTED ||                 entity.getRemovalCondition() == IEntityRemovalConditions.REMOVE_DEVASTATED ) {                wrecks.addElement(entity);            }        }        return wrecks.elements();    }    /**     * Returns an enumeration of entities that have retreated     */    public Enumeration getRetreatedEntities() {        Vector sanctuary = new Vector();        for (Enumeration i = vOutOfGame.elements(); i.hasMoreElements();) {            Entity entity = (Entity)i.nextElement();            if ( entity.getRemovalCondition() == IEntityRemovalConditions.REMOVE_IN_RETREAT ||                 entity.getRemovalCondition() == IEntityRemovalConditions.REMOVE_CAPTURED ||                 entity.getRemovalCondition() == IEntityRemovalConditions.REMOVE_PUSHED ) {                sanctuary.addElement(entity);            }        }        return sanctuary.elements();    }    /**     * Returns an enumeration of entities that were utterly destroyed     */    public Enumeration getDevastatedEntities() {        Vector smithereens = new Vector();        for (Enumeration i = vOutOfGame.elements(); i.hasMoreElements();) {            Entity entity = (Entity)i.nextElement();            if (entity.getRemovalCondition() == IEntityRemovalConditions.REMOVE_DEVASTATED) {                smithereens.addElement(entity);            }        }        return smithereens.elements();    }    /**     * Return the current number of entities in the game.     */    public int getNoOfEntities() {        return entities.size();    }    /**     * Returns the appropriate target for this game given a type and id     */    public Targetable getTarget(int nType, int nID) {        try {            switch (nType) {            case Targetable.TYPE_ENTITY :                return getEntity(nID);            case Targetable.TYPE_HEX_CLEAR :            case Targetable.TYPE_HEX_IGNITE :            case Targetable.TYPE_HEX_BOMB :            case Targetable.TYPE_MINEFIELD_DELIVER :            case Targetable.TYPE_FLARE_DELIVER :            case Targetable.TYPE_HEX_EXTINGUISH :            case Targetable.TYPE_HEX_ARTILLERY:                return new HexTarget(HexTarget.idToCoords(nID), board, nType);            case Targetable.TYPE_FUEL_TANK :            case Targetable.TYPE_FUEL_TANK_IGNITE :            case Targetable.TYPE_BUILDING :            case Targetable.TYPE_BLDG_IGNITE :                return new BuildingTarget(BuildingTarget.idToCoords(nID), board, nType);            case Targetable.TYPE_MINEFIELD_CLEAR :                return new MinefieldTarget(MinefieldTarget.idToCoords(nID), board);            case Targetable.TYPE_INARC_POD:                return INarcPod.idToInstance( nID );            default :                return null;            }        }        catch (IllegalArgumentException t) {            return null;        }    }    /**     * Returns the entity with the given id number, if any.     */    public Entity getEntity(int id) {        return (Entity)entityIds.get(new Integer(id));    }    public void addEntity(int id, Entity entity) {        entity.setGame(this);        entities.addElement(entity);        entityIds.put(new Integer(id), entity);                if(id > lastEntityId) lastEntityId = id;        megamek.debug.Assert.assertTrue(entities.size()==entityIds.size());        processGameEvent(new GameEntityNewEvent(this, entity));    }    public void setEntity(int id, Entity entity) {        setEntity(id,entity,null);        megamek.debug.Assert.assertTrue(entities.size()==entityIds.size());    }    public void setEntity(int id, Entity entity, Vector movePath) {        final Entity oldEntity = getEntity(id);        if (oldEntity == null) {            addEntity(id, entity);        } else {            entity.setGame(this);            entities.setElementAt(entity, entities.indexOf(oldEntity));            entityIds.put(new Integer(id), entity);            //Not sure if this really required            if(id > lastEntityId) lastEntityId = id;                        processGameEvent(new GameEntityChangeEvent(this,entity, movePath));        }        megamek.debug.Assert.assertTrue(entities.size()==entityIds.size());    }            /**     * @return int containing an unused entity id     */    public int getNextEntityId()    {      return lastEntityId + 1;    }            /**     * Returns true if an entity with the specified id number exists in this     * game.     */    public boolean hasEntity(int entityId) {        return entityIds.containsKey(new Integer(entityId));    }    /**     * Remove an entity from the master list.  If we can't find that entity,     * (probably due to double-blind) ignore it.     */    public void removeEntity( int id, int condition ) {        Entity toRemove = getEntity(id);        if (toRemove == null) {            //This next statement has been cluttering up double-blind            // logs for quite a while now.  I'm assuming it's no longer            // useful.            //System.err.println("Game#removeEntity: could not find entity to remove");            return;        }                entities.removeElement(toRemove);        entityIds.remove(new Integer(id));                toRemove.setRemovalCondition(condition);                // do not keep never-joined entities        if (vOutOfGame != null && condition != IEntityRemovalConditions.REMOVE_NEVER_JOINED) {            vOutOfGame.addElement(toRemove);        }                //We also need to remove it from the list of things to be deployed...        //we might still be in this list if we never joined the game        if ( deploymentTable.size() > 0 ) {            Enumeration iter = deploymentTable.elements();                        while ( iter.hasMoreElements() ) {                Vector vec = (Vector)iter.nextElement();                                for ( int i = vec.size() - 1; i >= 0; i-- ) {                    Entity en = (Entity)vec.elementAt(i);                                        if ( en.getId() == id )                        vec.removeElementAt(i);                }            }        }        processGameEvent(new GameEntityRemoveEvent(this,toRemove));    }    /**     * Resets this game.     */    public void reset() {        roundCount = 0;        entities.removeAllElements();        entityIds.clear();        vOutOfGame.removeAllElements();

⌨️ 快捷键说明

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