using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewPlayerScript : NewCharacterScript
{
public CameraScript playerCamera;
private TrackedTouch trackedTouch = new TrackedTouch();
private float killStreakTimeOut = 0f;
private const float killStreakInterval = 7.0f;
private int killStreak = 0;
private float vibroTimeOut = 0;
private float vibroTimeInterval = 2.0f;
private int lostedWeapons = 0;
private float maxCameraOffset = 2.5f;
private float cameraOffsetFactor = 20f;
#if UNITY_EDITOR
private float activationResurrectAbilityTime; // время с начала раунда, когда включается возможность воскрешаться
#else
private float activationResurrectAbilityTime = 10f; // время с начала раунда, когда включается возможность воскрешаться
#endif
private int resurrectQuantity = 1; // количество возможных воскрешений
private ResurrectModel resurrectModel;
// Achivements
private float zeroSwordsTimer = 0f;
private float fiveKillsInOneMinuteTimer = 0f;
private bool fiveKillsInOneMinuteStarted = false;
private int fiveKillsInOneMinuteAchiveKillCounter = 0;
private float fiveKillsInThirtySecondsTimer = 0f;
private bool fiveKillsInThirtySecondsStarted = false;
private int fiveKillsInThirtySecondsAchiveKillCounter = 0;
private bool is140SwordsCollected = false;
private float rotatingFactor = 0f;
// NEW MOVING SYSTEM
private bool hasMoving = false;
private Vector2 currentJoy = new Vector2();
private Vector2 previousPos = new Vector2();
public override void Start()
{
base.Start();
GameObject.Find("Canvas").GetComponent<Canvas>().worldCamera = playerCamera.gameObject.GetComponent<Camera>();
GameObject.Find("Canvas").GetComponent<Canvas>().sortingLayerName = "UI";
List <Loyalty> loyalties = LoyaltyManager.GetAvaiableLoyalties();
loyalties.ForEach((loyalty) =>
{
if (loyalty.enabled)
{
switch (loyalty.type)
{
case LoyaltyType.BonusSpeed:
bonusPercentSpeed += loyalty.loyaltyValue / 100f;
break;
}
}
});
AppendToMaxHealth();
damageModel = new DamageHandler(this);
}
public override void Update()
{
base.Update();
if (isDead || ThinkToResurrect)
return;
var weaponCount = GetWeaponsCount();
var cameraOffset = weaponCount / cameraOffsetFactor;
cameraOffset = cameraOffset > maxCameraOffset ? maxCameraOffset : cameraOffset;
playerCamera.offsetByWeaponCount = cameraOffset;
if (GetWeaponsCount() > 0)
zeroSwordsTimer = 0f;
else
zeroSwordsTimer += Time.deltaTime;
if(zeroSwordsTimer > 60f)
{
QuestsManager.instance.UpdateQuests(QuestType.OneMinuteWithoutKnives, 1);
zeroSwordsTimer = 0f;
}
// 5 kills in 30 seconds
if (fiveKillsInThirtySecondsTimer > 30f)
fiveKillsInThirtySecondsStarted = false;
if (fiveKillsInThirtySecondsStarted)
{
fiveKillsInThirtySecondsTimer += Time.deltaTime;
if (fiveKillsInThirtySecondsAchiveKillCounter >= 5)
{
SharedProperties.IncFiveKillsInThirtySecondsCount();
// Reset
fiveKillsInThirtySecondsStarted = false;
fiveKillsInThirtySecondsTimer = 0;
fiveKillsInThirtySecondsAchiveKillCounter = 0;
}
}
else
{
fiveKillsInThirtySecondsTimer = 0;
fiveKillsInThirtySecondsAchiveKillCounter = 0;
}
// 5 kills in 1 minute
if (fiveKillsInOneMinuteTimer > 60f)
fiveKillsInOneMinuteStarted = false;
if (fiveKillsInOneMinuteStarted)
{
fiveKillsInOneMinuteTimer += Time.deltaTime;
if(fiveKillsInOneMinuteAchiveKillCounter >= 5)
{
SharedProperties.IncFiveKillsInOneMinuteScoreCount();
// Reset
fiveKillsInOneMinuteStarted = false;
fiveKillsInOneMinuteTimer = 0;
fiveKillsInOneMinuteAchiveKillCounter = 0;
}
}
else
{
fiveKillsInOneMinuteTimer = 0;
fiveKillsInOneMinuteAchiveKillCounter = 0;
}
vibroTimeOut -= Time.deltaTime;
// #if UNITY_EDITOR
// targetPosition = transform.position + Vector3.up * Input.GetAxis("Vertical") + Vector3.right * Input.GetAxis("Horizontal");
// if(Input.GetKey(KeyCode.LeftShift))
// {
// bonusPercentSpeed = 2;
// } else
// {
// bonusPercentSpeed = 1;
// }
// #else
if (Input.touchCount > 0 && !gameHandler.IsGameEneded())
{
var touch = Input.GetTouch(0);
switch (touch.phase)
{
case TouchPhase.Began:
trackedTouch.startPos = touch.position;
trackedTouch.currentPos = touch.position;
isForceUndefence = true;
break;
case TouchPhase.Stationary:
case TouchPhase.Moved:
trackedTouch.currentPos = touch.position;
// Vector2 screenJoy = trackedTouch.currentPos - trackedTouch.startPos;
// screenJoy.x = Mathf.Clamp(screenJoy.x / (Screen.width * 0.5f), -1f, 1f);
// screenJoy.y = Mathf.Clamp(screenJoy.y / (Screen.height * 0.5f), -1f, 1f);
//
// targetTranslate = new Vector3(screenJoy.x, screenJoy.y, 0);
//
// targetPosition = new Vector3(transform.position.x + screenJoy.x, transform.position.y + screenJoy.y, 0);
//
// isForceUndefence = true;
// --= new system of movement =--
// Vector2 screenJoy = trackedTouch.currentPos - trackedTouch.startPos;
Vector2 screenJoy = new Vector2();
if (trackedTouch.currentPos != (previousPos != Vector2.zero ? previousPos : trackedTouch.startPos))
{
screenJoy = trackedTouch.currentPos -
(previousPos != Vector2.zero ? previousPos : trackedTouch.startPos);
}
if (hasMoving)
{
if (trackedTouch.currentPos == previousPos)
{
// сохраняем screenJoy
}
else
{
hasMoving = true;
screenJoy.x = Mathf.Clamp(screenJoy.x / (Screen.width * 0.5f), -1f, 1f);
screenJoy.y = Mathf.Clamp(screenJoy.y / (Screen.height * 0.5f), -1f, 1f);
}
}
else
{
if (trackedTouch.currentPos != previousPos && trackedTouch.currentPos != touch.position)
{
hasMoving = true;
screenJoy.x = Mathf.Clamp(screenJoy.x / (Screen.width * 0.5f), -1f, 1f);
screenJoy.y = Mathf.Clamp(screenJoy.y / (Screen.height * 0.5f), -1f, 1f);
}
}
targetTranslate = new Vector3(screenJoy.x, screenJoy.y, 0); // old
targetPosition = new Vector3(transform.position.x + screenJoy.x, transform.position.y + screenJoy.y, 0); // old
isForceUndefence = true; // old
previousPos = previousPos == Vector2.zero ? trackedTouch.startPos : trackedTouch.currentPos; // new
break;
case TouchPhase.Canceled:
case TouchPhase.Ended:
targetPosition = transform.position;
targetTranslate = Vector3.zero;
isForceUndefence = false;
hasMoving = false;
previousPos = Vector2.zero;
break;
}
}
else
{
targetPosition = transform.position;
}
// #endif
if (killStreakTimeOut > 0)
{
killStreakTimeOut -= Time.deltaTime;
}
else
{
killStreak = 0;
}
rotatingFactor = targetTranslate.magnitude / Mathf.Sqrt(2);
spriteHolder.transform.rotation = Quaternion.RotateTowards(spriteHolder.transform.rotation, Quaternion.Euler(0, 0, spriteAngle * rotatingFactor), selfRotatingSpeed * Mathf.Deg2Rad);
}
protected override void OnPauseEnabled(bool value)
{
base.OnPauseEnabled(value);
}
protected override void MoveToTargetPosition(float deltaTime)
{
base.MoveToTargetPosition(deltaTime);
isDefence = false;
if (playerCamera != null)
{
playerCamera.bigSize = 3.5f;
}
}
public override void DropWeapon(NewRazerScript weapon)
{
base.DropWeapon(weapon);
playerCamera.Shake(0.05f, 0.2f);
if (vibroTimeOut < 0)
{
if (SharedProperties.IsVibroOn() && !gameHandler.isNeedToDisableSounds)
{
#if UNITY_EDITOR
#elif UNITY_ANDROID
Vibration.Vibrate(10);
#elif UNITY_IOS
Vibration.VibratePop();
#endif
}
lostedWeapons = 0;
}
else if (lostedWeapons < 3 || lostedWeapons % 3 == 0)
{
if (SharedProperties.IsVibroOn() && !gameHandler.isNeedToDisableSounds)
{
#if UNITY_EDITOR
#elif UNITY_ANDROID
Vibration.Vibrate(10);
#elif UNITY_IOS
Vibration.VibratePop();
#endif
}
}
lostedWeapons += 1;
vibroTimeOut = vibroTimeInterval;
}
public override void AppendVictim(GameObject victim)
{
fiveKillsInOneMinuteStarted = true;
fiveKillsInOneMinuteAchiveKillCounter++;
fiveKillsInThirtySecondsStarted = true;
fiveKillsInThirtySecondsAchiveKillCounter++;
playerCamera.Shake(0.5f, 0.5f);
if (SharedProperties.IsVibroOn() && !gameHandler.isNeedToDisableSounds)
{
#if UNITY_EDITOR
#elif UNITY_ANDROID
Vibration.Vibrate(50);
#elif UNITY_IOS
Vibration.VibratePeek();
#endif
}
if (!victims.Contains(victim))
{
killStreak += 1;
killStreakTimeOut = killStreakInterval;
gameHandler.HandlePlayerKillStreak(killStreak);
if(isDefence)
{
QuestsManager.instance.UpdateQuests(QuestType.KillInDefence, 1);
}
}
base.AppendVictim(victim);
}
protected override void Defence()
{
base.Defence();
isDefence = true;
if (playerCamera != null)
{
playerCamera.bigSize = 0f;
}
}
public void Init()
{
float startTime = Time.time;
resurrectModel = new ResurrectModel(startTime, activationResurrectAbilityTime, resurrectQuantity);
}
protected override void HandleDeath(bool canResurrect)
{
if (ThinkToResurrect) return;
base.HandleDeath(resurrectModel.CanResurrect);
playerCamera.Shake(0.5f, 1f);
if (SharedProperties.IsVibroOn() && !gameHandler.isNeedToDisableSounds)
{
#if UNITY_EDITOR
#elif UNITY_ANDROID
Vibration.Vibrate(100);
#elif UNITY_IOS
Vibration.VibrateNope();
#endif
}
if (resurrectModel.CanResurrect)
{
resurrectModel.Resurrect();
ThinkToResurrect = true;
}
else
{
deathAnimator.Play();
}
}
public override void ZoomInCamera()
{
base.ZoomInCamera();
float scale = transform.localScale.x;
if (scale <= minScale)
{
return;
}
playerCamera.targetSize -= 0.05f;
}
public override void ZoomOutCamera()
{
base.ZoomOutCamera();
float scale = transform.localScale.x;
if (scale >= maxScale)
{
return;
}
playerCamera.targetSize += 0.05f;
}
public void SetUiModel(UnitUIModel model)
{
uiModel = model;
foreach (var weapon in weaponCollection)
{
weapon.SetUnitUIModel(uiModel);
}
}
private class TrackedTouch
{
public Vector2 startPos;
public Vector2 currentPos;
}
public override void AppendWeapon(NewRazerScript objectScript)
{
base.AppendWeapon(objectScript);
QuestsManager.instance.UpdateQuests(QuestType.CollectKnivesTotal, 1);
if(GetWeaponsCount() >= 140 && !is140SwordsCollected)
{
is140SwordsCollected = true;
SharedProperties.IncCollect140SwordsCount();
}
}
protected override void GetHaste(HasteScript hasteObject)
{
float tHasteDuration = hasteObject.hasteDuration + hasteObject.hasteDuration * bonusHasteDuration;
gameHandler.PlayerGotHaste(tHasteDuration);
base.GetHaste(hasteObject);
}
protected override void ApplySpell(SpellModel spell)
{
base.ApplySpell(spell);
if (TutorialManager.chanceStayAlive > 0f)
{
deflectionBonus = TutorialManager.chanceStayAlive / 100f;
}
}
}