Ejemplo n.º 1
0
void GameMainModel::receiveDtAndBallsPosition(const float dt, const std::vector<std::pair<float, float>> ballsPosition)
{
	_readyTime -= dt;

	if(_readyTime > 0.0f)
		return;
	else
		_readyTime = 0.0f;
	

	_timeLeft -= dt;

	if (_timeLeft < 0.0f) {
		setGameStatus(GameStatus::TimeUp);

		if (_gameStatus == GameStatus::TimeUp && _gamePrevStatus == GameStatus::Playing)
		{
			allBallsDisabled();
			outDust();
		}

		_timeLeft = 0.0f;
		_presentPoints = 0;

		return;
	}
	else if (_timeLeft < DEFAULT_TIME_LEFT)
	{
		setGameStatus(GameStatus::Playing);
	}

	int size = static_cast<int>(ballsPosition.size());
	for (int i = 0; i < size; ++i) {
		_ballsInfoList.at(i)->x = ballsPosition.at(i).first;
		_ballsInfoList.at(i)->y = ballsPosition.at(i).second;
	}

	if (_touchFlag)
		checkLengthSelectedBallToRest();

	if (_bonusModeFlag) {

		if (_chainPoints == CHAIN_POINTS_THRESHOLD)
			setGameStatus(GameStatus::BonusModeIn);

		_chainPoints -= (dt * CHAIN_POINTS_THRESHOLD / BONUS_MODE_LENGTH);

		if (_chainPoints < 0.0f) {
			_chainPoints = 0.0f;
			_bonusModeFlag = false;
			setGameStatus(GameStatus::BonusModeOut);
		}
	}
}
Ejemplo n.º 2
0
void GameLayer::onTouchEnded(Touch *touch, Event *unused_event){
	if(getGameStatus()==GAME_STATUS_READY){
		setGameStatus(GAME_STATUS_START);
		delegator->onGamePlay();
		snake->resetSnake();	
		setBall();
		ball->setVisible(true);
		bSetDirection=false;
		return ;
	}
	if(getGameStatus()==GAME_STATUS_OVER){
		setGameStatus(GAME_STATUS_READY);
		delegator->onGameStart();
		snake->removeAllChildren();
		ball->setVisible(false);
	}

	if(getGameStatus()==GAME_STATUS_START)
		this->setSnakeDirection(touch->getLocation());
}
Ejemplo n.º 3
0
void GameLayer::snakePlay(float at){
	
	if(getGameStatus()==GAME_STATUS_START){
		int width=SCREEN_WIDTH/SNAKE_WIDTH;
		int height=SCREEN_HEIGHT/SNAKE_WIDTH;
		Rect rect(1,1,width-2,height-2);
		if(snake->IsDie(rect)){
			setGameStatus(GAME_STATUS_OVER);
			delegator->onGameEnd();
			return;
		}
		if(IsEatBall()){
			snake->addSnakeNode();
			this->setBall();
		}
		else snake->play();
		bSetDirection=false;
		return;
	}
	
}
Ejemplo n.º 4
0
bool GameLayer::init(){
	if(!Layer::init())
		return false;
	ball=NULL;
	auto listener = EventListenerTouchOneByOne::create(); 
	listener->onTouchBegan = [&](Touch *touch, Event *unused_event)->bool {return true;};
	listener->onTouchEnded = CC_CALLBACK_2(GameLayer::onTouchEnded, this); 
	this->_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
	Size size=Director::getInstance()->getVisibleSize();
	Point origin=Director::getInstance()->getVisibleOrigin();
	setGameStatus(GAME_STATUS_READY);

	shiftLand = schedule_selector(GameLayer::snakePlay);
        this->schedule(shiftLand, 0.3f);
        this->scheduleUpdate();

		snake=Snake::create();
		
		this->addChild(snake,2);
		
	return true;
}
Ejemplo n.º 5
0
bool restartGame()
{
	// TODO clear any overlay screen I may have
	setGameStatus(GAME_PLAYING);
	return true;
}
Ejemplo n.º 6
0
bool playGame()
{
	int pic_idx; // id for drunkard image
	int beer_idx; // id for beer image
	static int barman_frame_idx;// idx for barman frame idling
	static int beer_frame_idx; 	// idx for beer frame filling
	static short barman_table_pos_idx;

	if (gameDelays.drunk_gen_wait == 0)
	{
		// listDrunkards(d_array);
		pic_idx = rand() % NUM_AVAIL_DRUNKARDS;	// one of the available avatars
// # warning DRUNKARDS DISABLED
#if 1 
		if (addDrunkard(pic_idx, d_array) == false)
			fail(ERR, "Could not add a new drunkard!");
		gameDelays.drunk_gen_wait = gameSetups[currGameSetupIdx].drunkard_gen_time;
#endif
	}

	// if moveBeers returns false, a beer has crashed
	if (moveBeers(b_array, barman_table_pos_idx) == false)
	{
		remLife();
		setGameStatus(GAME_LOCKED);

		// trigger barman failing sprite
		Sprite_SetPosition(&barmanFailSprt,
			barmanPosition[barman_table_pos_idx].x,
			barmanPosition[barman_table_pos_idx].y);

		// Init timer for beer falling
		gameDelays.falling_lag = FALLING_LAG;

		return false;
	}

	// listDrunkards(d_array);
	// if moveDrunkards return false, someone reached
	// the end of the table
	if (moveDrunkards(d_array) == false)
	{
		remLife();
		setGameStatus(GAME_LOCKED);

		// XXX hide the current barman (?) sprite
		// item->b_sprt.w = 0;
		// item->b_sprt.h = 0;

		// TODO trigger drunkard rage animation
		// trigger barman sprite being dragged
		// position him completely on the table
		Sprite_SetPosition(&barmanDragged,
			barmanPosition[barman_table_pos_idx].x -
							barmanDragged.w,
			barmanPosition[barman_table_pos_idx].y);

		return false;
	}

	// check for beers filling in this iteration
	beer_idx = haveBeersFilling(b_array);
	// printf("beers filling? %d\n", beer_idx);

	/***** Barman movement between tables *****/
	// barman can only move if the timer is over
	if (gameDelays.input_wait == 0)
	{
		// ... and he's not filling a mug
		if (beer_idx == -1)
		{
			if (IsPadPress(Pad1Down))
			{
				gameDelays.input_wait = INPUT_LAG;
				if (barman_table_pos_idx < 3)
				{
					barman_table_pos_idx++;
				}
			}

			if (IsPadPress(Pad1Up))
			{
				gameDelays.input_wait = INPUT_LAG;
				if (barman_table_pos_idx > 0)
				{
					barman_table_pos_idx--;
				}
			}

			// idle barman sprite positioning on screen
			Sprite_SetPosition(&barmanIdleSprt,
				barmanPosition[barman_table_pos_idx].x,
				barmanPosition[barman_table_pos_idx].y);

			// busy barman torso sprite positioning on screen
			Sprite_SetPosition(&barmanTapTorsoSprt,
				barmanPosition[barman_table_pos_idx].x,
				barmanPosition[barman_table_pos_idx].y);

			// busy barman legs sprite positioning on screen
			Sprite_SetPosition(&barmanTapLegsSprt,
				barmanPosition[barman_table_pos_idx].x,
				barmanPosition[barman_table_pos_idx].y + BARMAN_SPRT_BEER_TORSO_H);

			if (IsPadPress(Pad1Cross))
			{
				// generate a new beer and add to the list of active beers
				if (gameDelays.filling_lag <= 0)
				{
					// initialize filling animation
					gameDelays.filling_lag = FILLING_LAG;
					barman_frame_idx = 0;

					addBeer(
						barman_table_pos_idx, // table idx
						b_array);
					// listBeers(b_array);
				}
			}
		} // end of beer being filled

#if DEBUG
		if (IsPadPress(Pad1Triangle))
		{
			listBeers(b_array);
			listDrunkards(d_array);
		}
#endif

	} // end barman movevent between tables

	/**** BARMAN IS FILLING A BEER ****/
	// calculate the right barman frame
	if (beer_idx != -1)
	{
		// advance frame while the player is pressing Cross
		// (and I can count down the filling_lag)
		// otherwise stick to the last frame FILLING_OPERATING_4
		// even if the player keeps pressing Cross
		float fill_time = 0.0;
		if (IsPadPress(Pad1Cross))
		{
			barman_is_active = true;

			fill_time = (100 * gameDelays.filling_lag) / FILLING_LAG;
			if (fill_time <= 5.0)
			{
				barman_frame_idx = FILLING_OPERATING;
				beer_frame_idx = BEERTAP_FRO;
			}
			else if (fill_time <= 25.0)
			{
				barman_frame_idx = FILLING_OPERATING;
				beer_frame_idx = BEERTAP_100;
			}
			else if (fill_time <= 50.0)
			{
				barman_frame_idx = FILLING_OPERATING;
				beer_frame_idx = BEERTAP_050;
			}
			else if (fill_time <= 75.0)
			{	
				barman_frame_idx = FILLING_OPERATING;
				beer_frame_idx = BEERTAP_025;
			}
			else if (fill_time <= 100.0)
			{
				barman_frame_idx = FILLING_OPERATING;
				beer_frame_idx = BEERTAP_EMPTY;
			}
		}
		else
		{
			barman_is_active = false;

			if (fill_time <= 25.0)
			{
				barman_frame_idx = FILLING_IDLE;
				beer_frame_idx = BEERTAP_100;
			}
			else if (fill_time <= 50.0)
			{
				barman_frame_idx = FILLING_IDLE;
				beer_frame_idx = BEERTAP_050;
			}
			else if (fill_time <= 75.0)
			{
				barman_frame_idx = FILLING_IDLE;
				beer_frame_idx = BEERTAP_025;
			}
			else if (fill_time <= 100.0)
			{
				barman_frame_idx = FILLING_IDLE;
				beer_frame_idx = BEERTAP_EMPTY;
			}
		}

		// set barman frame
		Sprite_Change(&barmanTapTorsoSprt, &barmanImg,
			barman_frames[barman_frame_idx].x,
			barman_frames[barman_frame_idx].y);

		// set filling beer sprite
		Sprite_Init(&beerTapSprt, &barmanImg,
			SPRITE_NORMAL,
			beerTap_frames[beer_frame_idx].x,
			beerTap_frames[beer_frame_idx].y,
			BEER_TAP_SPRT_W, BEER_TAP_SPRT_W);

		Sprite_SetPosition(&beerTapSprt,
			tapSprt[barman_table_pos_idx].x,
			tapSprt[barman_table_pos_idx].y + BEER_TAP_SPRT_H);
	}
	else
	{
		/**** BARMAN IS IDLE ****/
		if (gameDelays.barman_breath_wait == 0)
		{
			gameDelays.barman_breath_wait = BARMAN_BREATH_WAIT;
			if ((barman_frame_idx % 2) == 0)
			{
				barman_frame_idx = IDLE_0;
				Sprite_Change(&barmanIdleSprt, &barmanImg,
					barman_frames[IDLE_0].x,
					barman_frames[IDLE_0].y);
			}
			else
			{
				barman_frame_idx = IDLE_1;
				Sprite_Change(&barmanIdleSprt, &barmanImg,
					barman_frames[IDLE_1].x,
					barman_frames[IDLE_1].y);
			}
		}
	}

	// where is the barman and what is he doing?
	// draw the tap sprites accordingly!
	{
		int i;
		for (i = 0; i < 4; ++i)
		{
			// this tap is the one next to the barman
			if (i == barman_table_pos_idx)
			{
				if (beer_idx != -1 && barman_is_active == true)
				{
					// barman is filling a beer
					Sprite_Change(&tapSprt[i], &barmanImg,
						tap_frames[TAP_BARMAN_1].x,
						tap_frames[TAP_BARMAN_1].y);
				}
				else if (beer_idx != -1 && barman_is_active == false)
				{
					// barman is idle at the tap
					Sprite_Change(&tapSprt[i], &barmanImg,
						tap_frames[TAP_BARMAN_0].x,
						tap_frames[TAP_BARMAN_0].y);
				}
				else
				{
					// barman is doing nothing
					Sprite_Change(&tapSprt[i], &barmanImg,
						tap_frames[TAP_NORMAL].x,
						tap_frames[TAP_NORMAL].y);
				}
			}
			else
			{
				// tap is not where the barman stands
				Sprite_Change(&tapSprt[i], &barmanImg,
					tap_frames[TAP_NORMAL].x,
					tap_frames[TAP_NORMAL].y);
			}
		}
	}

	// listBeers(b_array);

	// Finished filling a beer? Position the beer
	// on the table and launch it!
	// NOTE: there must be at most one beer in B_BEINGFILLED status.
	// NOTE1: when the wait is over, the beer can be launched.
	// NOTE2: "filling_lag" can be global, because we only have one barman
	// 			and he can fill one beer mug at a time!
	if (
		(beer_idx != -1)  &&
		(gameDelays.filling_lag <= 0) &&
		(!IsPadPress(Pad1Cross))
	)
	{
		setBeerStatus(b_array[beer_idx], B_MOVING);
	}

#if 0 // DEBUG
		for (i = 0; i < MAX_BEERS; ++i)
		{
			if (b_array[i]->dbgstats != 0)
			{
				FntPrint(b_array[i]->dbgstats, "%p", b_array[i]);
				FntFlush(b_array[i]->dbgstats);
			}
		}
#endif

	err_cond->err_table = -1;
	err_cond->err_code = ERR_NONE;
	return true;
}
Ejemplo n.º 7
0
bool showMenu()
{
	static int menu_pos_idx = 0;

	struct menu_list menu[] = {
		{"NEW GAME", 	360, 40}, /* [x,y] coordinates */
		{"HIGH SCORES",	380, 50},
		{"QUIT",		400, 60},
	};

	enum
	{
		NEWGAME,
		HIGHSCORE,
		QUIT
	};

	// Show BG
	TextureDraw(&menu_bg, 0, 0);

	// manage cursor: it can only move if the timer is over
	if (gameDelays.input_wait == 0)
	{
		if (IsPadPress(Pad1Down))
		{
			gameDelays.input_wait = INPUT_LAG;
			if (menu_pos_idx < 3)
			{
				menu_pos_idx++;
			}
		}

		if (IsPadPress(Pad1Up))
		{
			gameDelays.input_wait = INPUT_LAG;
			if (menu_pos_idx > 0)
			{
				menu_pos_idx--;
			}
		}

		if (IsPadPress(Pad1Cross))
		{
			gameDelays.waiting_screen = WAITING_SCREEN;
			switch(menu_pos_idx)
			{
				case NEWGAME:
				{
					setGameStatus(GAME_PLAYING);
				}; break;

				// TODO
				case HIGHSCORE:
				case QUIT:
					break;
			}
		}
	}

	printMenuItems(menu, menu_pos_idx);

	return true;
}