Esempio n. 1
0
/*!
  Moves the block at (x,y) in the specified direction.
  Returns the number of blocks moved.
*/
cat_move_t Rodent::moveCat(unsigned &x, unsigned &y) {

  unsigned moves = 0;
  bool found_mouse = false;
  int xoff = 0;
  int yoff = 0;
  
  for (int i=-1;i<=1;++i) {
    for (int j=-1;j<=1;++j) {
      if (blockAt(x+i,y+j) == mouse) {
	found_mouse = true;
	xoff = i;
	yoff = j;
	setBlockAt(x+xoff, y+yoff, blockAt(x, y));
	setBlockAt(x, y, empty);
	x += xoff;
	y += yoff;
	return killed_mouse;
      }
      if (blockAt(x+i,y+j) == empty)
	moves++;
    }
  }
  if (moves == 0) return frozen;
  

  direction_t dir;
  do {
    dir = direction_t(std::rand()%last_dir);
    getOffset(dir, xoff, yoff);
  } while (blockAt(x+xoff,y+yoff) != empty);
  setBlockAt(x+xoff, y+yoff, blockAt(x, y));
  setBlockAt(x, y, empty);
  x += xoff;
  y += yoff;
  return cat_moved;
}
Esempio n. 2
0
EntiteMobile::EntiteMobile(bool decoupagePerspective, Niveau *n, uindex_t index, ElementNiveau::elementNiveau_t cat) : ElementNiveau(decoupagePerspective, n, index, cat), _tempsPrecedent(0), _images(), _cadres(), _tempsAffichage(), _nbImages(), _imageActuelle(0), _action(a_immobile), _direction(gauche), _mort(false), _mortTerminee(false) {
	std::memset(_nbImages, 0, nbActions * sizeof(size_t));
	for(action_t a = premiereAction; a != nbActions; ++a) {
		std::memset(_cadres[a], 0, 8 * sizeof(Rectangle *));
	}
	
	TiXmlElement *e = ElementNiveau::description(index, cat);
	char const *img = e->Attribute("image");
	if(img) {
		for(action_t a = premiereAction; a != nbActions; ++a) {
			_images[a] = Image(Session::cheminRessources() + img);
		}
	}
	
	size_t dimX = 128, dimY = 128;
	if(e->Attribute("blocX"))
		e->Attribute("blocX", &dimX);
	if(e->Attribute("blocY"))
		e->Attribute("blocY", &dimY);
	
	size_t premiereImage[nbActions] = {0};
	
	for(action_t a = premiereAction; a != nbActions; ++a) {
		TiXmlElement *action = e->FirstChildElement(EntiteMobile::transcriptionAction(a));
		if(action) {
			action->Attribute("nbPoses", &_nbImages[a]);
			if(action->Attribute("image")) {
				_images[a] = Image(Session::cheminRessources() + action->Attribute("image"));
			}
			
			if(action->Attribute("premiere"))
				action->Attribute("premiere", &premiereImage[a]);
			else if(a != premiereAction) {
				premiereImage[a] = premiereImage[a - 1] + _nbImages[a - 1];
			}
			int temps;
			action->Attribute("tempsAttente", &temps);
			_tempsAffichage[a] = temps / 1000.0f;
			
			if(a == a_attaquer) {
				if(action->Attribute("attaque")) {
					action->Attribute("attaque", &_imageAttaque);
				}
				else {
					_imageAttaque = _nbImages[a_attaquer] - 1;
				}
			}
			
			if(!_images[a].chargee()) {
				throw ElementNiveau::Exc_DefinitionEntiteIncomplete();
			}
		}
	}
	
	for(int direction = 0; direction < 8; ++direction) {
		for(action_t a = premiereAction; a != nbActions; ++a) {
			index_t x = premiereImage[a];
			_cadres[a][direction] = new Rectangle[_nbImages[a]];
			for(int p = 0; p < _nbImages[a]; ++p) {
				_cadres[a][direction][p] = Rectangle(x * dimX, direction * dimY, dimX, dimY);
				++x;
			}
		}
	}
	
	this->definirAction(EntiteMobile::a_immobile);
	_direction = direction_t(nombreAleatoire(8));
}