Ejemplo n.º 1
0
void BattleGesture::TouchEnded(Touch* touch)
{
	if (touch == _trackingTouch)
	{
		if (_trackingMarker != nullptr)
		{
			Unit* unit = _trackingMarker->GetUnit();
			UnitCommand command;

			command.path = _trackingMarker->_path;
			command.running = _trackingMarker->GetRunning();
			command.meleeTarget = _trackingMarker->GetMeleeTarget();

			Unit* missileTarget = _trackingMarker->GetMissileTarget();
			glm::vec2* orientation = _trackingMarker->GetOrientationX();

			if (missileTarget != nullptr)
			{
				command.missileTarget = missileTarget;
				command.missileTargetLocked = true;
				if (missileTarget != unit)
					command.bearing = angle(missileTarget->state.center - command.GetDestination());
				unit->state.loadingTimer = 0;
			}
			else if (orientation)
			{
				command.bearing = angle(*orientation - command.GetDestination());
			}

			if (!touch->HasMoved())
			{
				if (_tappedUnitCenter && touch->GetTapCount() > 1)
				{
					command.meleeTarget = nullptr;
					command.ClearPathAndSetDestination(unit->state.center);
					command.missileTarget = nullptr;
					command.missileTargetLocked = false;
				}
				else if (_tappedDestination && !_tappedUnitCenter)
				{
					if (!command.running)
					{
						command.running = true;
					}
				}
				else if (_tappedUnitCenter && !_tappedDestination)
				{
					if (command.running)
					{
						command.running = false;
					}
				}
			}

			unit->timeUntilSwapFighters = 0.2f;

			_battleView->RemoveTrackingMarker(_trackingMarker);
			_trackingMarker = nullptr;

			_battleView->GetSimulator()->SetUnitCommand(unit, command, _battleView->GetSimulator()->GetTimerDelay());

			if (touch->GetTapCount() == 1)
				SoundPlayer::singleton->Play(SoundBufferCommandAck);

		}
	}


	if (touch == _modifierTouch && _trackingTouch != nullptr)
	{
		_trackingTouch->ResetHasMoved();
	}


	if (_trackingTouch != nullptr && _modifierTouch != nullptr)
	{
		_trackingTouch->ResetVelocity();
		_modifierTouch->ResetVelocity();
	}


	/*if (_trackingTouch == nullptr && _modifierTouch != nullptr)
	{
		if (!ViewState::singleton->showTitleScreen)
		{
			vector2 velocity = touch->GetVelocity();
			float speed = norm(velocity) - 1;
			if (speed < 0)
				speed = 0;
			else if (speed > 10)
				speed = 10;
			velocity = vector2::from_polar(velocity.angle(), speed);

			float k = SamuraiSurface::singleton->renderLayer.actualWidth / 2;
			if (SamuraiSurface::singleton->renderLayer.flip)
				k = -k;

			BoardGesture::scrollVelocity = velocity * k;
		}
	}*/


	if (touch == _trackingTouch)
	{
		_trackingTouch = nullptr;
	}
	else if (touch == _modifierTouch)
	{
		_modifierTouch = nullptr;
	}
}
Ejemplo n.º 2
0
void BattleGesture::TouchBegan(Touch* touch)
{
	if (touch->GetSurface() != _battleView->GetSurface())
		return;
	if (touch->HasGesture())
		return;
	if (!_battleView->GetFrame().contains(touch->GetPosition()))
		return;
	if (_battleView->GetCommander() == nullptr)
		return;
	if (_battleView->GetCommander()->GetType() != BattleCommanderType::Player)
		return;
	if (!_battleView->GetCommander()->IsActive())
		return;

	glm::vec2 screenPosition = touch->GetPosition();
	glm::vec2 terrainPosition = _battleView->GetTerrainPosition3(screenPosition).xy();
	Unit* unit = FindPlayerUnit(screenPosition, terrainPosition);

	if (_trackingTouch == nullptr)
	{
		if (unit == nullptr)
			return;

		if (unit != nullptr && _battleView->GetTrackingMarker(unit) == nullptr)
		{
			const UnitCommand command = unit->GetCommand();

			_allowTargetEnemyUnit = unit->stats.missileType != MissileType::None;
			_trackingMarker = _battleView->AddTrackingMarker(unit);

			float distanceToUnitCenter = glm::distance(GetUnitCurrentBounds(unit).center(), screenPosition);
			float distanceToDestination = glm::distance(GetUnitFutureBounds(unit).center(), screenPosition);
			float distanceToModifierArea = glm::distance(GetUnitModifierBounds(unit).center(), screenPosition);
			float distanceMinimum = glm::min(distanceToUnitCenter, glm::min(distanceToDestination, distanceToModifierArea));

			_tappedUnitCenter = distanceToUnitCenter == distanceMinimum;
			_tappedDestination = distanceToDestination == distanceMinimum && !_tappedUnitCenter;
			_tappedModiferArea = distanceToModifierArea == distanceMinimum && !_tappedUnitCenter && !_tappedDestination;

			if (_tappedDestination || _tappedModiferArea)
			{
				_offsetToMarker = 0;//(_boardView->ContentToScreen(vector3(unit->movement.GetFinalDestination(), 0)).y - _boardView->ContentToScreen(vector3(terrainPosition, 0)).y) * GetFlipSign();
				if (_offsetToMarker < 0)
					_offsetToMarker = 0;

				std::vector<glm::vec2>& path = _trackingMarker->_path;
				path.clear();
				path.insert(path.begin(), command.path.begin(), command.path.end());

				glm::vec2 orientation = command.GetDestination() + 18.0f * vector2_from_angle(command.bearing);
				_trackingMarker->SetOrientation(&orientation);
			}
			else
			{
				_offsetToMarker = 0;//(_boardView->ContentToScreen(vector3(unit->state.center, 0)).y - _boardView->ContentToScreen(vector3(terrainPosition, 0)).y) * GetFlipSign();
				if (_offsetToMarker < 0)
					_offsetToMarker = 0;

				glm::vec2 orientation = unit->state.center + 18.0f * vector2_from_angle(unit->state.bearing);
				_trackingMarker->SetOrientation(&orientation);
			}

			if (touch->GetTapCount() > 1 && _tappedUnitCenter && !_tappedDestination)
			{
				UnitCommand command;

				command.ClearPathAndSetDestination(unit->state.center);

				_battleView->GetSimulator()->SetUnitCommand(unit, command, _battleView->GetSimulator()->GetTimerDelay());
			}

			_trackingMarker->SetRunning(touch->GetTapCount() > 1 || (!_tappedUnitCenter && command.running));

			CaptureTouch(touch);
			_trackingTouch = touch;
		}
		else if (_modifierTouch == nullptr)
		{
			CaptureTouch(touch);
			_modifierTouch = touch;
		}
		else
		{
			CaptureTouch(touch);
			_trackingTouch = touch;
		}
	}
	else if (_modifierTouch == nullptr)
	{
		if (unit != nullptr)
			return;

		if (_gestures != nullptr)
		{
			for (Gesture* g : *_gestures)
			{
				BattleGesture* gesture = dynamic_cast<BattleGesture*>(g);
				if (gesture != nullptr && gesture != this && gesture->_trackingTouch != nullptr)
				{
					if (glm::length(_trackingTouch->GetPosition() - touch->GetPosition()) > glm::length(gesture->_trackingTouch->GetPosition() - touch->GetPosition()))
						return;
				}
			}
		}

		CaptureTouch(touch);
		_modifierTouch = touch;
	}
}