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

📄 entityencoder.java

📁 MegaMek is a networked Java clone of BattleTech, a turn-based sci-fi boardgame for 2+ players. Fight
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            throw new IllegalStateException( "Not passed a entityData node." );        }        // TODO : perform version checking.        // Walk the entityData node's children.        Enumeration children = node.elements();        while ( children.hasMoreElements() ) {            ParsedXML child = (ParsedXML) children.nextElement();            String childName = child.getName();            // Handle null child names.            if ( null == childName ) {                // No-op.            }            // Did we find the coords node?            else if ( childName.equals("coords") ) {                // We can decode the coords immediately.                coords = CoordsEncoder.decode( child, game );            } // End found-"coords"-child            // Did we find the action node?            // TODO : rename me            else if ( childName.equals("action") ) {                // Save the action node for later decoding.                actionNode = child;            } // End found-"action"-child            // Did we find the narcs node?            else if ( childName.equals("narcs") ) {                // Save the narc node for later decoding.                narcNode = child;            } // End found-"narc"-child            // Did we find the inferno node?            else if ( childName.equals("inferno") ) {                // Save the inferno node for later decoding.                infernoNode = child;            } // End found-"inferno"-child            // Did we find the loadedUnits node?            else if ( childName.equals("loadedUnits") ) {                // Save the loadedUnits node for later decoding.                loadedUnitsNode = child;            } // End found-"loadedUnits"-child            // Did we find the class node?            else if ( childName.equals("class") ) {                // Create the appropriate sub-class of Entity.                attrStr = child.getAttribute( "name" );                if ( null == attrStr ) {                    throw new IllegalStateException                        ( "Couldn't decode the name of a class node." );                }                else if ( attrStr.equals("BipedMech") ) {                    entity = BipedMechEncoder.decode( child, game );                }                else if ( attrStr.equals("QuadMech") ) {                    entity = QuadMechEncoder.decode( child, game );                }                else if ( attrStr.equals("Tank") ) {                    entity = TankEncoder.decode( child, game );                }                else if ( attrStr.equals("BattleArmor") ) {                    entity = BattleArmorEncoder.decode( child, game );                }                else if ( attrStr.equals("Infantry") ) {                    entity = InfantryEncoder.decode( child, game );                }                else if ( attrStr.equals("Protomech") ) {                    entity = ProtomechEncoder.decode( child, game );                }                else if ( attrStr.equals("GunEmplacement") ) {                    entity = GunEmplacementEncoder.decode( child, game );                }                else {                    throw new IllegalStateException                        ( "Unexpected name for a class node: " + attrStr );                }            } // End found-"class"-child        } // Look at the next child.        // Did we find the entity yet?        if ( null == entity ) {            throw new IllegalStateException                ( "Couldn't locate the class for an entityData node." );        }        // Decode the inferno node.        EntityEncoder.decodeInferno( infernoNode, entity );        // TODO : a whole lot more decoding needed.        return entity;    }    /**     * Decode a <code>Entity</code> object from the passed node.     *     * @param   node - the <code>ParsedXML</code> node for this object.     *          This value must not be <code>null</code>.     * @param   game - the <code>IGame</code> the decoded object belongs to.     * @return  the <code>Entity</code> object based on the node.     * @throws  IllegalArgumentException if the node is     *          <code>null</code>.     * @throws  IllegalStateException if the node does not     *          contain a valid <code>Entity</code>.     */    public static Entity decode( ParsedXML node, IGame game ) {        String attrStr = null;        int attrVal = 0;        Entity entity = null;        Coords coords = null;        Vector locations = new Vector();        ParsedXML pilotNode = null;        ParsedXML equipNode = null;        Enumeration children = null;        ParsedXML child = null;        String childName;        // Did we get a null node?        if ( null == node ) {            throw new IllegalArgumentException( "The entity is null." );        }        // Make sure that the node is for a Entity object.        if ( !node.getName().equals( "entity" ) ) {            throw new IllegalStateException( "Not passed a entity node." );        }        // TODO : perform version checking.        // Walk the entity node's children, finding bits for later parsing..        children = node.elements();        while ( children.hasMoreElements() ) {            child = (ParsedXML) children.nextElement();            childName = child.getName();            // Handle null child childNames.            if ( null == childName ) {                // No-op.            }            // Did we find the pilot node?            else if ( childName.equals( "pilot" ) ) {                // Save the entity's pilot for later decoding.                pilotNode = child;            } // End found-"entityEquipment"-node            // Did we find the entityData node?            else if ( childName.equals( "entityData" ) ) {                // Did we find the entity already?                if ( null != entity ) {                    throw new IllegalStateException                        ( "Found two entityData nodes for an Entity node." );                }                // Decode the entity data.                entity = EntityEncoder.decodeEntityData( child, game );            } // End found-"entityData"-node            // Did we find the entityEquipment node?            else if ( childName.equals( "entityEquipment" ) ) {                // Save the entity equipment for later decoding.                equipNode = child;            } // End found-"entityEquipment"-node            // Did we find the location node?            else if ( childName.equals( "location" ) ) {                // Save this location for later decoding.                locations.addElement( child );            } // End found-"location"-node        } // Look at the next child.        // Did we find the needed elements?        if ( null == entity ) {            throw new IllegalStateException                ( "Couldn't locate the entityData for an Entity node." );        }        else if ( null == pilotNode ) {            throw new IllegalStateException                ( "Couldn't locate the pilot for an Entity node." );        }        else if ( null == equipNode ) {            throw new IllegalStateException                ( "Couldn't locate the entityEquipment for an Entity node." );        }        else if ( locations.size() != entity.locations() ) {            StringBuffer msgBuf = new StringBuffer();            msgBuf.append( "Found " )                .append( locations.size() )                .append( " locations for an Entity node. " )                .append( "Was expecting to find " )                .append( entity.locations() )                .append( "." );            throw new IllegalStateException( msgBuf.toString() );        }        // Decode the entity node's chassis.        attrStr = node.getAttribute( "chassis" );        if ( null == attrStr ) {            throw new IllegalStateException                ( "Couldn't decode the chassis from an Entity node." );        }        entity.setChassis( attrStr );        // Decode the entity node's model.        attrStr = node.getAttribute( "model" );        if ( null == attrStr ) {            throw new IllegalStateException                ( "Couldn't decode the model from an Entity node." );        }        entity.setModel( attrStr );        // Decode the entity node's movement type.        attrStr = node.getAttribute( "typeVal" );        if ( null == attrStr ) {            throw new IllegalStateException                ( "Couldn't decode the typeVal from an Entity node." );        }        // Try to pull the value from the string        try {            attrVal = Integer.parseInt( attrStr );        }        catch ( NumberFormatException exp ) {            throw new IllegalStateException                ( "Couldn't get an integer from " + attrStr );        }        entity.setMovementMode( attrVal );        // Decode the entity node's year.        attrStr = node.getAttribute( "year" );        if ( null == attrStr ) {            throw new IllegalStateException                ( "Couldn't decode the year from an Entity node." );        }        // Try to pull the value from the string        try {            attrVal = Integer.parseInt( attrStr );        }        catch ( NumberFormatException exp ) {            throw new IllegalStateException                ( "Couldn't get an integer from " + attrStr );        }        entity.setYear( attrVal );        // Decode the entity node's techBase.        attrStr = node.getAttribute( "techBase" );        if ( null == attrStr ) {            throw new IllegalStateException                ( "Couldn't decode the techBase from an Entity node." );        }        // Try to pull the value from the string        try {            attrVal = Integer.parseInt( attrStr.substring(0,1) );        }        catch ( NumberFormatException exp ) {            throw new IllegalStateException                ( "Couldn't get an integer from " + attrStr.substring(0,1) );        }        entity.setTechLevel( attrVal );        // Decode the entity node's mass.        attrStr = node.getAttribute( "mass" );        if ( null == attrStr ) {            throw new IllegalStateException                ( "Couldn't decode the mass from an Entity node." );        }        // Try to pull the value from the string        try {            attrVal = Integer.parseInt( attrStr );        }        catch ( NumberFormatException exp ) {            throw new IllegalStateException                ( "Couldn't get an integer from " + attrStr );        }        entity.setWeight( attrVal );        // Decode the entity node's walkMp.        attrStr = node.getAttribute( "walkMp" );        if ( null == attrStr ) {            throw new IllegalStateException                ( "Couldn't decode the walkMp from an Entity node." );        }        // Try to pull the value from the string        try {            attrVal = Integer.parseInt( attrStr );        }        catch ( NumberFormatException exp ) {            throw new IllegalStateException                ( "Couldn't get an integer from " + attrStr );        }        entity.setOriginalWalkMP( attrVal );        // Decode the entity node's jumpMp.        attrStr = node.getAttribute( "jumpMp" );        if ( null == attrStr ) {            throw new IllegalStateException                ( "Couldn't decode the jumpMp from an Entity node." );        }        // Try to pull the value from the string        try {            attrVal = Integer.parseInt( attrStr );        }        catch ( NumberFormatException exp ) {            throw new IllegalStateException                ( "Couldn't get an integer from " + attrStr );        }        entity.setOriginalJumpMP( attrVal );        // Try to pull the value from the string        try {            attrVal = Integer.parseInt( attrStr );        }        catch ( NumberFormatException exp ) {            throw new IllegalStateException                ( "Couldn't get an integer from " + attrStr );        }        // Decode the entity's pilot.        EntityEncoder.decodePilot( pilotNode, entity );        // Decode the entity's equipment.        EntityEncoder.decodeEntityEquipment( equipNode, entity );        // Decode the entity's locations.        children = locations.elements();        while ( children.hasMoreElements() ) {            child = (ParsedXML) children.nextElement();            EntityEncoder.decodeLocation( child, entity );        }        return entity;    }}

⌨️ 快捷键说明

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