Adds skills to monster cards.
This commit is contained in:
@@ -13,6 +13,7 @@ public class Monster {
|
||||
|
||||
public Monster() {
|
||||
mSavingThrows = new HashSet<>();
|
||||
mSkills = new HashSet<>();
|
||||
}
|
||||
|
||||
private String mName;
|
||||
@@ -586,4 +587,36 @@ public class Monster {
|
||||
public void setCustomProficiencyBonus(int 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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,95 +1,68 @@
|
||||
package com.majinnaibu.monstercards.models;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.majinnaibu.monstercards.data.enums.AbilityScore;
|
||||
import com.majinnaibu.monstercards.data.enums.AdvantageType;
|
||||
import com.majinnaibu.monstercards.data.enums.ProficiencyType;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.Objects;
|
||||
import java.util.Locale;
|
||||
|
||||
@SuppressLint("DefaultLocale")
|
||||
public class Skill implements Comparator<Skill>, Comparable<Skill> {
|
||||
|
||||
public String name;
|
||||
public AbilityScore abilityScore;
|
||||
public AdvantageType advantageType;
|
||||
public ProficiencyType proficiencyType;
|
||||
private String mName;
|
||||
private String mAbilityScoreName;
|
||||
private String mNote;
|
||||
|
||||
public Skill(String name, AbilityScore abilityScore) {
|
||||
this(name, abilityScore, AdvantageType.NONE, ProficiencyType.PROFICIENT);
|
||||
public Skill(String name, String abilityScoreName) {
|
||||
mName = name;
|
||||
mAbilityScoreName = abilityScoreName;
|
||||
mNote = "";
|
||||
}
|
||||
|
||||
public Skill(String name, AbilityScore abilityScore, AdvantageType advantageType) {
|
||||
this(name, abilityScore, advantageType, ProficiencyType.PROFICIENT);
|
||||
public Skill(String name, String abilityScoreName, String note) {
|
||||
mName = name;
|
||||
mAbilityScoreName = abilityScoreName;
|
||||
mNote = note;
|
||||
}
|
||||
|
||||
public Skill(String name, AbilityScore abilityScore, AdvantageType advantageType, ProficiencyType proficiencyType) {
|
||||
this.name = name;
|
||||
this.abilityScore = abilityScore;
|
||||
this.advantageType = advantageType;
|
||||
this.proficiencyType = proficiencyType;
|
||||
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 modifier = monster.getAbilityModifier(abilityScore);
|
||||
switch (proficiencyType) {
|
||||
case PROFICIENT:
|
||||
return modifier + monster.getProficiencyBonus();
|
||||
case EXPERTISE:
|
||||
return modifier + monster.getProficiencyBonus() * 2;
|
||||
case NONE:
|
||||
default:
|
||||
return modifier;
|
||||
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(
|
||||
"%s%s %+d%s",
|
||||
name.charAt(0),
|
||||
name.substring(1),
|
||||
bonus,
|
||||
advantageType == AdvantageType.ADVANTAGE ? " A" : advantageType == AdvantageType.DISADVANTAGE ? " D" : ""
|
||||
);
|
||||
return String.format(Locale.US, "%s%s %d", mName.substring(0,1), mName.substring(1), bonus);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Skill o) {
|
||||
return this.name.compareToIgnoreCase(o.name);
|
||||
return this.getName().compareToIgnoreCase(o.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compare(Skill o1, Skill o2) {
|
||||
return o1.name.compareToIgnoreCase(o2.name);
|
||||
return o1.getName().compareToIgnoreCase(o2.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(@Nullable Object obj) {
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (!(obj instanceof Skill)) {
|
||||
return false;
|
||||
}
|
||||
Skill otherSkill = (Skill) obj;
|
||||
if (!Objects.equals(this.name, otherSkill.name)) {
|
||||
return false;
|
||||
}
|
||||
if (this.abilityScore != otherSkill.abilityScore) {
|
||||
return false;
|
||||
}
|
||||
if (this.advantageType != otherSkill.advantageType) {
|
||||
return false;
|
||||
}
|
||||
if (this.proficiencyType != otherSkill.proficiencyType) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ import com.majinnaibu.monstercards.R;
|
||||
import com.majinnaibu.monstercards.helpers.StringHelper;
|
||||
import com.majinnaibu.monstercards.models.Monster;
|
||||
import com.majinnaibu.monstercards.models.SavingThrow;
|
||||
import com.majinnaibu.monstercards.models.Skill;
|
||||
|
||||
@SuppressWarnings("FieldCanBeLocal")
|
||||
public class MonsterFragment extends Fragment {
|
||||
@@ -66,6 +67,9 @@ public class MonsterFragment extends Fragment {
|
||||
monster.addSavingThrow(new SavingThrow("int", 3));
|
||||
monster.addSavingThrow(new SavingThrow("wis", 4));
|
||||
monster.addSavingThrow(new SavingThrow("cha", 5));
|
||||
//Skills
|
||||
monster.addSkill(new Skill("perception", "wis"));
|
||||
monster.addSkill(new Skill("stealth", "dexterity"));
|
||||
|
||||
// Challenge Rating
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +34,8 @@ public class MonsterViewModel extends ViewModel {
|
||||
mCharisma.setValue("");
|
||||
mSavingThrows = new MutableLiveData<>();
|
||||
mSavingThrows.setValue("");
|
||||
mSkills = new MutableLiveData<>();
|
||||
mSkills.setValue("");
|
||||
}
|
||||
|
||||
private MutableLiveData<String> mName;
|
||||
@@ -84,6 +86,10 @@ public class MonsterViewModel extends ViewModel {
|
||||
public LiveData<String> getSavingThrows() {
|
||||
return mSavingThrows;
|
||||
}
|
||||
private MutableLiveData<String> mSkills;
|
||||
public LiveData<String> getSkills() {
|
||||
return mSkills;
|
||||
}
|
||||
|
||||
private Monster mMonster;
|
||||
public void setMonster(Monster monster) {
|
||||
@@ -100,5 +106,6 @@ public class MonsterViewModel extends ViewModel {
|
||||
mWisdom.setValue(monster.getWisdomDescription());
|
||||
mCharisma.setValue(monster.getCharismaDescription());
|
||||
mSavingThrows.setValue(monster.getSavingThrowsDescription());
|
||||
mSkills.setValue(monster.getSkillsDescription());
|
||||
}
|
||||
}
|
||||
@@ -318,5 +318,18 @@
|
||||
app:layout_constraintTop_toBottomOf="@+id/divider3"
|
||||
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>
|
||||
</ScrollView>
|
||||
Reference in New Issue
Block a user