Damage or Heal based on the buttons you press

Posted on

Problem

If you press the damage button, it will damage armor before health. If you press the heal button, it will heal armor before health.

The project is here: https://github.com/austingaee/DamageHealth-Armor

Are there any improvements I can make?

import UIKit

class ViewController: UIViewController {


    @IBOutlet weak var armorBarView: UIView!

    @IBOutlet weak var healthBarView: UIView!

    var damageAmount : Float = 0.0
    var healAmount : Float = 0.0

    //Maximum Health
    var barAmount : Float = 0.0

    override func viewDidLoad() {
        super.viewDidLoad()

        damageAmount = Float(self.armorBarView.frame.size.width) * 0.10
        healAmount = Float(self.armorBarView.frame.size.width) * 0.05
        barAmount = Float(self.armorBarView.frame.size.width)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func damageBar(_ sender: UIButton) {
        //7.63 came from how much damage was left after 10 hits
        if self.armorBarView.frame.size.width > 7.63 {
            self.armorBarView.frame.size.width -= CGFloat(damageAmount)
        } else if self.healthBarView.frame.size.width > 7.63 {
            self.healthBarView.frame.size.width -= CGFloat(damageAmount)
        }
    }

    @IBAction func healBar(_ sender: Any) {
        if self.armorBarView.frame.size.width < CGFloat(barAmount) {
            self.armorBarView.frame.size.width += CGFloat(healAmount)
            print(self.armorBarView.frame.size.width)
        } else if self.healthBarView.frame.size.width < CGFloat(barAmount) {
            self.healthBarView.frame.size.width += CGFloat(healAmount)
        }
    }

}

Solution

not so much but some tiny changes:

DRY

i just would avoid all the self.healthBarView.frame.size.width

and make methods:

  • getWidth
  • setWidth(or updateWidth because you set the diff)

Constants

the magic numbers like 0.1 0.05 10 i would define as constants so it can changed only at one place. 7.63 i would calculate

Naming

rename barAmount to maxAmount

create minAmount

Float/CGFloat

the Float / CGFloat is realy needed? just put them also into getWidth and updateWidth

else if

the else if block is not needed (same condition)

7.63

this is an value depending on your setup – it should also calculated so it not changes when you init it with different width

Resulting Code

import UIKit

class ViewController: UIViewController {


    @IBOutlet weak var armorBarView: UIView!

    @IBOutlet weak var healthBarView: UIView!

    var damageAmount : Float = 0.0
    var healAmount : Float = 0.0

    // Min/Maximum Health
    var minAmount: Float = 0.00
    var maxAmount : Float = 0.0

    // how much damage was left after this number of hits
    let minAmountHits:Float = 10

    let demageFactor: Float = 0.10
    let healFactor: Float = 0.05

    override func viewDidLoad() {
        super.viewDidLoad()

        initAmount()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func initAmount(){
        damageAmount = getWidth() * demageFactor
        healAmount = getWidth() * healFactor
        maxAmount = getWidth()
        minAmount = maxAmount - minAmountHits * damageAmount
    }

    @IBAction func damageBar(_ sender: UIButton) {
        if getWidth() > minAmount {
            updateWidth(-damageAmount)
        }
    }

    @IBAction func healBar(_ sender: Any) {
        if getWidth() < maxAmount {
            updateWidth(healAmount)
        }
    }

    func getWidth() -> Float {
        return Float(self.armorBarView.frame.size.width)
    }

    func updateWidth(_ amount: Float) {
        self.healthBarView.frame.size.width += CGFloat(amount)
        print(self.armorBarView.frame.size.width)
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *