Пример #1
0
bool Board::rotate()
{
    if(checkCollision2())
        return false;
    if(state == 3)
        state = 0;
    else
        ++state;

    switch(shape)
    {
        case 0:
            switch(state)
            {
                case 0:
                    --latitude[x+1];
                    latitude[x+2] = y+2;
                    break;
                case 1:
                    latitude[x] = -1;
                    ++latitude[x+1];
                    break;
                case 2:
                    latitude[x] = y+2;
                    break;
                case 3:
                    latitude[x+2] = -1;
                    break;
            }
            break;
        case 2:
            switch(state)
            {
			case 0: case 2:
                    latitude[x] = y+4;
                    latitude[x+1] = -1;
                    latitude[x+2] = -1;
                    latitude[x+3] = -1;
                    break;
                default:
                    latitude[x] = y+1;
                    latitude[x+1] = y+1;
                    latitude[x+2] = y+1;
                    latitude[x+3] = y+1;
                    break;
            }
            break;
        case 3:
            switch(state)
            {
			case 0: case 2:
                    latitude[x] = y+1;
                    latitude[x+1] = y+2;
                    latitude[x+2] = y+2;
                    break;
                default:
                    latitude[x] = y+3;
                    latitude[x+1] = y+2;
                    latitude[x+2] = -1;
                    break;
            }
            break;
        case 4:
            switch(state)
            {
			case 0: case 2:
                    --latitude[x+1];
                    latitude[x+2] = y+1;
                    break;
                default:
                    ++latitude[x+1];
                    latitude[x+2] = -1;
                    break;
            }
            break;
        case 5:
            switch(state)
            {
                case 0:
                    ++latitude[x];
                    ++latitude[x+1];
                    latitude[x+2] = -1;
                    break;
                case 1:
                    --latitude[x];
                    latitude[x+1] -= 2;
                    latitude[x+2] = y+1;
                    break;
                case 2:
                    --latitude[x];
                    latitude[x+1] += 2;
                    latitude[x+2] = -1;
                    break;
                case 3:
                    ++latitude[x];
                    --latitude[x+1];
                    latitude[x+2] = y+2;
                    break;
            }
            break;
        case 6:
            switch(state)
            {
                case 0:
                    latitude[x]+=2;
                    latitude[x+1]+=2;
                    latitude[x+2] = -1;
                    break;
                case 1:
                    --latitude[x];
                    --latitude[x+1];
                    latitude[x+2] = y+2;
                    break;
                case 2:
                    ++latitude[x];
                    --latitude[x+1];
                    latitude[x+2] = -1;
                    break;
                case 3:
                    latitude[x] -= 2;
                    latitude[x+2] = y+2;
                    break;
            }
            break;
		default:;
    }
    return true;
}
Пример #2
0
void Herding::update( float elapsedTime )
{
	for(auto p : players)
	{
		p->position += elapsedTime * 600 * p->joystick.leftStick;
		p->position = glm::vec2(glm::min(1920.0f, glm::max(0.0f, p->position.x)), glm::min(1080.0f, glm::max(0.0f, p->position.y)));
	}

	std::vector<glm::vec2> oldPositions = blib::linq::select<std::vector<glm::vec2> >(sheep, [] (Sheep* s) { return s->position; });
	for(auto s : sheep)
	{
		for(auto player : players)
		{
			glm::vec2 diff = player->position - s->position;
			float len = glm::length(diff);
			len = 200 - len;

			if (glm::length(diff) < 200 && glm::length(diff) > 0)
			{
				diff = glm::normalize(diff);
				s->position -= len * diff;
				s->position = glm::vec2(glm::min(1920.0f, glm::max(0.0f, s->position.x)), glm::min(1080.0f, glm::max(0.0f, s->position.y)));
				checkCollision(s);
			}
			s->position = glm::vec2(glm::min(1920.0f, glm::max(0.0f, s->position.x)), glm::min(1080.0f, glm::max(0.0f, s->position.y)));
			checkCollision(s);
		}
		glm::vec2 direction = glm::vec2(0,0);
		for(auto ss : sheep)
		{
			if (s == ss)
				continue;
			glm::vec2 diff = ss->position - s->position;
			direction += diff;
		}

		direction = glm::normalize(direction);
		if (glm::length(direction) > 0.1)
		{
			direction = glm::normalize(direction);
			glm::vec2 oldPos = s->position;
			s->position += 60 * elapsedTime * direction * 2.0f;

			if (checkCollision2(s))
				s->position = oldPos;
		}
	}

	for (size_t i = 0; i < sheep.size(); i++)
	{
		glm::vec2 diff = oldPositions[i] - sheep[i]->position;
		if (glm::length(diff) > 0.01)
		{
			sheep[i]->direction = 0.9f * sheep[i]->direction + 0.1f * (float)atan2(diff.y, diff.x);
			sheep[i]->moveFrame += 0.1f + (float)elapsedTime * 5 * glm::length(diff);
			while (sheep[i]->moveFrame > 4)
				sheep[i]->moveFrame -= 4;
		}
		else
			sheep[i]->moveFrame = 0;
	}

	for (int i = sheep.size() - 1; i >= 0; i--)
	{
		HerdingPlayer* taken = NULL;
		for (auto player : players)
			if (glm::length(sheep[i]->position - player->basePosition) < 250)
				taken = player;

		if (taken != NULL)
		{
			taken->score++;
			sheep.erase(sheep.begin() + i);
		}
	}
	std::sort(sheep.begin(), sheep.end(), [] (Sheep* a, Sheep* b) { return a->position.y < b->position.y; });
}
Пример #3
0
bool Board::goDown()
{
    if(checkCollision1())
    {
        switch(shape)
        {
            case 0:
                switch(state)
                {
                    case 0:
						slots[y][x+1].isOccupied = slots[y+1][x].isOccupied =
							slots[y+1][x+1].isOccupied = slots[y+1][x+2].isOccupied = true;
						slots[y][x+1].color = slots[y+1][x].color =
							slots[y+1][x+1].color = slots[y+1][x+2].color = 0;
                        break;
                    case 1:
						slots[y][x+1].isOccupied = slots[y+1][x+1].isOccupied =
							slots[y+2][x+1].isOccupied = slots[y+1][x+2].isOccupied = true;
						slots[y][x+1].color = slots[y+1][x+1].color =
							slots[y+2][x+1].color = slots[y+1][x+2].color = 0;
                        break;
                    case 2:
						slots[y+1][x].isOccupied = slots[y+1][x+1].isOccupied =
							slots[y+1][x+2].isOccupied = slots[y+2][x+1].isOccupied = true;
						slots[y+1][x].color = slots[y+1][x+1].color =
							slots[y+1][x+2].color = slots[y+2][x+1].color = 0;
                        break;
                    default:
						slots[y+1][x].isOccupied = slots[y][x+1].isOccupied =
							slots[y+1][x+1].isOccupied = slots[y+2][x+1].isOccupied = true;
						slots[y+1][x].color = slots[y][x+1].color =
							slots[y+1][x+1].color = slots[y+2][x+1].color = 0;
                }
                break;
            case 1:
				slots[y][x].isOccupied = slots[y+1][x].isOccupied =
					slots[y][x+1].isOccupied = slots[y+1][x+1].isOccupied = true;
				slots[y][x].color = slots[y+1][x].color =
					slots[y][x+1].color = slots[y+1][x+1].color = 1;
                break;
            case 2:
                switch(state)
                {
				case 0:case 2:
					slots[y][x].isOccupied = slots[y+1][x].isOccupied =
							slots[y+2][x].isOccupied = slots[y+3][x].isOccupied = true;
					slots[y][x].color = slots[y+1][x].color =
						slots[y+2][x].color = slots[y+3][x].color = 2;
                        break;
                    default:
						slots[y][x+1].isOccupied = slots[y][x].isOccupied =
							slots[y][x+2].isOccupied = slots[y][x+3].isOccupied = true;
						slots[y][x+1].color = slots[y][x].color =
							slots[y][x+2].color = slots[y][x+3].color = 2;
                }
                break;
            case 3:
                switch(state)
                {
				case 0: case 2:
					slots[y][x].isOccupied = slots[y][x+1].isOccupied =
						slots[y+1][x+1].isOccupied = slots[y+1][x+2].isOccupied = true;
					slots[y][x].color = slots[y][x+1].color =
						slots[y+1][x+1].color = slots[y+1][x+2].color = 3;
                        break;
                    default:
						slots[y][x+1].isOccupied = slots[y+1][x].isOccupied =
							slots[y+1][x+1].isOccupied = slots[y+2][x].isOccupied = true;
						slots[y][x+1].color = slots[y+1][x].color =
							slots[y+1][x+1].color = slots[y+2][x].color = 3;
                }
                break;
            case 4:
                switch(state)
                {
				case 0: case 2:
					slots[y][x+1].isOccupied = slots[y][x+2].isOccupied =
						slots[y+1][x].isOccupied = slots[y+1][x+1].isOccupied = true;
					slots[y][x+1].color = slots[y][x+2].color =
						slots[y+1][x].color = slots[y+1][x+1].color = 4;
                        break;
                    default:
						slots[y][x].isOccupied = slots[y+1][x].isOccupied =
							slots[y+1][x+1].isOccupied = slots[y+2][x+1].isOccupied = true;
						slots[y][x].color = slots[y+1][x].color =
							slots[y+1][x+1].color = slots[y+2][x+1].color = 4;
                }
                break;
            case 5:
                switch(state)
                {
                    case 0:
						slots[y][x].isOccupied = slots[y+1][x].isOccupied =
							slots[y+2][x].isOccupied = slots[y+2][x+1].isOccupied = true;
						slots[y][x].color = slots[y+1][x].color =
							slots[y+2][x].color = slots[y+2][x+1].color = 5;
                        break;
                    case 1:
						slots[y][x].isOccupied = slots[y][x+1].isOccupied =
							slots[y][x+2].isOccupied = slots[y+1][x].isOccupied = true;
						slots[y][x].color = slots[y][x+1].color =
							slots[y][x+2].color = slots[y+1][x].color = 5;
                        break;
                    case 2:
						slots[y][x].isOccupied = slots[y][x+1].isOccupied =
							slots[y+1][x+1].isOccupied = slots[y+2][x+1].isOccupied = true;
						slots[y][x].color = slots[y][x+1].color =
							slots[y+1][x+1].color = slots[y+2][x+1].color = 5;
                        break;
                    default:
						slots[y][x+2].isOccupied = slots[y+1][x].isOccupied =
							slots[y+1][x+1].isOccupied = slots[y+1][x+2].isOccupied = true;
						slots[y][x+2].color = slots[y+1][x].color =
							slots[y+1][x+1].color = slots[y+1][x+2].color = 5;
                }
                break;
            case 6:
                switch(state)
                {
                    case 0:
						slots[y][x+1].isOccupied = slots[y+1][x+1].isOccupied =
							slots[y+2][x].isOccupied = slots[y+2][x+1].isOccupied = true;
						slots[y][x+1].color = slots[y+1][x+1].color =
							slots[y+2][x].color = slots[y+2][x+1].color = 6;
                        break;
                    case 1:
						slots[y][x].isOccupied = slots[y+1][x].isOccupied =
							slots[y+1][x+1].isOccupied = slots[y+1][x+2].isOccupied = true;
						slots[y][x].color = slots[y+1][x].color =
							slots[y+1][x+1].color = slots[y+1][x+2].color = 6;
                        break;
                    case 2:
						slots[y][x].isOccupied = slots[y][x+1].isOccupied =
							slots[y+1][x].isOccupied = slots[y+2][x].isOccupied = true;
						slots[y][x].color = slots[y][x+1].color =
							slots[y+1][x].color = slots[y+2][x].color = 6;
                        break;
                    default:
						slots[y][x].isOccupied = slots[y][x+1].isOccupied =
							slots[y][x+2].isOccupied = slots[y+1][x+2].isOccupied = true;
						slots[y][x].color = slots[y][x+1].color =
							slots[y][x+2].color = slots[y+1][x+2].color = 6;
                }
                break;
            default:;
        }

        checkLine();
        //srand(time(NULL));
        shape = rand() % 7;
        state = 0;
        for(int i=0; i<10; ++i)
            latitude[i] = -1;

        if(shape == 0 || shape == 3 || shape == 4)
            x = 3;
        else
            x = 4;
		y=0;
        switch(shape)
        {
            case 0: latitude[3] = latitude[4] = latitude[5] = 2;
			if(checkCollision1() || checkCollision2())
			{
				apply_surface(0,0,gameOver,screen);
				//Render the text
				char buffer[4];
				buffer[3] = '/n';
				gameOverMessage = TTF_RenderText_Solid( gameOverFont, itoa(score,buffer,10), gameOverFontTextColor );

				//If there was an error in rendering the text
				if( gameOverMessage == NULL )
				{
					std::cerr << SDL_GetError() << std::endl;
					exit(1);
			    }
				apply_surface(140,272,gameOverMessage,screen);
				SDL_Flip(screen);
				SDL_Delay(10000);
				exit(0);
			}
				break;
        case 1: latitude[4] = latitude[5] = 2;
			if(checkCollision1() || checkCollision2())
			{
				apply_surface(0,0,gameOver,screen);
				//Render the text
				char buffer[4];
				buffer[3] = '/n';
				gameOverMessage = TTF_RenderText_Solid( gameOverFont, itoa(score,buffer,10), gameOverFontTextColor );

				//If there was an error in rendering the text
				if( gameOverMessage == NULL )
				{
					std::cerr << SDL_GetError() << std::endl;
					exit(1);
			    }
				apply_surface(140,272,gameOverMessage,screen);
				SDL_Flip(screen);
				SDL_Delay(10000);
				exit(0);
			}
			break;
        case 2: latitude[4] = 4;
			if(checkCollision1() || checkCollision2())
			{
				apply_surface(0,0,gameOver,screen);
				//Render the text
				char buffer[4];
				buffer[3] = '/n';
				gameOverMessage = TTF_RenderText_Solid( gameOverFont, itoa(score,buffer,10), gameOverFontTextColor );

				//If there was an error in rendering the text
				if( gameOverMessage == NULL )
				{
					std::cerr << SDL_GetError() << std::endl;
					exit(1);
			    }
				apply_surface(140,272,gameOverMessage,screen);
				SDL_Flip(screen);
				SDL_Delay(10000);
				exit(0);
			}
			break;
        case 3: latitude[3] = 1; latitude[4] = latitude[5] = 2;
			if(checkCollision1() || checkCollision2())
			{
				apply_surface(0,0,gameOver,screen);
				//Render the text
				char buffer[4];
				buffer[3] = '/n';
				gameOverMessage = TTF_RenderText_Solid( gameOverFont, itoa(score,buffer,10), gameOverFontTextColor );

				//If there was an error in rendering the text
				if( gameOverMessage == NULL )
				{
					std::cerr << SDL_GetError() << std::endl;
					exit(1);
			    }
				apply_surface(140,272,gameOverMessage,screen);
				SDL_Flip(screen);
				SDL_Delay(10000);
				exit(0);
			}
			break;
        case 4: latitude[3] = latitude[4] = 2; latitude[5] = 1;
			if(checkCollision1() || checkCollision2())
			{
				apply_surface(0,0,gameOver,screen);
				//Render the text
				char buffer[4];
				buffer[3] = '/n';
				gameOverMessage = TTF_RenderText_Solid( gameOverFont, itoa(score,buffer,10), gameOverFontTextColor );

				//If there was an error in rendering the text
				if( gameOverMessage == NULL )
				{
					std::cerr << SDL_GetError() << std::endl;
					exit(1);
			    }
				apply_surface(140,272,gameOverMessage,screen);
				SDL_Flip(screen);
				SDL_Delay(10000);
				exit(0);
			}
			break;
        case 5: case 6: latitude[4] = latitude[5] = 3;
			if(checkCollision1() || checkCollision2())
			{
				apply_surface(0,0,gameOver,screen);
				//Render the text
				char buffer[4];
				buffer[3] = '/n';
				gameOverMessage = TTF_RenderText_Solid( gameOverFont, itoa(score,buffer,10), gameOverFontTextColor );

				//If there was an error in rendering the text
				if( gameOverMessage == NULL )
				{
					std::cerr << SDL_GetError() << std::endl;
					exit(1);
			    }
				apply_surface(140,272,gameOverMessage,screen);
				SDL_Flip(screen);
				SDL_Delay(10000);
				exit(0);
			}
			break;
        default:;
        }


        return false;
    }
    for(int i = 0;i < 10;++i)
        if(latitude[i] != -1)
            ++latitude[i];
    ++y;
	return true;
}