Class Player
java.lang.Object
model.entity.Entity
model.entity.Player
- Direct Known Subclasses:
AlienPlayer,ArcherPlayer,KnightPlayer,RebornPlayer
Base class for all 4 player characters (Knight, Archer, Reborn, Alien).
Manages inventory, skills, movement, and the defend system.
calculateAttack() is left to subclasses to override because the values differ.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected intColumn position on the map.List of items currently held.protected booleanWhether the player is currently defending (halves incoming damage this turn).protected intMaximum number of items that can be held; default is 4.protected intPlayer number (1 or 2).protected intRow position on the map.protected intIndex of the selected item in the inventory.protected SkillThe character's special skill.Fields inherited from class model.entity.Entity
attackPower, baseAttackPower, baseDefense, currentHp, defense, maxHp, name -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanChecks whether this character can move to the given cell.intgetCol()intintintgetRow()intgetSkill()booleanbooleanbooleanisValidMove(int fromRow, int fromCol, int toRow, int toCol) Checks whether moving from (fromRow, fromCol) to (toRow, toCol) is valid.voidmove(int newRow, int newCol) Moves the character to a new position on the map.booleanpickUpItem(Item item) Picks up an item and adds it to the inventory if there is space.voidsetDefending(boolean d) Sets the defending state.voidsetPlayerNumber(int n) Sets the player number.voidsetSelectedItemIndex(int index) Selects an item by its index in the inventory.voidtakeDamage(int dmg) Takes damage, halving it first if the player is currently defending.booleanUses the first item in the inventory and removes it.Methods inherited from class model.entity.Entity
calculateAttack, getAttackPower, getCurrentHp, getDefense, getDescription, getMaxHp, getName, heal, isAlive, resetAttackPower, resetDefense, setAttackPower, setBaseAttackPower, setBaseDefense, setCurrentHp, setDefense, setMaxHp
-
Field Details
-
row
protected int rowRow position on the map. -
col
protected int colColumn position on the map. -
inventory
-
maxInventorySize
protected int maxInventorySizeMaximum number of items that can be held; default is 4. -
skill
The character's special skill. -
isDefending
protected boolean isDefendingWhether the player is currently defending (halves incoming damage this turn). -
playerNumber
protected int playerNumberPlayer number (1 or 2). -
selectedItemIndex
protected int selectedItemIndexIndex of the selected item in the inventory.
-
-
Constructor Details
-
Player
Creates a player with the specified starting stats.- Parameters:
name- Character namemaxHp- Maximum HPcurrentHp- Starting HPattackPower- Starting attack powerdefense- Starting defenseskill- The character's skill
-
-
Method Details
-
takeDamage
public void takeDamage(int dmg) Takes damage, halving it first if the player is currently defending. The defending state is always reset to false after damage is applied.- Overrides:
takeDamagein classEntity- Parameters:
dmg- Raw damage before reduction
-
canMoveTo
Checks whether this character can move to the given cell. By default, only walkable cells (not ROCK, RIVER, or TREE) are allowed. Subclasses such as Archer or Reborn override this to add exceptions.- Parameters:
cell- Target cell- Returns:
trueif movement is allowed
-
isValidMove
public boolean isValidMove(int fromRow, int fromCol, int toRow, int toCol) Checks whether moving from (fromRow, fromCol) to (toRow, toCol) is valid. By default, only straight-line moves of exactly 1 step are allowed (no diagonal). Alien overrides this to permit diagonal movement.- Parameters:
fromRow- Starting rowfromCol- Starting columntoRow- Destination rowtoCol- Destination column- Returns:
trueif the move is valid
-
move
public void move(int newRow, int newCol) Moves the character to a new position on the map.- Parameters:
newRow- New rownewCol- New column
-
pickUpItem
Picks up an item and adds it to the inventory if there is space.- Parameters:
item- Item to pick up- Returns:
trueif picked up successfully,falseif inventory is full
-
useFirstItem
public boolean useFirstItem()Uses the first item in the inventory and removes it.- Returns:
trueif successful,falseif the inventory is empty
-
getRow
public int getRow()- Returns:
- Current row on the map
-
getCol
public int getCol()- Returns:
- Current column on the map
-
getInventory
-
getSkill
- Returns:
- The character's skill
-
isDefending
public boolean isDefending()- Returns:
trueif the player is defending this turn
-
setDefending
public void setDefending(boolean d) Sets the defending state.- Parameters:
d-trueto start defending
-
getPlayerNumber
public int getPlayerNumber()- Returns:
- Player number (1 or 2).
-
setPlayerNumber
public void setPlayerNumber(int n) Sets the player number.- Parameters:
n- Player number (1 or 2)
-
getMaxInventorySize
public int getMaxInventorySize()- Returns:
- Maximum number of items that can be held
-
isInventoryFull
public boolean isInventoryFull()- Returns:
trueif the inventory is full
-
getSelectedItemIndex
public int getSelectedItemIndex()- Returns:
- Index of the currently selected item
-
setSelectedItemIndex
public void setSelectedItemIndex(int index) Selects an item by its index in the inventory.- Parameters:
index- Index of the item in the inventory
-