void RunUp(float mnozhitel_speed) { isFirstAttack = true; //Чтобы после бега/ходьбы была проиграна первая атака currentEnergy -= mnozhitel_speed == 2 ? 2 * expenseEnergy : expenseEnergy; currentEnergy = currentEnergy < 0.0F ? 0.0F : currentEnergy; //userData.ggData.stats.Set(Stats.Key.ENERGY, currentEnergy); //Останавливаем анимацию, после чего определяем направление движения и затем осуществляем нужную анимацию и движение. //ВОЗМОЖНО, можно перенести на две разные функции, чтобы был механизм похожий на функцию Wake(...) animator.StopPlayback(); direction = transform.up * Input.GetAxis("Vertical"); if (direction.y > 0.0F) { State = GGState.RunBack; //Vector2 PointA = new Vector2(transform.position.x - (float)0.15, transform.position.y + (float)0.4); //Vector2 PointB = new Vector2(transform.position.x + (float)0.15, transform.position.y + (float)0.3); //Collider2D[] colliders = Physics2D.OverlapAreaAll(PointA, PointB); //if (colliders.Length == 0) transform.position = Vector3.MoveTowards(transform.position, transform.position + direction, mnozhitel_speed * speed.y * Time.deltaTime); transform.position = Vector3.MoveTowards(transform.position, transform.position + direction, mnozhitel_speed * speed.y * Time.deltaTime); } else { State = GGState.RunFront; //Vector2 PointA = new Vector2(transform.position.x - (float)0.15, transform.position.y - (float)0.4); //Vector2 PointB = new Vector2(transform.position.x + (float)0.15, transform.position.y - (float)0.3); //Collider2D[] colliders = Physics2D.OverlapAreaAll(PointA, PointB); //if (colliders.Length == 0) transform.position = Vector3.MoveTowards(transform.position, transform.position + direction, mnozhitel_speed * speed.y * Time.deltaTime); transform.position = Vector3.MoveTowards(transform.position, transform.position + direction, mnozhitel_speed * speed.y * Time.deltaTime); } }
void RunSide(float mnozhitel_speed) { isFirstAttack = true; //Чтобы после бега/ходьбы была проиграна первая атака currentEnergy -= mnozhitel_speed == 2 ? 2 * expenseEnergy : expenseEnergy; currentEnergy = currentEnergy < 0.0F ? 0.0F : currentEnergy; //userData.ggData.stats.Set(Stats.Key.ENERGY, currentEnergy); //Так как одна анимация на два случая жизни, то сразу отображаем анимацию, а потом осуществляем ходьбу. State = GGState.RunSide; direction = transform.right * Input.GetAxis("Horizontal"); if (direction.x < 0.0F) { //Vector2 PointA = new Vector2(transform.position.x - (float)0.22, transform.position.y + (float)0.3); //Vector2 PointB = new Vector2(transform.position.x - (float)0.15, transform.position.y - (float)0.3); //Collider2D[] colliders = Physics2D.OverlapAreaAll(PointA, PointB); //if(colliders.Length == 0) transform.position = Vector3.MoveTowards(transform.position, transform.position + direction, mnozhitel_speed * speed.x * Time.deltaTime); transform.position = Vector3.MoveTowards(transform.position, transform.position + direction, mnozhitel_speed * speed.x * Time.deltaTime); } else { //Vector2 PointA = new Vector2(transform.position.x + (float)0.22, transform.position.y + (float)0.3); //Vector2 PointB = new Vector2(transform.position.x + (float)0.15, transform.position.y - (float)0.3); //Collider2D[] colliders = Physics2D.OverlapAreaAll(PointA, PointB); //if (colliders.Length == 0) transform.position = Vector3.MoveTowards(transform.position, transform.position + direction, mnozhitel_speed * speed.x * Time.deltaTime); transform.position = Vector3.MoveTowards(transform.position, transform.position + direction, mnozhitel_speed * speed.x * Time.deltaTime); } sprite.flipX = direction.x < 0.0F; }
void RunRight2(bool right, float mnozhitel_speed) { isFirstAttack = true; //Чтобы после бега/ходьбы была проиграна первая атака currentEnergy -= mnozhitel_speed == 2 ? 2 * expenseEnergy : expenseEnergy; currentEnergy = currentEnergy < 0.0F ? 0.0F : currentEnergy; State = GGState.RunSide; direction = transform.right; if (!right) { direction *= -1.0f; transform.position = Vector3.MoveTowards(transform.position, transform.position + direction, mnozhitel_speed * speed.x * Time.deltaTime); } else { transform.position = Vector3.MoveTowards(transform.position, transform.position + direction, mnozhitel_speed * speed.x * Time.deltaTime); } sprite.flipX = !right; }
void RunUp2(bool up, float mnozhitel_speed) { isFirstAttack = true; //Чтобы после бега/ходьбы была проиграна первая атака currentEnergy -= mnozhitel_speed == 2 ? 2 * expenseEnergy : expenseEnergy; currentEnergy = currentEnergy < 0.0F ? 0.0F : currentEnergy; animator.StopPlayback(); direction = transform.up; if (up) { State = GGState.RunBack; transform.position = Vector3.MoveTowards(transform.position, transform.position + direction, mnozhitel_speed * speed.y * Time.deltaTime); } else { direction *= -1.0f; State = GGState.RunFront; transform.position = Vector3.MoveTowards(transform.position, transform.position + direction, mnozhitel_speed * speed.y * Time.deltaTime); } }
void WakeRight2(bool right, float mnozhitel_speed) { GetComponent <AudioSource>().clip = Shagi[shag]; if (!GetComponent <AudioSource>().isPlaying) { GetComponent <AudioSource>().Play(); } isFirstAttack = true; //Чтобы после бега/ходьбы была проиграна первая атака State = GGState.WalkRight; direction = transform.right; if (!right) { direction *= -1.0f; transform.position = Vector3.MoveTowards(transform.position, transform.position + direction, mnozhitel_speed * speed.x * Time.deltaTime); } else { transform.position = Vector3.MoveTowards(transform.position, transform.position + direction, mnozhitel_speed * speed.x * Time.deltaTime); } sprite.flipX = !right; }
void Dash() { currentEnergy -= 50; userData.ggData.stats.Set(Stats.Key.ENERGY, currentEnergy); if (State == (GGState)0 || State == (GGState)3 || State == (GGState)8 || State == (GGState)14 || State == (GGState)15) { State = GGState.DashSide; if (State == (GGState)14) { sprite.flipX = true; } } else if (State == (GGState)2 || State == (GGState)5 || State == (GGState)7 || State == (GGState)12) { State = GGState.DashFront; } else if (State == (GGState)1 || State == (GGState)4 || State == (GGState)6 || State == (GGState)13) { State = GGState.DashBack; } IsDash = true; timer = 0 + Time.deltaTime; }
void Dying() { //Персонаж умер. Устанавливаем таймер в 0 для проигрывания анимации IsDeath = true; timer = 0; //Установка нужной анимации if (State == (GGState)0 || State == (GGState)3 || State == (GGState)8 || State == (GGState)14 || State == (GGState)15 || State == GGState.DashSide) { State = GGState.DeathSide; if (State == (GGState)14) { sprite.flipX = true; } } else if (State == (GGState)2 || State == (GGState)4 || State == (GGState)6 || State == (GGState)12 || State == GGState.DashFront) { State = GGState.DeathFront; } else if (State == (GGState)1 || State == (GGState)5 || State == (GGState)7 || State == (GGState)13 || State == GGState.DashBack) { State = GGState.DeathBack; } }
void WakeUp2(bool up, float mnozhitel_speed) { GetComponent <AudioSource>().clip = Shagi[shag]; if (!GetComponent <AudioSource>().isPlaying) { GetComponent <AudioSource>().Play(); } isFirstAttack = true; //Чтобы после бега/ходьбы была проиграна первая атака animator.StopPlayback(); direction = transform.up; if (up) { State = GGState.WalkUp; transform.position = Vector3.MoveTowards(transform.position, transform.position + direction, mnozhitel_speed * speed.y * Time.deltaTime); } else { direction *= -1.0f; State = GGState.WalkDown; transform.position = Vector3.MoveTowards(transform.position, transform.position + direction, mnozhitel_speed * speed.y * Time.deltaTime); } }
void Update() { //обновляем значение энергии из данных игрока. Использование этой переменной в дальнейшем не меняет значения энергии в юзердата //поэтому после каждого изменения здесь обновляю вручную с помощью Set TODO решить этот вопрос по другому currentEnergy = userData.ggData.stats.Get(Stats.Key.ENERGY); restoringEnergy = userData.ggData.stats.Get(Stats.Key.RESTORING_ENERGY); expenseEnergy = userData.ggData.stats.Get(Stats.Key.EXPENSE_ENERGY); //Первая проверка на смерть персонажа, если умер, то нет смысла что-то нажимать //ВОПРОС!? А нормально каждый кадр проверять userData.ggData.stats.Get(Stats.Key.HP) > 0 if (userData.ggData.stats.Get(Stats.Key.HP) > 0) { //Таймер для анимации кувырок timer += 1 * Time.deltaTime; //Если нажата ЛКМ, то анимация удара if (Input.GetMouseButtonUp(0) && (State == GGState.IdleDown || State == GGState.IdleRight || State == GGState.IdleUp)) { timer = 0; Attack(Input.mousePosition); } //Если нажат пробел - по анимцию Dash else if (Input.GetKeyDown(userData.settings.keys["Roll"]) && currentEnergy > 50) { Dash(); } //Используем свои карманы else if (Input.GetKeyDown(userData.settings.keys["Lpocket"])) { if (userData.Lpocket.id != -1) { userData.ggData.stats.Set(Stats.Key.HP, userData.ggData.stats.Get(Stats.Key.HP) + userData.Lpocket.RestoringHP); userData.Lpocket.Stackable--; if (userData.Lpocket.Stackable == 0) { userData.Lpocket.id = -1; } GameObject.Find("HP_Bar").GetComponent <ChangeHPBar>().FunctionOnEnable(); userData.ggData.stats.Set(Stats.Key.WEIGHT, userData.ggData.stats.Get(Stats.Key.WEIGHT) - userData.Lpocket.weight); } } else if (Input.GetKeyDown(userData.settings.keys["Rpocket"])) { if (userData.Rpocket.id != -1) { userData.ggData.stats.Set(Stats.Key.HP, userData.ggData.stats.Get(Stats.Key.HP) + userData.Rpocket.RestoringHP); userData.Rpocket.Stackable--; if (userData.Rpocket.Stackable == 0) { userData.Rpocket.id = -1; } GameObject.Find("HP_Bar").GetComponent <ChangeHPBar>().FunctionOnEnable(); userData.ggData.stats.Set(Stats.Key.WEIGHT, userData.ggData.stats.Get(Stats.Key.WEIGHT) - userData.Rpocket.weight); } } //Если Shift + направление - бег (LeftShift) else if (!IsDash && Input.GetKey(userData.settings.keys["Acceleration"]) && Input.GetKey(userData.settings.keys["Up"]) && Input.GetKey(userData.settings.keys["Right"])) { RunDiag2(true, 0.7071F, true, 0.7071F); } else if (!IsDash && Input.GetKey(userData.settings.keys["Acceleration"]) && Input.GetKey(userData.settings.keys["Up"]) && Input.GetKey(userData.settings.keys["Left"])) { RunDiag2(false, 0.7071F, true, 0.7071F); } else if (!IsDash && Input.GetKey(userData.settings.keys["Acceleration"]) && Input.GetKey(userData.settings.keys["Down"]) && Input.GetKey(userData.settings.keys["Right"])) { RunDiag2(true, 0.7071F, false, 0.7071F); } else if (!IsDash && Input.GetKey(userData.settings.keys["Acceleration"]) && Input.GetKey(userData.settings.keys["Down"]) && Input.GetKey(userData.settings.keys["Left"])) { RunDiag2(false, 0.7071F, false, 0.7071F); } else if (!IsDash && Input.GetKey(userData.settings.keys["Acceleration"]) && Input.GetKey(userData.settings.keys["Up"])) { RunUp2(true, 2 * 1.0f); } else if (!IsDash && Input.GetKey(userData.settings.keys["Acceleration"]) && Input.GetKey(userData.settings.keys["Down"])) { RunUp2(false, 2 * 1.0f); } else if (!IsDash && Input.GetKey(userData.settings.keys["Acceleration"]) && Input.GetKey(userData.settings.keys["Right"])) { RunRight2(true, 2 * 1.0f); } else if (!IsDash && Input.GetKey(userData.settings.keys["Acceleration"]) && Input.GetKey(userData.settings.keys["Left"])) { RunRight2(false, 2 * 1.0f); } //else if (!IsDash && Input.GetKey(userData.settings.keys["Acceleration"]) && Input.GetButton("Vertical") && Input.GetButton("Horizontal") && (currentEnergy > 10.0F)) RunDiag(); //else if (!IsDash && Input.GetKey(userData.settings.keys["Acceleration"]) && Input.GetButton("Vertical") && (currentEnergy > 10.0F)) RunUp(2 * 1.0F); //else if (!IsDash && Input.GetKey(userData.settings.keys["Acceleration"]) && Input.GetButton("Horizontal") && (currentEnergy > 10.0F)) RunSide(2 * 1.0F); //ХОДЬБА //Если нажаты две кнопки - происходит движение по диагонали и отображается анимация ходьбы в сторону. //Если ходьба в какую-либо сторону, то происходит движение именно туда. //Если нажатия кнопок нет, то происходит анимация "стоит". else if (!IsDash && Input.GetKey(userData.settings.keys["Up"]) && Input.GetKey(userData.settings.keys["Right"])) { WakeDiag2(true, 0.7071F, true, 0.7071F); } else if (!IsDash && Input.GetKey(userData.settings.keys["Up"]) && Input.GetKey(userData.settings.keys["Left"])) { WakeDiag2(false, 0.7071F, true, 0.7071F); } else if (!IsDash && Input.GetKey(userData.settings.keys["Down"]) && Input.GetKey(userData.settings.keys["Right"])) { WakeDiag2(true, 0.7071F, false, 0.7071F); } else if (!IsDash && Input.GetKey(userData.settings.keys["Down"]) && Input.GetKey(userData.settings.keys["Left"])) { WakeDiag2(false, 0.7071F, false, 0.7071F); } else if (!IsDash && Input.GetKey(userData.settings.keys["Up"])) { WakeUp2(true, 1.0f); } else if (!IsDash && Input.GetKey(userData.settings.keys["Down"])) { WakeUp2(false, 1.0f); } else if (!IsDash && Input.GetKey(userData.settings.keys["Right"])) { WakeRight2(true, 1.0f); } else if (!IsDash && Input.GetKey(userData.settings.keys["Left"])) { WakeRight2(false, 1.0f); } //Старая версия ходьбы, до появления смены кнопок управления /*else if (!IsDash && Input.GetButton("Vertical") && Input.GetButton("Horizontal") && (currentEnergy > 10.0F)) WakeDiag(); * else if (!IsDash && Input.GetButton("Vertical") && (currentEnergy > 10.0F)) WakeUp(1.0F);*/ //else if (!IsDash && Input.GetButton("Horizontal") && (currentEnergy > 10.0F)) Wake(1.0F); else { GetComponent <AudioSource>().Stop(); //Возвращаемся в состояние "Стояние" currentEnergy += restoringEnergy; currentEnergy = currentEnergy > 100.0F ? 100.0F : currentEnergy; userData.ggData.stats.Set(Stats.Key.ENERGY, currentEnergy); if (State == (GGState)3 || State == (GGState)8 || (State == (GGState)11 && timer >= 0.5f)) { State = GGState.IdleRight; IsDash = false; } else if (State == (GGState)4 || State == (GGState)6 || (State == (GGState)9 && timer >= 0.5f)) { State = GGState.IdleUp; IsDash = false; } else if (State == (GGState)5 || State == (GGState)7 || (State == (GGState)10 && timer >= 0.5f)) { State = GGState.IdleDown; IsDash = false; } else if ((State == GGState.Attack1Left || State == GGState.Attack2Left) && timer > 0.5f) { sprite.flipX = true; State = GGState.IdleRight; } else if ((State == GGState.Attack1Right || State == GGState.Attack2Right) && timer > 0.5f) { State = GGState.IdleRight; } else if ((State == GGState.Attack1Back || State == GGState.Attack2Back) && timer > 0.5f) { State = GGState.IdleUp; } else if ((State == GGState.Attack1Front || State == GGState.Attack2Front) && timer > 0.5f) { State = GGState.IdleDown; } } if (IsDash) { //В зависимости от того, куда кувырок, то производим смещение. if (State == GGState.DashFront) { transform.position = Vector3.MoveTowards(transform.position, transform.position - new Vector3(0, 1.5f), 3 * Time.deltaTime); } else if (State == GGState.DashBack) { transform.position = Vector3.MoveTowards(transform.position, transform.position + new Vector3(0, 1.5f), 3 * Time.deltaTime); } else if (State == GGState.DashSide && sprite.flipX == false) { transform.position = Vector3.MoveTowards(transform.position, transform.position + new Vector3(1.5f, 0), 3 * Time.deltaTime); } else { transform.position = Vector3.MoveTowards(transform.position, transform.position - new Vector3(1.5f, 0), 3 * Time.deltaTime); } } } else { //Для начала проигрывания анимации if (!IsDeath) { Dying(); } //Таймер для того, чтобы сыграть анимацию умирания timer += 1 * Time.deltaTime; if (timer > 1.5F) { //Останавливаем анимацию ГГ, он будет лежать на земле GetComponent <Animator>().speed = 0; //Если еще не был открыт канвас "Вы погибли", то обнуляем таймер, чтобы сыграть анимацию на канвасе if (!YouDied.activeSelf) { timer = 0; } //Открываем канвас "Вы погибли" и тем самым проигрывается анимация YouDied.SetActive(true); //Если анимация подходит к концу, то останавливаем игровой таймер, тем самым анимация на канвасе тоже закончится и //делаем активными кнопки на этом канвасе if (YouDied.activeSelf && timer > 2) { Time.timeScale = 0.0F; YouDied.GetComponent <YouDiedScript>().SetActiveAllButtons(); } } } //Для того, чтобы все люди относительно друг друга имели передний и задний план. transform.position = new Vector3(transform.position.x, transform.position.y, -2 + transform.position.y / 1000); //это решили не делать, но может еще пригодиться как образец рабочего рейкаста //прозрачность перса если он зашел за дом /*RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector3.forward); * * if (hit.collider != null && hit.transform.parent && hit.transform.parent.gameObject.tag == "Building") * { * col = new Color(sprite.color.r, sprite.color.g, sprite.color.b, 0.2f); * sprite.color = col; * } * else * { * col = new Color(sprite.color.r, sprite.color.g, sprite.color.b, 1); * sprite.color = col; * }*/ }
void Attack(Vector3 MousePosition) { if (userData.ggData.IsBattle && isFirstAttack) { //Проверка в какую из четырех областей было нажато ЛКМ sprite.flipX = false; if (MousePosition.y / MousePosition.x < (float)Screen.height / (float)Screen.width && (-(float)Screen.height + MousePosition.y) / MousePosition.x < -((float)Screen.height / (float)Screen.width)) { State = GGState.Attack1Front; KudaFirstAttack = 2; //Debug.Log("Attack1Front | " + isFirstAttack); } if (MousePosition.y / MousePosition.x >= (float)Screen.height / (float)Screen.width && (-(float)Screen.height + MousePosition.y) / MousePosition.x <= -((float)Screen.height / (float)Screen.width)) { State = GGState.Attack1Left; KudaFirstAttack = 3; //Debug.Log("Attack1Left | " + isFirstAttack); } if (MousePosition.y / MousePosition.x <= (float)Screen.height / (float)Screen.width && (-(float)Screen.height + MousePosition.y) / MousePosition.x >= -((float)Screen.height / (float)Screen.width)) { State = GGState.Attack1Right; KudaFirstAttack = 4; //Debug.Log("Attack1Right | " + isFirstAttack); } if (MousePosition.y / MousePosition.x > (float)Screen.height / (float)Screen.width && (-(float)Screen.height + MousePosition.y) / MousePosition.x > -((float)Screen.height / (float)Screen.width)) { State = GGState.Attack1Back; KudaFirstAttack = 1; //Debug.Log("Attack1Back | " + isFirstAttack); } } else if (userData.ggData.IsBattle) { //Проверка в какую из четырех областей было нажато ЛКМ sprite.flipX = false; if (MousePosition.y / MousePosition.x < (float)Screen.height / (float)Screen.width && (-(float)Screen.height + MousePosition.y) / MousePosition.x < -((float)Screen.height / (float)Screen.width)) { //Debug.Log("Attack2Front | " + isFirstAttack); if (KudaFirstAttack == 2) { State = GGState.Attack2Front; } else { KudaFirstAttack = 2; isFirstAttack = true; State = GGState.Attack1Front; } } if (MousePosition.y / MousePosition.x >= (float)Screen.height / (float)Screen.width && (-(float)Screen.height + MousePosition.y) / MousePosition.x <= -((float)Screen.height / (float)Screen.width)) { //Debug.Log("Attack2Left | " + isFirstAttack); if (KudaFirstAttack == 3) { State = GGState.Attack2Left; } else { KudaFirstAttack = 3; isFirstAttack = true; State = GGState.Attack1Left; } } if (MousePosition.y / MousePosition.x <= (float)Screen.height / (float)Screen.width && (-(float)Screen.height + MousePosition.y) / MousePosition.x >= -((float)Screen.height / (float)Screen.width)) { //Debug.Log("Attack2Right | " + isFirstAttack); if (KudaFirstAttack == 4) { State = GGState.Attack2Right; } else { KudaFirstAttack = 4; isFirstAttack = true; State = GGState.Attack1Right; } } if (MousePosition.y / MousePosition.x > (float)Screen.height / (float)Screen.width && (-(float)Screen.height + MousePosition.y) / MousePosition.x > -((float)Screen.height / (float)Screen.width)) { //Debug.Log("Attack2Back | " + isFirstAttack); if (KudaFirstAttack == 1) { State = GGState.Attack2Back; } else { KudaFirstAttack = 1; isFirstAttack = true; State = GGState.Attack1Back; } } } isFirstAttack = !isFirstAttack; }