Beispiel #1
0
Member::Member(const xmlNode *     xml,
               const std::string & main_folder)
  : parent(NULL)
  , angle_rad(0)
  , alpha(0)
  , go_through_ground(false)
  , pos(0,0)
  , scale(0,0)
  , spr(NULL)
  , name("")
  , type("")
  , anchor(0,0)
{
  XmlReader::ReadStringAttr(xml, "name", name);
  ASSERT(name!="");

  // Load the sprite
  spr = GetResourceManager().LoadSprite(xml, name, main_folder);
  spr->EnableCaches(true, 0);

  // Get the various option
  std::string type_str;
  XmlReader::ReadStringAttr(xml, "type", type_str);
  ASSERT(type_str!="");
  type = MemberType(type_str);

  const xmlNode * el = XmlReader::GetMarker(xml, "anchor");

  if (el) {
    int dx = 0, dy = 0;
    XmlReader::ReadIntAttr(el, "dx", dx);
    XmlReader::ReadIntAttr(el, "dy", dy);
    MSG_DEBUG("body", "   Member %s has anchor (%i,%i)\n", name.c_str(), dx, dy);
    anchor = Point2d(dx, dy);
    spr->SetRotation_HotSpot(Point2i(dx, dy));
  } else {
    MSG_DEBUG("body", "   Member %s has no anchor\n", name.c_str());
  }

  XmlReader::ReadBoolAttr(xml, "go_through_ground", go_through_ground);

  xmlNodeArray                 nodes = XmlReader::GetNamedChildren(xml, "attached");
  xmlNodeArray::const_iterator it    = nodes.begin();
  xmlNodeArray::const_iterator itEnd = nodes.end();

  std::string att_type;
  int         dx = 0;
  int         dy = 0;
  Point2d     d;     // TODO: Rename !!
  std::string frame_str;

  for (; it != itEnd; ++it) {
    //std::string att_type; // TODO lami : can be move ?!

    if (!XmlReader::ReadStringAttr(*it, "member_type", att_type)) {
      std::cerr << "Malformed attached member definition" << std::endl;
      continue;
    }
    MemberType type(att_type);

    XmlReader::ReadIntAttr(*it, "dx", dx);
    XmlReader::ReadIntAttr(*it, "dy", dy);
    MSG_DEBUG("body", "   Attached member %s has anchor (%i,%i)\n", att_type.c_str(), dx, dy);
    d.SetValues(dx, dy);
    XmlReader::ReadStringAttr(*it, "frame", frame_str);

    if ("*" == frame_str) {
      v_attached rot_spot;
      rot_spot.assign(spr->GetFrameCount(), d);
      attached_types[type] = rot_spot;
    } else {
      int frame;

      if (!str2int(frame_str, frame) || frame < 0 || frame >= (int)spr->GetFrameCount()) {
        std::cerr << "Malformed attached member definition (wrong frame number)" << std::endl;
        continue;
      }

      if (attached_types.find(type) == attached_types.end()) {
        v_attached rot_spot;
        rot_spot.resize(spr->GetFrameCount(), Point2d(0.0, 0.0));
        attached_types[type] = rot_spot;
      }
      (attached_types.find(type)->second)[frame] = d;
    }
  }

  AttachTypeMap::iterator attachment_it = attached_types.begin();
  for (; attachment_it != attached_types.end(); ++attachment_it)
    attachment_it->second.SetAnchor(anchor);

  ResetMovement();
}