Exemple #1
0
void Gamestep3::update()
{
	int width= window.getSize().x;
	int height= window.getSize().y;

	int white1size = white1.size();
	int white2size = white2.size();

	for(unsigned int j=0; j<white1size; j++){
		if(white1size>0 && white1.at(j)->getPosition().y>height+128){
			white1.erase(white1.begin()+j);
			white1size--;
		}
	}

	for(unsigned int j=0; j<white2size; j++){
		if(white2size>0 && white2.at(j)->getPosition().y>height+128){
			white2.erase(white2.begin()+j);
			white2size--;
		}
	}


	//spawn white1
	if(frame%30==0){
		int r = 0 + (rand() % ((width-128) - 0));
		white1.push_back(new Element(Texture("white1.png"),sf::Vector2f(r,0), 0.f));
		float srand = (rand()%5)/10.0;
		white1[white1.size()-1]->setScale(0.75+(srand),0.75+(srand));
		white1l.addElement(white1[white1.size()-1]);
	}

	//spawn white2
	if(frame%50==0){
		int r = 0 + (rand() % ((width-128) - 0));
		white2.push_back(new Element(Texture("white2.png"),sf::Vector2f(r,0), 0.f));
		float srand = (rand()%10)/10.0;
		white2[white2.size()-1]->setScale(0.75+(srand),0.75+(srand));
		white2l.addElement(white2[white2.size()-1]);
	}
	//Update white1
	for(std::vector<Element*>::iterator i = white1.begin(); i != white1.end(); i++){
		(*i)->move(0,2);
	}

	//Update white2
	for(std::vector<Element*>::iterator i = white2.begin(); i != white2.end(); i++){
		(*i)->move(0,1);
	}


	// Gestion du click
	bool lbutton = sf::Mouse::isButtonPressed(sf::Mouse::Left);
	bool rbutton = sf::Mouse::isButtonPressed(sf::Mouse::Right);

	if( (lbutton || rbutton) && !clicked)
	{
		clicked = true;
		bool clicDansLeVide = true;

		for(std::vector<Cell*>::iterator i = cells.begin(); i != cells.end(); i++)
		{
			if( (*i)->isInside(sf::Mouse::getPosition()) )
			{
				onclick(*i, lbutton);
				clicDansLeVide = false;
				break;
			}
		}

		if(lbutton && clicDansLeVide) unselect();
	}

	if(!lbutton && !rbutton) clicked = false;






	// Dessin du réticule pour la cellule selectionnée
	if(selectedCell != NULL)
	{
		reticule.setColor(sf::Color::Green);
		reticule.setPosition(selectedCell->getPosition());
		reticule.rotate(2);
		float scale = (100+abs(((frame/4)%40)-20)) / 100.f;
		reticule.setScale( scale * selectedCell->getScale() * 5.f );
	}
	else
	{
		reticule.setColor(sf::Color::Transparent);
	}


	// Suppression des agents inactifs
	for(std::vector<Agent*>::iterator i = agents.begin(); i != agents.end();)
	{
		if( !(*i)->isAlive() )
		{
			delete (*i);
			layerAgent.removeElement(*i);
			i = agents.erase(i);
		}
		else i++;
	}



	// Conditions de victoire et pre-check pour l'AI
	int nbPositive = 0;
	int nbNegative = 0;
	int nbNeutre = 0;
	for(std::vector<Cell*>::iterator i = cells.begin(); i != cells.end(); i++)
	{
		if( (*i)->getValue() > 0 ) nbPositive++;
		if( (*i)->getValue() < 0 ) nbNegative++;
		if( (*i)->getValue() == 0 ) nbNeutre++;
	}
	if(nbPositive == 0) lose = true;
	if(nbNegative == 0) win = true;


	// Boucle AI
	for(std::vector<Cell*>::iterator i = cells.begin(); i != cells.end(); i++)
	{
		if( (*i)->getValue() < 0 )
		{
			int intervalBetweenAttacks = ((120 /*+ rand()%60*/) /*- (*i)->getValue()*3*/ + nbNegative*50);

			if((*i)->getLastAttack() == 0) (*i)->setLastAttack(frame);
			if(frame - (*i)->getLastAttack() > intervalBetweenAttacks)
			{
				(*i)->setLastAttack(frame);

				// Opérations très lourdes mais osef total vu les perfs du jeu
				std::vector<Cell*> targets = cells;
				std::sort(targets.begin(), targets.end(), AI::SortFunctor((*i)));
				std::remove_if(targets.begin(), targets.end(), AI::RemoveFunctor((*i)));

				if(targets.size() > 0)
				{
					int target = rand() % min(3, (int)targets.size());

					Agent* a = new Agent(10, *(*i), *(targets[target]));
					agents.push_back(a);
					layerAgent.addElement(a);
				}
			}

		}
	}

	// Fond rouge
	int red = 90+abs(((frame/4)%60)-30);
	window.clear(sf::Color(red, 22, 22));


	layerManager.update(frame);
	layerManager.draw(window);
	window.display();

	frame++;
}
Exemple #2
0
bool BaseWidget::mouse_clicked_callback(Action do_this) {
    double mx, my;
    display_get_cursor_position(&mx, &my);
    return onclick((int)mx, (int)my, do_this);
}
Exemple #3
0
void ui_loop(int full_throttle){
	Window root_return,*children_return,parent;
	unsigned int n_children;
	XEvent ev;
	event_t *v_event;
	int i;
	XWindowAttributes winattr,wa;
	int x,y;
	int propagate;
	char keydown[16];
	KeySym keysym_ret;
	XComposeStatus status_in_out;

	symbol_x11 data;

	vkey key=ANY;

	if(full_throttle){
		send_empty_event(win);
	}

	XColor reds,redx;

	XAllocNamedColor(dpy,
                     DefaultColormapOfScreen(DefaultScreenOfDisplay(dpy)),
	                     "red", &reds, &redx);

	while(running){
		XNextEvent(dpy,&ev);

		switch(ev.type){
			case ButtonRelease:
				lastMX=ev.xbutton.x;
				lastMY=ev.xbutton.y;

				onclick(lastMX,lastMY);
				refresh();
				break;
			case KeyRelease:
				uk_log("KeyRelease %i",ev.xkey.keycode);

	// FIXME what should index be? currently at 0, also this function is deprecated
				data.ks=XKeycodeToKeysym(dpy,ev.xkey.keycode,0);
				data.modifier=0;
				data.keycode=ev.xkey.keycode;

				log_platformspecific(&data);
				
				if(ev.xkey.keycode==22) key=BACKSPACE;
				else if(ev.xkey.keycode==36) key=ENTER;
				else key=ANY;

				XLookupString(&ev,keydown,16,&keysym_ret,&status_in_out);
				onrelease(keydown,&propagate,key,(void *)&data);
				uk_log("propagate = %i",propagate);
				if(!propagate) break;

				uk_log("event propagated");
				v_event=_get_event_from_keycode(ev.xkey.keycode);
				onevent(v_event);

				refresh();
				XFlush(dpy);
				break;
			case DestroyNotify:
				uk_log("destroy window");
				//  Destroy notify here makes the program close whenever you open 
				// A new window on my machine. Wonder why?
//				running=0;
				break;
			case ClientMessage:
				if(ev.xclient.data.l[0]!=0x42 || ! full_throttle) break;
				send_empty_event(win);
			case Expose:
				exposes++;	
				if((exposes%10)==0)
					uk_log("eps=%i",1000*exposes/(get_msec()-start_time));
				onrender();

				reset_clip();
				__x11_frame++;
				XCopyArea(dpy,double_buffer,win,gc,0,0,WIDTH,HEIGHT,0,0);
//				XFlush(dpy);
				break;
			case ReparentNotify:
				uk_log("Reparent win=%x par=%x",ev.xreparent.window,ev.xreparent.parent);
				if(ev.xreparent.parent!=0){	
					win_parent=ev.xreparent.parent;
				}
				break;
			case ConfigureNotify:
				get_win_pos(win,&lastX,&lastY);
				// Let's start out with window
/*				i=0;	
				for(Window cwin=win; cwin != 0; cwin=parent){
				// I'm sure this is cheating!
					winX=ev.xconfigure.x;
					winY=ev.xconfigure.y;
					XQueryTree(dpy,cwin,&root_return,&parent,&children_return,&n_children);


					// Don't ask. This is pure magic!
					// Probably it only works on Ubuntu
					// there are for some 2 windows around my window 
					// until I can see the  actual position of the window. 
					// Milage may vary. 
					if(i ==  2){
						XWindowAttributes attr;
						XGetWindowAttributes(dpy,cwin,&attr);
						lastX=attr.x; lastY=attr.y;
						uk_log("windows moved to (%i,%i)",lastX,lastY);
					}
					i++;
				}
				*/
				break;
		}
	}	
	uk_log("ui_loop end!");
//	writestate(STATE_FILE,lastX,lastY);
	veta_exit();
}