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

📄 board.java

📁 MegaMek is a networked Java clone of BattleTech, a turn-based sci-fi boardgame for 2+ players. Fight
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        // and see if the fire is still burning.        tracker = (InfernoTracker) this.infernos.get( coords );        if ( null != tracker ) {            if ( tracker.isStillBurning() ) {                result = true;            }        }        return result;    }    /**     * Record that a new round of burning has passed for the given coordinates.     * This routine also determines if the fire is still burning.     *     * @param   coords - the <code>Coords</code> being checked.     * @return  <code>true</code> if those coordinates have a burning     *          inferno round. <code>false</code> if no inferno has hit     *          those coordinates or if it has burned out.     */    public boolean burnInferno( Coords coords ) {        boolean result = false;        InfernoTracker tracker = null;        // Get the tracker for those coordinates, record the round        // of burning and see if the fire is still burning.        tracker = (InfernoTracker) this.infernos.get( coords );        if ( null != tracker ) {            tracker.newRound(-1);            if ( tracker.isStillBurning() ) {                result = true;            }            else {                infernos.remove(coords);            }        }        return result;    }    /**     * Get an enumeration of all coordinates with infernos still burning.     *     * @return  an <code>Enumeration</code> of <code>Coords</code> that     *          have infernos still burning.     */    public Enumeration getInfernoBurningCoords() {        // Only include *burning* inferno trackers.        Vector burning = new Vector();        Enumeration iter = this.infernos.keys();        while ( iter.hasMoreElements() ) {            final Coords coords = (Coords) iter.nextElement();            if ( this.isInfernoBurning(coords) ) {                burning.addElement( coords );            }        }        return burning.elements();    }    /**     *      * Determine the remaining number of turns the given coordinates will have     * a burning inferno.     *     * @param   coords - the <code>Coords</code> being checked.     *          This value must not be <code>null</code>.  Unchecked.     * @return  the <code>int</code> number of burn turns left for all infernos     *          This value will be non-negative.     */    public int getInfernoBurnTurns( Coords coords ) {        int turns = 0;        InfernoTracker tracker = null;        // Get the tracker for those coordinates        // and see if the fire is still burning.        tracker = (InfernoTracker) this.infernos.get( coords );        if ( null != tracker ) {            turns = tracker.getTurnsLeftToBurn();        }        return turns;    }    /**     *      * Determine the remaining number of turns the given coordinates will have     * a burning Inferno IV round.     *     * @param   coords - the <code>Coords</code> being checked.     *          This value must not be <code>null</code>.  Unchecked.     * @return  the <code>int</code> number of burn turns left for Arrow IV     *          infernos.  This value will be non-negative.     */    public int getInfernoIVBurnTurns( Coords coords ) {        int turns = 0;        InfernoTracker tracker = null;        // Get the tracker for those coordinates        // and see if the fire is still burning.        tracker = (InfernoTracker) this.infernos.get( coords );        if ( null != tracker ) {            turns = tracker.getArrowIVTurnsLeftToBurn();        }        return turns;    }    /**     * Get an enumeration of all buildings on the board.     *     * @return  an <code>Enumeration</code> of <code>Building</code>s.     */    public Enumeration getBuildings() {        return this.buildings.elements();    }    /**     * Get the building at the given coordinates.     *     * @param   coords - the <code>Coords</code> being examined.     * @return  a <code>Building</code> object, if there is one at the     *          given coordinates, otherwise a <code>null</code> will     *          be returned.     */    public Building getBuildingAt( Coords coords ) {        return (Building) this.bldgByCoords.get( coords );    }    /**     * Get the local object for the given building.  Call this routine     * any time the input <code>Building</code> is suspect.     *     * @param   other - a <code>Building</code> object which may or may     *          not be represented on this board.     *          This value may be <code>null</code>.     * @return  The local <code>Building</code> object if we can find a     *          match.  If the other building is not on this board, a     *          <code>null</code> is returned instead.     */    private Building getLocalBuilding( Building other ) {        // Handle garbage input.        if ( other == null ) {            return null;        }        // ASSUMPTION: it is better to use the Hashtable than the Vector.        Building local = null;        Enumeration coords = other.getCoords();        if ( coords.hasMoreElements() ) {            local = (Building) bldgByCoords.get( coords.nextElement() );            if ( !other.equals( local ) ) {                local = null;            }        }        // TODO: if local is still null, try the Vector.        return local;    }    /**     * Collapse an array of buildings.     *     * @param   bldgs - the <code>Vector</code> of <code>Building</code>     *          objects to be collapsed.     */    public void collapseBuilding( Vector bldgs ) {        // Walk through the vector of buildings.        Enumeration loop = bldgs.elements();        while ( loop.hasMoreElements() ) {            final Building other = (Building) loop.nextElement();            // Find the local object for the given building.            Building bldg = this.getLocalBuilding( other );            // Handle garbage input.            if ( bldg == null ) {                System.err.print( "Could not find a match for " );                System.err.print( other );                System.err.println( " to collapse." );                continue;            }            // Update the building.            this.collapseBuilding( bldg );        } // Handle the next building.    }    /**     * The given building has collapsed.  Remove it from the board and     * replace it with rubble.     *     * @param   other - the <code>Building</code> that has collapsed.     */    public void collapseBuilding( Building bldg ) {        // Remove the building from our building vector.        this.buildings.removeElement( bldg );        // Walk through the building's hexes.        Enumeration bldgCoords = bldg.getCoords();        while ( bldgCoords.hasMoreElements() ) {            final Coords coords = (Coords) bldgCoords.nextElement();            final IHex curHex = this.getHex( coords );            int elevation = curHex.getElevation();            // Remove the building from the building map.            this.bldgByCoords.remove( coords );            // Remove the building terrain.            curHex.removeTerrain(Terrains.BUILDING);            curHex.removeTerrain(Terrains.BLDG_CF);            curHex.removeTerrain(Terrains.BLDG_ELEV);            curHex.removeTerrain(Terrains.FUEL_TANK);            curHex.removeTerrain(Terrains.FUEL_TANK_CF);            curHex.removeTerrain(Terrains.FUEL_TANK_ELEV);            curHex.removeTerrain(Terrains.BRIDGE);            curHex.removeTerrain(Terrains.BRIDGE_CF);            curHex.removeTerrain(Terrains.BRIDGE_ELEV);            // Add rubble terrain that matches the building type.            curHex.addTerrain(Terrains.getTerrainFactory().createTerrain(Terrains.RUBBLE, bldg.getType()));            // Any basement reduces the hex's elevation.            if ( curHex.containsTerrain(Terrains.BLDG_BASEMENT)) {System.out.println("Setting basement elevation: "+elevation+":"+curHex);                elevation -= curHex.terrainLevel(Terrains.BLDG_BASEMENT);                curHex.removeTerrain(Terrains.BLDG_BASEMENT);                curHex.setElevation(elevation);            }            // Update the hex.            // TODO : Do I need to initialize it???            // ASSUMPTION: It's faster to update one at a time.            this.setHex( coords, curHex );        } // Handle the next building hex.    } // End public void collapseBuilding( Building )    /**     * Update the construction factors on an array of buildings.     *     * @param   bldgs - the <code>Vector</code> of <code>Building</code>     *          objects to be updated.     */    public void updateBuildingCF( Vector bldgs ) {        // Walk through the vector of buildings.        Enumeration loop = bldgs.elements();        while ( loop.hasMoreElements() ) {            final Building other = (Building) loop.nextElement();            // Find the local object for the given building.            Building bldg = this.getLocalBuilding( other );            // Handle garbage input.            if ( bldg == null ) {                System.err.print( "Could not find a match for " );                System.err.print( other );                System.err.println( " to update." );                continue;            }            // Set the current and phase CFs of the building.            bldg.setCurrentCF( other.getCurrentCF() );            bldg.setPhaseCF( other.getPhaseCF() );        } // Handle the next building.    }    /**     * Get the current value of the "road auto-exit" option.     *     * @return  <code>true</code> if roads should automatically exit onto     *          all adjacent pavement hexes.  <code>false</code> otherwise.     */    public boolean getRoadsAutoExit() {        return this.roadsAutoExit;    }    /**     * Set the value of the "road auto-exit" option.     *     * @param   value - The value to set for the option; <code>true</code>     *          if roads should automatically exit onto all adjacent pavement     *          hexes.  <code>false</code> otherwise.     */    public void setRoadsAutoExit( boolean value ) {        this.roadsAutoExit = value;    }    /**     * Populate the <code>bldgByCoords</code> member from the current     * <code>Vector</code> of buildings.  Use this method after de-     * serializing a <code>Board</code> object.     */    private void createBldgByCoords() {        // Make a new hashtable.        bldgByCoords = new Hashtable();        // Walk through the vector of buildings.        Enumeration loop = buildings.elements();        while ( loop.hasMoreElements() ) {            final Building bldg = (Building) loop.nextElement();            // Each building identifies the hexes it covers.            Enumeration iter = bldg.getCoords();            while ( iter.hasMoreElements() ) {                bldgByCoords.put( iter.nextElement(), bldg );            }        }    }    /**     * Override the default deserialization to populate the transient     * <code>bldgByCoords</code> member.     *     * @param   in - the <code>ObjectInputStream</code> to read.     * @throws  <code>IOException</code>     * @throws  <code>ClassNotFoundException</code>     */    private void readObject( ObjectInputStream in )        throws IOException, ClassNotFoundException     {        in.defaultReadObject();        // Restore bldgByCoords from buildings.        createBldgByCoords();    }    /* (non-Javadoc)     * @see megamek.common.IBoard#addBoardListener(megamek.common.BoardListener)     */    public void addBoardListener(BoardListener listener) {        getListeners().addElement(listener);    }    /* (non-Javadoc)     * @see megamek.common.IBoard#removeBoardListener(megamek.common.BoardListener)     */    public void removeBoardListener(BoardListener listener) {        if (boardListeners != null) {            boardListeners.removeElement(listener);        }    }    protected void processBoardEvent(BoardEvent event) {        if (boardListeners == null) {            return;        }        for(Enumeration e = boardListeners.elements(); e.hasMoreElements();) {            BoardListener l = (BoardListener)e.nextElement();            switch(event.getType()) {            case BoardEvent.BOARD_CHANGED_HEX :                l.boardChangedHex(event);                break;            case BoardEvent.BOARD_NEW_BOARD :                l.boardNewBoard(event);                break;            }        }    }        protected Vector getListeners() {        if (boardListeners == null) {            boardListeners = new Vector();        }        return boardListeners;    }    //Is there any way I can get around using this?    public Hashtable getInfernos() {        return infernos;    }}

⌨️ 快捷键说明

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