all repos — RPG @ 782b83c6c259a2b11d9354097fd68317d1ded9a7

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