Started adding skills.
This commit is contained in:
53
iOS/MonsterCards/Models/Enums/AbilityScore.swift
Normal file
53
iOS/MonsterCards/Models/Enums/AbilityScore.swift
Normal file
@@ -0,0 +1,53 @@
|
||||
//
|
||||
// AbilityScore.swift
|
||||
// MonsterCards
|
||||
//
|
||||
// Created by Tom Hicks on 1/18/21.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
enum AbilityScore: String, CaseIterable, Identifiable {
|
||||
case strength = "strength"
|
||||
case dexterity = "dexterity"
|
||||
case constitution = "constitution"
|
||||
case intelligence = "intelligence"
|
||||
case wisdom = "wisdom"
|
||||
case charisma = "charisma"
|
||||
|
||||
var id: AbilityScore { self }
|
||||
|
||||
var displayName: String {
|
||||
switch self {
|
||||
case .strength:
|
||||
return "Strength"
|
||||
case .dexterity:
|
||||
return "Dexterity"
|
||||
case .constitution:
|
||||
return "Constitution"
|
||||
case .intelligence:
|
||||
return "Intelligence"
|
||||
case .wisdom:
|
||||
return "Wisdom"
|
||||
case .charisma:
|
||||
return "Charisma"
|
||||
}
|
||||
}
|
||||
|
||||
var shortDisplayName: String {
|
||||
switch self {
|
||||
case .strength:
|
||||
return "STR"
|
||||
case .dexterity:
|
||||
return "DEX"
|
||||
case .constitution:
|
||||
return "CON"
|
||||
case .intelligence:
|
||||
return "INT"
|
||||
case .wisdom:
|
||||
return "WIS"
|
||||
case .charisma:
|
||||
return "CHA"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -137,6 +137,23 @@ public class Monster: NSManagedObject {
|
||||
return Int(floor(Double((score - 10)) / 2.0))
|
||||
}
|
||||
|
||||
func abilityModifierForAbilityScore(_ abilityScore: AbilityScore) -> Int {
|
||||
switch abilityScore {
|
||||
case .strength:
|
||||
return strengthModifier;
|
||||
case .dexterity:
|
||||
return dexterityModifier
|
||||
case .constitution:
|
||||
return constitutionModifier
|
||||
case .intelligence:
|
||||
return intelligenceModifier
|
||||
case .wisdom:
|
||||
return wisdomModifier
|
||||
case .charisma:
|
||||
return charismaModifier
|
||||
}
|
||||
}
|
||||
|
||||
var strengthModifier: Int {
|
||||
get {
|
||||
return Monster.abilityModifierForScore(Int(strengthScore))
|
||||
@@ -587,6 +604,21 @@ public class Monster: NSManagedObject {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Skills
|
||||
|
||||
var skillsDescription: String {
|
||||
get {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
var skillsArray: [Skill] {
|
||||
let set = skills as? Set<Skill> ?? []
|
||||
return set.sorted {
|
||||
$0.wrappedName < $1.wrappedName
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: End
|
||||
|
||||
}
|
||||
|
||||
8
iOS/MonsterCards/Models/MonsterViewModel.swift
Normal file
8
iOS/MonsterCards/Models/MonsterViewModel.swift
Normal file
@@ -0,0 +1,8 @@
|
||||
//
|
||||
// MonsterViewModel.swift
|
||||
// MonsterCards
|
||||
//
|
||||
// Created by Tom Hicks on 1/18/21.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
63
iOS/MonsterCards/Models/Skill+CoreDataClass.swift
Normal file
63
iOS/MonsterCards/Models/Skill+CoreDataClass.swift
Normal file
@@ -0,0 +1,63 @@
|
||||
//
|
||||
// Skill+CoreDataClass.swift
|
||||
// MonsterCards
|
||||
//
|
||||
// Created by Tom Hicks on 1/18/21.
|
||||
//
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import CoreData
|
||||
|
||||
@objc(Skill)
|
||||
public class Skill: NSManagedObject {
|
||||
|
||||
var wrappedName: String {
|
||||
get {
|
||||
return name ?? ""
|
||||
}
|
||||
set {
|
||||
name = newValue
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var wrappedProficiency: ProficiencyType {
|
||||
get {
|
||||
return ProficiencyType.init(rawValue: proficiency ?? "") ?? .none
|
||||
}
|
||||
set {
|
||||
proficiency = newValue.rawValue
|
||||
}
|
||||
}
|
||||
|
||||
var wrappedAbilityScore: AbilityScore {
|
||||
get {
|
||||
return AbilityScore.init(rawValue: abilityScoreName ?? "") ?? .strength
|
||||
}
|
||||
set {
|
||||
abilityScoreName = newValue.rawValue
|
||||
}
|
||||
}
|
||||
|
||||
var modifier: Int64 {
|
||||
get {
|
||||
let proficiencyBonus = Double(monster?.proficiencyBonus ?? 0)
|
||||
let abilityScoreModifier = Double(monster?.abilityModifierForAbilityScore(wrappedAbilityScore) ?? 0)
|
||||
switch wrappedProficiency {
|
||||
case .none:
|
||||
return Int64(abilityScoreModifier)
|
||||
case .proficient:
|
||||
return Int64(abilityScoreModifier + proficiencyBonus)
|
||||
case .expertise:
|
||||
return Int64(abilityScoreModifier + 2 * proficiencyBonus)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var skillDescription: String {
|
||||
get {
|
||||
return String(format: "%@ %+d%@", name ?? "", modifier)
|
||||
}
|
||||
}
|
||||
}
|
||||
38
iOS/MonsterCards/Models/SkillViewModel.swift
Normal file
38
iOS/MonsterCards/Models/SkillViewModel.swift
Normal file
@@ -0,0 +1,38 @@
|
||||
//
|
||||
// SkillViewModel.swift
|
||||
// MonsterCards
|
||||
//
|
||||
// Created by Tom Hicks on 1/18/21.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
class SkillViewModel: ObservableObject {
|
||||
|
||||
init(_ rawSkill: Skill?) {
|
||||
if (rawSkill != nil) {
|
||||
self.rawSkill = rawSkill
|
||||
_name = rawSkill!.name ?? ""
|
||||
} else {
|
||||
_name = ""
|
||||
}
|
||||
}
|
||||
|
||||
var rawSkill: Skill?
|
||||
|
||||
private var _name: String = ""
|
||||
var name: String {
|
||||
get {
|
||||
return _name
|
||||
}
|
||||
set {
|
||||
if (newValue != _name) {
|
||||
_name = newValue
|
||||
// Notify changed name
|
||||
}
|
||||
if (rawSkill != nil) {
|
||||
rawSkill!.name = newValue
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -49,8 +49,16 @@
|
||||
<attribute name="wisdomSavingThrowAdvantage" attributeType="String" defaultValueString="none"/>
|
||||
<attribute name="wisdomSavingThrowProficiency" attributeType="String" defaultValueString="none"/>
|
||||
<attribute name="wisdomScore" attributeType="Integer 64" defaultValueString="10" usesScalarValueType="YES"/>
|
||||
<relationship name="skills" optional="YES" toMany="YES" deletionRule="Cascade" destinationEntity="Skill" inverseName="monster" inverseEntity="Skill"/>
|
||||
</entity>
|
||||
<entity name="Skill" representedClassName="Skill" syncable="YES" codeGenerationType="category">
|
||||
<attribute name="abilityScoreName" attributeType="String" defaultValueString=""/>
|
||||
<attribute name="name" attributeType="String" defaultValueString=""/>
|
||||
<attribute name="proficiency" attributeType="String" defaultValueString=""/>
|
||||
<relationship name="monster" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Monster" inverseName="skills" inverseEntity="Monster"/>
|
||||
</entity>
|
||||
<elements>
|
||||
<element name="Monster" positionX="-63" positionY="-18" width="128" height="763"/>
|
||||
<element name="Monster" positionX="-63" positionY="-18" width="128" height="778"/>
|
||||
<element name="Skill" positionX="-63" positionY="135" width="128" height="103"/>
|
||||
</elements>
|
||||
</model>
|
||||
@@ -54,7 +54,8 @@ struct EditMonster: View {
|
||||
@State private var monsterWisdomSavingThrowAdvantage: AdvantageType = .none
|
||||
@State private var monsterCharismaSavingThrowProficiency: ProficiencyType = .none
|
||||
@State private var monsterCharismaSavingThrowAdvantage: AdvantageType = .none
|
||||
|
||||
@State private var monsterSkills: [Skill] = []
|
||||
|
||||
|
||||
var body: some View {
|
||||
List {
|
||||
@@ -258,6 +259,50 @@ struct EditMonster: View {
|
||||
}
|
||||
}
|
||||
.textCase(nil)
|
||||
Section(header: HStack {
|
||||
Text("Skills")
|
||||
Spacer()
|
||||
Button(action: addSkill) {
|
||||
Image(systemName:"plus")
|
||||
}
|
||||
}) {
|
||||
VStack {
|
||||
// ForEach((1...10).reversed(), id: \.self) {
|
||||
// Text("\($0)…")
|
||||
// }
|
||||
// List(monster.skillsArray) {_ in
|
||||
// Text ("Name")
|
||||
// }
|
||||
ForEach(monsterSkills, id: \.self) { skill in
|
||||
VStack {
|
||||
Text("---")
|
||||
Text(skill.wrappedName)
|
||||
Text(skill.wrappedProficiency.displayName)
|
||||
Text(skill.wrappedAbilityScore.displayName)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.textCase(nil)
|
||||
|
||||
// Section(header: HStack {
|
||||
// Text("Skills")
|
||||
// Spacer()
|
||||
// Button(action: addSkill) {
|
||||
// Image(systemName:"plus")
|
||||
// }
|
||||
// }) {
|
||||
// VStack {
|
||||
//// let skills2: [Skill] = monster.skills?.allObjects)
|
||||
// ForEach(monster.allSkills)) { skill in
|
||||
// Text("A SKill")
|
||||
// }
|
||||
// }
|
||||
//// ForEach(arrayLiteral: Array(monster.skills)) { skill in
|
||||
//// Text("A Skill")
|
||||
//// }
|
||||
// }
|
||||
// .textCase(nil)
|
||||
}
|
||||
.onAppear(perform: copyMonsterToLocal)
|
||||
.toolbar(content: {
|
||||
@@ -273,6 +318,29 @@ struct EditMonster: View {
|
||||
.navigationBarBackButtonHidden(true)
|
||||
}
|
||||
|
||||
private func addSkill() {
|
||||
print("Add Skill pressed")
|
||||
|
||||
let newSkill = Skill.init(context: viewContext)
|
||||
newSkill.name = "Acrobatics"
|
||||
newSkill.wrappedAbilityScore = .dexterity
|
||||
newSkill.wrappedProficiency = .proficient
|
||||
monster.addToSkills(newSkill);
|
||||
// newSkill.monster = monster
|
||||
// monster.addSkill(newSkill)
|
||||
// (monster.skills as! NSMutableSet).add(newSkill)
|
||||
// var s1: NSMutableSet = ["A", "S", "D", "F"]
|
||||
// var s2: Set = ["A", "S", "D", "F"]
|
||||
// s1.add
|
||||
//
|
||||
do {
|
||||
try viewContext.save()
|
||||
monsterSkills = monster.skillsArray
|
||||
} catch {
|
||||
print("error")
|
||||
}
|
||||
}
|
||||
|
||||
private func dismissView() {
|
||||
self.presentationMode.wrappedValue.dismiss()
|
||||
}
|
||||
@@ -376,6 +444,17 @@ struct EditMonster: View {
|
||||
monster.wisdomSavingThrowAdvantageEnum = monsterWisdomSavingThrowAdvantage
|
||||
monster.charismaSavingThrowProficiencyEnum = monsterCharismaSavingThrowProficiency
|
||||
monster.charismaSavingThrowAdvantageEnum = monsterCharismaSavingThrowAdvantage
|
||||
monster.skills?.forEach { s in
|
||||
let skill = s as! Skill
|
||||
if (!monsterSkills.contains(skill)) {
|
||||
monster.removeFromSkills(skill)
|
||||
}
|
||||
}
|
||||
monster.skillsArray.forEach { skill in
|
||||
if (!(monster.skills?.contains(skill) ?? false)) {
|
||||
monster.addToSkills(skill)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user