Swiftpack.co - Package - 0xLeif/ScapeKit

ScapeKit

A package to manage Skills, Levels, and Experience.

Example

let character = ScapeCharacter(name: "Hans")

XCTAssertEqual(character.skills[.ranged]?.name, "Ranged")
XCTAssertEqual(character.skills[.ranged]?.currentLevel, 1)
XCTAssertEqual(character.skills[.ranged]?.maxLevel, 99)
XCTAssertEqual(character.skills[.ranged]?.currentExperience, 0)

XCTAssertEqual(character.skills.count, 23)
XCTAssertEqual(character.totalLevel, 23)

XCTAssertEqual(character.skills[.ranged]?.name, "Ranged")
XCTAssertEqual(character.skills[.ranged]?.currentLevel, 1)
XCTAssertEqual(character.skills[.ranged]?.maxLevel, 99)
XCTAssertEqual(character.skills[.ranged]?.currentExperience, 0)

let nonexistentActionName = "Some Action"

let skillActionResult = character.skills[.ranged]?.roll(actionNamed: nonexistentActionName)
let skillActionResultText = character.skills[.ranged]?.use(actionNamed: nonexistentActionName)

XCTAssertNil(skillActionResult)
XCTAssertEqual(skillActionResultText, "The Skill Some Action does not contain an action named: \(nonexistentActionName)")

let skillAction = ScapeSkillAction(name: "Arrow Volley", level: 10)

character.skills[.ranged]?.actions.append(skillAction)

let skillActionResult = character.skills[.ranged]?.roll(actionNamed: "Arrow Volley")
let skillActionResultText = character.skills[.ranged]?.use(actionNamed: "Arrow Volley")

XCTAssertEqual(skillActionResult, SkillActionResult.fail)
XCTAssertEqual(skillActionResultText, "Arrow Volley (Required Level: 10): Failed for level 1")

Inspired by RuneScape

Github

link
Stars: 2

Dependencies

Used By

Total: 0

Releases

Level 02 -

Character and Actions

struct ScapeCharacter {
   var name: String
   var skills: [ScapeSkillType: ScapeSkill]
}
public protocol SkillLevelAction {
    var name: String { get set }
    var level: Int { get set }
    
    func result(forLevel level: Int) -> SkillActionResult
    func resultDescription(forLevel level: Int) -> String
}

Examples

let character = ScapeCharacter(name: "Hans")

// init levels and skills

XCTAssertEqual(character.skills[.ranged]?.name, "Ranged")
XCTAssertEqual(character.skills[.ranged]?.currentLevel, 1)
XCTAssertEqual(character.skills[.ranged]?.maxLevel, 99)
XCTAssertEqual(character.skills[.ranged]?.currentExperience, 0)

XCTAssertEqual(character.skills.count, 23)
XCTAssertEqual(character.totalLevel, 23)

// skill level

character.skills[.ranged]?.levelUp(levels: 9)

XCTAssertEqual(character.skills[.ranged]?.name, "Ranged")
XCTAssertEqual(character.skills[.ranged]?.currentLevel, 10)
XCTAssertEqual(character.skills[.ranged]?.maxLevel, 99)
XCTAssertEqual(character.skills[.ranged]?.currentExperience, 1154)

XCTAssertEqual(character.skills.count, 23)
XCTAssertEqual(character.totalLevel, 32)

// max level

ScapeSkillType.allCases.forEach {
    character.skills[$0]?.levelUp(levels: 98)
}

XCTAssertEqual(character.skills[.ranged]?.name, "Ranged")
XCTAssertEqual(character.skills[.ranged]?.currentLevel, 99)
XCTAssertEqual(character.skills[.ranged]?.maxLevel, 99)
XCTAssertEqual(character.skills[.ranged]?.currentExperience, 13034431)

XCTAssertEqual(character.skills.count, 23)
XCTAssertEqual(character.totalLevel, 2277)
let character = ScapeCharacter(name: "Hans")

XCTAssertEqual(character.skills[.ranged]?.name, "Ranged")
XCTAssertEqual(character.skills[.ranged]?.currentLevel, 1)
XCTAssertEqual(character.skills[.ranged]?.maxLevel, 99)
XCTAssertEqual(character.skills[.ranged]?.currentExperience, 0)

let nonexistentActionName = "Some Action"

let skillActionResult = character.skills[.ranged]?.roll(actionNamed: nonexistentActionName)
let skillActionResultText = character.skills[.ranged]?.use(actionNamed: nonexistentActionName)

XCTAssertNil(skillActionResult)
XCTAssertEqual(skillActionResultText, "The Skill Some Action does not contain an action named: \(nonexistentActionName)")

let skillAction = ScapeSkillAction(name: "Arrow Volley", level: 10)

character.skills[.ranged]?.actions.append(skillAction)

let skillActionResult = character.skills[.ranged]?.roll(actionNamed: "Arrow Volley")
let skillActionResultText = character.skills[.ranged]?.use(actionNamed: "Arrow Volley")

XCTAssertEqual(skillActionResult, SkillActionResult.fail)
XCTAssertEqual(skillActionResultText, "Arrow Volley (Required Level: 10): Failed for level 1")

Level 01 -

ScapeKit

A package to manage Skills, Levels, and Experience.

Example

var skill = ScapeSkill(name: ScapeSkillType.ranged.name,
   currentLevel: 1,
   maxLevel: 99)

skill.levelUp(levels: 5)

XCTAssertEqual(skill.name, "Ranged")
XCTAssertEqual(skill.currentLevel, 6)
XCTAssertEqual(skill.maxLevel, 99)
XCTAssertEqual(skill.currentExperience, 512)

Inspired by RuneScape