コード例 #1
0
  // Create Corresponding GameObject in the xml
  public GameObject createGameObjects(String spriteName) {

    GameObject curObj = null; // cleaning the previous object

    // Don't miss the break statement !
    switch (SpriteType.toSprite(spriteName)) {
      case Jack:
        curObj = new Jack(en);
        break;
      case Tree:
        curObj = new Tree(en);
        break;
      case Chicken:
        curObj = new Chicken(en);
        break;
      case Bomby:
        curObj = new Bomby(en);
        break;
      case House:
        curObj = new House(en);
        break;
      case LightBase:
        curObj = new LightBase(en);
        break;
      case LightHouse:
        curObj = new LightHouse(en);
        break;
      case Zelda:
        curObj = new Zelda(en);
        break;
      case Dragon:
        curObj = new Dragon(en);
        // System.out.println(curObj);
        break;
      case Hexen:
        curObj = new Hexen(en);
        // System.out.println(curObj);
        break;
      case Pocky:
        curObj = new Pocky(en);
        // System.out.println(curObj);
        break;
      case Lion:
        curObj = new Lion(en);
        // System.out.println(curObj);
        break;

      case Beast:
        curObj = new Beast(en);
        System.out.println(curObj);
        break;

      case Kraken:
        curObj = new Kraken(en);
        System.out.println(curObj);
        break;

        // Handles unknown type
      case Unknown:
        System.err.println("Found unknown type: " + spriteName);
        break;

      default:
        System.err.println("Object is not sprite"); // Very unlikely - just safer side !
    }

    if (curObj != null) curObj.setName(spriteName);
    else
      // Handles unknown type and not defined objects
      System.err.println(
          "SpDataHandler :"
              + spriteName
              + " class may not be created and you only have it in xml file");

    return curObj;
  }
コード例 #2
0
  // private otherwise not safe - since constructor calling this method
  private void loadObjects() {

    staticGameObjs = new ArrayList<GameObject>();
    animatedGameObjs = new ArrayList<GameObject>();
    InputStream in = null;

    try {
      XMLInputFactory inputFactory = XMLInputFactory.newInstance();
      in = en.loadData(configFile);
      // this.setStream(in);
      XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
      // Sprite sprite = null;
      GameObject curObj = null;
      // level = null;

      while (eventReader.hasNext()) {

        XMLEvent event = eventReader.nextEvent();

        if (event.isStartElement()) {

          StartElement startElement = event.asStartElement();

          // If we have a item element we create a new item
          if (startElement.getName().getLocalPart().equals(OBJ_SPRITE)) {

            Iterator<Attribute> attributes = startElement.getAttributes();
            while (attributes.hasNext()) {

              Attribute attribute = attributes.next();
              if (attribute.getName().toString().equals(SPRITE_TYPE)) {

                String spriteName = attribute.getValue();

                curObj = createGameObjects(spriteName);
              }

              if (attribute.getName().toString().equals(NATURE)) {
                curObj.setNature(attribute.getValue());
              }
            }
          } else if (event.isStartElement() && curObj != null) {

            String property = event.asStartElement().getName().getLocalPart();
            event = eventReader.nextEvent();
            String data_Str = event.asCharacters().getData();

            configProperties(curObj, property, data_Str);
          }
        }

        if (event.isEndElement() && curObj != null) {

          EndElement endElement = event.asEndElement();
          if (endElement.getName().getLocalPart().equals(OBJ_SPRITE)) {

            if (curObj.getIsAnimated()) {
              this.animatedGameObjs.add(curObj);
              // Create remaining GameObjects defined in xml tag <total>
              if (curObj.getTotal() > 1) {
                for (int i = 0; i < curObj.getTotal() - 1; i++) {

                  GameObject newCurObj = createGameObjects(curObj.getName());
                  newCurObj.setX((float) Math.random() * 600);
                  newCurObj.setY((float) Math.random() * 100);
                  newCurObj.setName(curObj.getName());
                  newCurObj.setStgy(curObj.getStgy());
                  this.animatedGameObjs.add(newCurObj);
                }
              }

            } else this.staticGameObjs.add(curObj);
          }
        }
      }

      // Send GameObject arrays to current Level
      this.level.setArrGameAnim(animatedGameObjs);
      this.level.setArrGameStatic(staticGameObjs);

    } catch (FileNotFoundException ex) {

    } catch (XMLStreamException e) {
    }
  }