Adds skills to monster cards.

This commit is contained in:
2020-09-01 22:11:04 -07:00
parent 257ecb7758
commit 592c0732d2
5 changed files with 138 additions and 0 deletions

View File

@@ -13,6 +13,7 @@ public class Monster {
public Monster() { public Monster() {
mSavingThrows = new HashSet<>(); mSavingThrows = new HashSet<>();
mSkills = new HashSet<>();
} }
private String mName; private String mName;
@@ -586,4 +587,36 @@ public class Monster {
public void setCustomProficiencyBonus(int proficiencyBonus) { public void setCustomProficiencyBonus(int proficiencyBonus) {
mCustomProficiencyBonus = proficiencyBonus; mCustomProficiencyBonus = proficiencyBonus;
} }
private HashSet<Skill> mSkills;
public Set<Skill> getSkills() {
return mSkills;
}
public void addSkill(Skill skill) {
mSkills.add(skill);
}
public void removeSkill(Skill skill) {
mSkills.remove(skill);
}
public void clearSkill(Skill skill) {
mSkills.clear();
}
public String getSkillsDescription() {
Skill[] elements = new Skill[mSkills.size()];
elements = mSkills.toArray(elements);
Arrays.sort(elements);
StringBuilder sb = new StringBuilder();
boolean isFirst = true;
for (Skill skill : elements) {
if (!isFirst) {
sb.append(", ");
}
String name = skill.getName();
sb.append(skill.getText(this));
isFirst = false;
}
return sb.toString();
}
} }

View File

@@ -0,0 +1,68 @@
package com.majinnaibu.monstercards.models;
import java.util.Comparator;
import java.util.Locale;
public class Skill implements Comparator<Skill>, Comparable<Skill> {
private String mName;
private String mAbilityScoreName;
private String mNote;
public Skill(String name, String abilityScoreName) {
mName = name;
mAbilityScoreName = abilityScoreName;
mNote = "";
}
public Skill(String name, String abilityScoreName, String note) {
mName = name;
mAbilityScoreName = abilityScoreName;
mNote = note;
}
public String getName() {
return mName;
}
public void setName(String name) {
mName = name;
}
public String getAbilityScoreName() {
return mAbilityScoreName;
}
public void setAbilityScoreName(String abilityScoreName) {
mAbilityScoreName = abilityScoreName;
}
public String getNote() {
return mNote;
}
public int getSkillBonus(Monster monster) {
int bonus = monster.getAbilityModifier(mAbilityScoreName);
if (" (ex)".equals(getNote())) {
bonus += 2 * monster.getProficiencyBonus();
} else {
bonus += monster.getProficiencyBonus();
}
return bonus;
}
public String getText(Monster monster) {
int bonus = getSkillBonus(monster);
return String.format(Locale.US, "%s%s %d", mName.substring(0,1), mName.substring(1), bonus);
}
@Override
public int compareTo(Skill o) {
return this.getName().compareToIgnoreCase(o.getName());
}
@Override
public int compare(Skill o1, Skill o2) {
return o1.getName().compareToIgnoreCase(o2.getName());
}
}

View File

@@ -17,6 +17,7 @@ import com.majinnaibu.monstercards.R;
import com.majinnaibu.monstercards.helpers.StringHelper; import com.majinnaibu.monstercards.helpers.StringHelper;
import com.majinnaibu.monstercards.models.Monster; import com.majinnaibu.monstercards.models.Monster;
import com.majinnaibu.monstercards.models.SavingThrow; import com.majinnaibu.monstercards.models.SavingThrow;
import com.majinnaibu.monstercards.models.Skill;
@SuppressWarnings("FieldCanBeLocal") @SuppressWarnings("FieldCanBeLocal")
public class MonsterFragment extends Fragment { public class MonsterFragment extends Fragment {
@@ -66,6 +67,9 @@ public class MonsterFragment extends Fragment {
monster.addSavingThrow(new SavingThrow("int", 3)); monster.addSavingThrow(new SavingThrow("int", 3));
monster.addSavingThrow(new SavingThrow("wis", 4)); monster.addSavingThrow(new SavingThrow("wis", 4));
monster.addSavingThrow(new SavingThrow("cha", 5)); monster.addSavingThrow(new SavingThrow("cha", 5));
//Skills
monster.addSkill(new Skill("perception", "wis"));
monster.addSkill(new Skill("stealth", "dexterity"));
// Challenge Rating // Challenge Rating
monster.setChallengeRating("*"); monster.setChallengeRating("*");
@@ -177,6 +181,19 @@ public class MonsterFragment extends Fragment {
} }
}); });
final TextView monsterSkills = root.findViewById(R.id.skills);
monsterViewModel.getSkills().observe(getViewLifecycleOwner(), new Observer<String>() {
@Override
public void onChanged(String skills) {
if (StringHelper.isNullOrEmpty(skills)) {
monsterSkills.setVisibility(View.GONE);
} else {
monsterSkills.setVisibility(View.VISIBLE);
}
monsterSkills.setText(Html.fromHtml("<b>Skills</b> " + skills));
}
});
return root; return root;
} }
} }

View File

@@ -34,6 +34,8 @@ public class MonsterViewModel extends ViewModel {
mCharisma.setValue(""); mCharisma.setValue("");
mSavingThrows = new MutableLiveData<>(); mSavingThrows = new MutableLiveData<>();
mSavingThrows.setValue(""); mSavingThrows.setValue("");
mSkills = new MutableLiveData<>();
mSkills.setValue("");
} }
private MutableLiveData<String> mName; private MutableLiveData<String> mName;
@@ -84,6 +86,10 @@ public class MonsterViewModel extends ViewModel {
public LiveData<String> getSavingThrows() { public LiveData<String> getSavingThrows() {
return mSavingThrows; return mSavingThrows;
} }
private MutableLiveData<String> mSkills;
public LiveData<String> getSkills() {
return mSkills;
}
private Monster mMonster; private Monster mMonster;
public void setMonster(Monster monster) { public void setMonster(Monster monster) {
@@ -100,5 +106,6 @@ public class MonsterViewModel extends ViewModel {
mWisdom.setValue(monster.getWisdomDescription()); mWisdom.setValue(monster.getWisdomDescription());
mCharisma.setValue(monster.getCharismaDescription()); mCharisma.setValue(monster.getCharismaDescription());
mSavingThrows.setValue(monster.getSavingThrowsDescription()); mSavingThrows.setValue(monster.getSavingThrowsDescription());
mSkills.setValue(monster.getSkillsDescription());
} }
} }

View File

@@ -318,5 +318,18 @@
app:layout_constraintTop_toBottomOf="@+id/divider3" app:layout_constraintTop_toBottomOf="@+id/divider3"
tools:text="Saving Throws" /> tools:text="Saving Throws" />
<TextView
android:id="@+id/skills"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/saving_throws"
tools:text="Skills" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView> </ScrollView>