all repos — RPG @ 5ec8a78dde387284a445b93ea73f4a6a14b5e1e6

Fully functional 3D turn based role playing game coded in C# and base Unity.

Assets/Scripts/Battle/Abilities/Attack.cs (view raw)

 1using System;
 2using UnityEngine;
 3
 4[System.Serializable]
 5public class Attack : Ability
 6{
 7    //constructor
 8    public Attack()
 9    {
10        dbName = "Attack";
11        guiName = "Attack";
12        hasTarget = true;
13        ow_usable = false;
14        MP_cost = 0;
15    }
16    
17    public override string Execute(Fighter source, Fighter target, out bool output)
18    {
19        if(target.stats.HP == 0)
20        {
21            output = false;
22            return target + " is already dead!";
23        }
24        string str = source + " attacks " + target;
25        //number logic
26        double prob, crit = 0, damage = 0;
27        bool didCrit = false;
28
29        prob = 3 * Math.Log(source.stats.AGL / target.stats.AGL) + 87;
30        if (UnityEngine.Random.Range(0, 100) >= prob) // prob = probability to hit; using > is like inverting the logic
31        {
32            str += " (Miss...)";
33        }
34        else
35        {
36            //check critical
37            prob = ((31 * source.stats.LCK) + 200) / 50;
38            if (UnityEngine.Random.Range(0, 100) <= prob)
39            {
40                crit = source.stats.ATK * 3;
41                str += " (Critical!)";
42                didCrit = true;
43            }
44            
45            damage = 27.241 * Math.Log(source.stats.ATK / source.stats.DEF) + 148.78 + crit;
46
47            damage += (damage * UnityEngine.Random.Range(-5, 5) / 100);
48            
49            target.stats.HP -= damage;
50        }
51        UI.ShowDamage(target, (int)damage, didCrit);
52        source.animator.SetTrigger("Attack");
53        output = true;
54        return str;
55    }
56
57    public override string GetDescription()
58    {
59        return "A normal physical attack.";
60    }
61}