refactor: generated equals, hashCode and toString for entity classes

This commit is contained in:
Yannik Bretschneider 2021-05-11 21:17:06 +02:00
parent 1cb2c145d1
commit 28ac25fa55
5 changed files with 131 additions and 0 deletions

View File

@ -2,6 +2,8 @@ package uulm.teamname.marvelous.gamelibrary.entities;
import uulm.teamname.marvelous.gamelibrary.IntVector2;
import java.util.Objects;
/** Represents a playable character inside a match. */
public class Character extends Entity {
/** The name of the character */
@ -72,4 +74,33 @@ public class Character extends Entity {
clone.ap.setValue(ap.getValue());
return clone;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Character character = (Character) o;
return attackRange == character.attackRange && rangedDamage == character.rangedDamage && meleeDamage == character.meleeDamage && Objects.equals(name, character.name) && Objects.equals(hp, character.hp) && Objects.equals(mp, character.mp) && Objects.equals(ap, character.ap) && Objects.equals(inventory, character.inventory);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), name, hp, mp, ap, attackRange, rangedDamage, meleeDamage, inventory);
}
@Override
public String toString() {
return "Character{" +
"name='" + name + '\'' +
", hp=" + hp +
", mp=" + mp +
", ap=" + ap +
", attackRange=" + attackRange +
", rangedDamage=" + rangedDamage +
", meleeDamage=" + meleeDamage +
", inventory=" + inventory +
'}';
}
}

View File

@ -2,6 +2,8 @@ package uulm.teamname.marvelous.gamelibrary.entities;
import uulm.teamname.marvelous.gamelibrary.IntVector2;
import java.util.Objects;
/** Represents an infinity stone {@link Entity}. Can only exist on the map. */
public class InfinityStone extends Entity {
/** The {@link StoneType} of the infinity stone */
@ -24,4 +26,26 @@ public class InfinityStone extends Entity {
public InfinityStone clone() {
return new InfinityStone(id, position, type);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
InfinityStone that = (InfinityStone) o;
return type == that.type;
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), type);
}
@Override
public String toString() {
return "InfinityStone{" +
"type=" + type +
'}';
}
}

View File

@ -3,6 +3,7 @@ package uulm.teamname.marvelous.gamelibrary.entities;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Objects;
/** Represents an inventory of 6 slots of {@link StoneType}s that can be manipulated. */
public class Inventory implements Iterable<StoneType> {
@ -79,4 +80,27 @@ public class Inventory implements Iterable<StoneType> {
public Iterator<StoneType> iterator() {
return content.iterator();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Inventory that = (Inventory) o;
return size == that.size && Objects.equals(content, that.content);
}
@Override
public int hashCode() {
return Objects.hash(size, content);
}
@Override
public String toString() {
return "Inventory{" +
"size=" + size +
", content=" + content +
'}';
}
}

View File

@ -2,6 +2,8 @@ package uulm.teamname.marvelous.gamelibrary.entities;
import uulm.teamname.marvelous.gamelibrary.IntVector2;
import java.util.Objects;
/** Represents a rock entity on the map. */
public class Rock extends Entity {
/** The maximum hp of the rock */
@ -38,4 +40,29 @@ public class Rock extends Entity {
clone.hp = hp;
return clone;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Rock rock = (Rock) o;
return maxHP == rock.maxHP && hp == rock.hp;
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), maxHP, hp);
}
@Override
public String toString() {
return "Rock{" +
"maxHP=" + maxHP +
", hp=" + hp +
'}';
}
}

View File

@ -1,5 +1,7 @@
package uulm.teamname.marvelous.gamelibrary.entities;
import java.util.Objects;
/** Represents a stat property of a {@link Character}. */
public class Stat {
/** The {@link StatType} of the stat */
@ -37,4 +39,27 @@ public class Stat {
public void decreaseValue(int value) {
this.value -= value;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Stat stat = (Stat) o;
return max == stat.max && value == stat.value && type == stat.type;
}
@Override
public int hashCode() {
return Objects.hash(type, max, value);
}
@Override
public String toString() {
return "Stat{" +
"type=" + type +
", max=" + max +
", value=" + value +
'}';
}
}