예제 #1
0
Object* ObjectFactory::create_object(const char *name,
                                     MediaDB *media,
                                     const char *type,
                                     int x, int y,
                                     const Tmx::Object *obj)
{
    Object *object = 0;
    Object::Direction dir = Object::Right;

    if (!type && obj) {
        type = obj->GetType().c_str();
        if (!name) {
            name = obj->GetName().c_str();
        }
        x = obj->GetX();
        y = obj->GetY();
        dir = get_dir(obj);
    }

    if (strcmp(type, "Player") == 0) {
        std::string pathname = Object::get_prefix() + std::string(name);
        TiXmlDocument doc(pathname.c_str());
        if (doc.LoadFile()) {
            search_nodes(&doc);
        }

        if (strcmp(priv_object_type, "knight") == 0) {
            object = new Knight(name, media, x, y, dir);
        }
        else if (strcmp(priv_object_type, "climber") == 0) {
            object = new Climber(name, media, x, y, dir);
        }
        else if (strcmp(priv_object_type, "flyer") == 0) {
            object = new Flyer(name, media, x, y, dir);
        }
        else if (strcmp(priv_object_type, "natator") == 0) {
            object = new Natator(name, media, x, y, dir);
        }
        else if (strcmp(priv_object_type, "dragon") == 0) {
            object = new Dragon(name, media, x, y, dir);
        }
        else {
            object = new Player(name, media, x, y, dir);
        }
    }
    else if (strcmp(type, "Collectable") == 0) {
        std::string pathname = Object::get_prefix() + std::string(name);
        TiXmlDocument doc(pathname.c_str());
        if (doc.LoadFile()) {
            search_nodes(&doc);
        }

        if (strcmp(priv_object_type, "coin") == 0) {
            object = new Coin(name, media, x, y);
        }
        else if (strcmp(priv_object_type, "heart_container") == 0) {
            object = new HeartContainer(name, media, x, y);
        }
        else if (strcmp(priv_object_type, "heart_refill") == 0) {
            object = new HeartRefill(name, media, x, y);
        }
        else if (strcmp(priv_object_type, "potion") == 0) {
            object = new Potion(name, media, x, y);
        }
        else if (strcmp(priv_object_type, "stone") == 0) {
            object = new Stone(name, media, x, y);
        }
        else if (strcmp(priv_object_type, "fire_refill") == 0) {
            object = new FireRefill(name, media, x, y);
        }
        else if (strcmp(priv_object_type, "tornado_refill") == 0) {
            object = new TornadoRefill(name, media, x, y);
        }
        else if (strcmp(priv_object_type, "arrow_refill") == 0) {
            object = new ArrowRefill(name, media, x, y);
        }
        else if (strcmp(priv_object_type, "boomerang_refill") == 0) {
            object = new BoomerangRefill(name, media, x, y);
        }
        else if (strcmp(priv_object_type, "thunder_refill") == 0) {
            object = new ThunderRefill(name, media, x, y);
        }
        else if (strcmp(priv_object_type, "break_rock") == 0) {
            object = new BreakRock(name, media, x, y);
        }
        else if (strcmp(priv_object_type, "create_rock") == 0) {
            object = new CreateRock(name, media, x, y);
        }
    }
    else if (strcmp(type, "Item") == 0) {
        std::string pathname = Object::get_prefix() + std::string(name);
        TiXmlDocument doc(pathname.c_str());
        if (doc.LoadFile()) {
            search_nodes(&doc);
        }

        if (strcmp(priv_object_type, "key") == 0) {
            object = new Key(name, media, x, y);
        }
        else if (strcmp(priv_object_type, "arm") == 0) {
            object = new Arm(name, media, x, y);
        }
        else if (strcmp(priv_object_type, "shield") == 0) {
            object = new Shield(name, media, x, y);
        }
        else if (strcmp(priv_object_type, "armour") == 0) {
            object = new Armour(name, media, x, y);
        }
    }
    else if (strcmp(type, "Walker") == 0) {
        object = new Walker(name, media, x, y, dir);
    }
    else if (strcmp(type, "Crawler") == 0) {
        object = new Crawler(name, media, x, y, dir);
    }
    else if (strcmp(type, "Erupter") == 0) {
        object = new Erupter(name, media, x, y, dir);
    }
    else if (strcmp(type, "Guardian") == 0) {
        object = new Guardian(name, media, x, y, dir);
    }
    else if (strcmp(type, "Shooter") == 0) {
        object = new Shooter(name, media, x, y, dir);
    }
    else if (strcmp(type, "Diver") == 0) {
        object = new Diver(name, media, x, y, dir);
    }
    else if (strcmp(type, "Hovering") == 0) {
        if (obj) {
            object = new Hovering(name, media, x, y, obj->GetWidth(), dir);
        }
    }
    else if (strcmp(type, "Floater") == 0) {
        object = new Floater(name, media, x, y, dir);
    }
    else if (strcmp(type, "Swimmer") == 0) {
        object = new Swimmer(name, media, x, y, dir);
    }
    else if (strcmp(type, "Haunter") == 0) {
        object = new Haunter(name, media, x, y,
                            obj->GetWidth(), obj->GetHeight(), dir);
    }
    else if (strcmp(type, "Waver") == 0) {
        object = new Waver(name, media, x, y, dir);
    }
    else if (strcmp(type, "Dancer") == 0) {
        object = new Dancer(name, media, x, y, dir);
    }
    else if (strcmp(type, "Roller") == 0) {
        object = new Roller(name, media, x, y, obj->GetPolyline());
    }
    else if (strcmp(type, "Falling") == 0) {
        object = new Falling(name, media, x, y, dir);
    }
    else if (strcmp(type, "Obstacle") == 0) {
        object = new Obstacle(name, media, x, y, dir);
    }
    else if (strcmp(type, "MekaDragon") == 0) {
        object = new MekaDragon(name, media, x, y, dir);
    }
    else if (strcmp(type, "MummyDragon") == 0) {
        object = new MummyDragon(name, media, x, y, dir);
    }
    else if (strcmp(type, "Area") == 0) {
        if (obj) {
            const Tmx::PropertySet prop = obj->GetProperties();
            std::string tn = prop.GetLiteralProperty(std::string("type"));
            object = new Area(name, media, tn.c_str(),
                              x, y, obj->GetWidth(), obj->GetHeight());

            if (object) {
                std::string musicname =
                    prop.GetLiteralProperty(std::string("music"));
                if (musicname != std::string("No such property!")) {
                    object->set_string("music", musicname.c_str());
                }
            }
        }
    }
    else if (strcmp(type, "Chest") == 0) {
        object = new Chest(name, media, x, y);
    }

    if (object) {
#ifdef TODO
        // Causes error now for areas
        if (!object->get_loaded()) {
            delete object;
            object = 0;
        }
#endif

        // Initialize all attributes in tmx mapfile
        if (obj) {
            const Tmx::PropertySet prop = obj->GetProperties();
            std::map<std::string, std::string> pmap = prop.GetList();
            for (std::map<std::string, std::string>::const_iterator it =
                     pmap.begin();
                 it != pmap.end();
                 ++it) {
                std::string attr_name = it->first;
                if (attr_name != std::string("direction") &&
                    attr_name != std::string("music")) {
                    object->set_attribute(attr_name.c_str(),
                                          atoi(it->second.c_str()));
                }
            }
        }

        object->initialize();
    }

    return object;
}