Advertisement
Guest User

Entity.java

a guest
Nov 21st, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.77 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. /**
  4.  * A class to handle all living, breathing creatures
  5.  */
  6. public class Entity {
  7.     private String name;
  8.     private int HP;
  9.     private int MaxHP;          //Sets itself to whatever HP is on creation
  10.     private int STR;
  11.     private int hunger;         //Current hunger, starts at 100
  12.     private double mood;        //Current mood value
  13.     private double moodMult;    //Increase or decreases depending on external factors, used to determine mood
  14.     private boolean isAlive;
  15.     private ArrayList<Item> contains = new ArrayList<>();   //Inventory
  16.  
  17.     /**
  18.      * Constructor for Entity
  19.      *
  20.      * @param name The String name of this Entity
  21.      * @param HP   This Entity' initial HP, set to MaxHP as well
  22.      * @param STR  The Entity's physical strength
  23.      */
  24.     public Entity(String name, int HP, int STR) {
  25.         this.name = name;
  26.         this.HP = HP;
  27.         this.MaxHP = HP;
  28.         this.STR = STR;
  29.         this.hunger = 100;
  30.         this.mood = 100;
  31.         this.isAlive = true;
  32.     }
  33.  
  34.     /**
  35.      * Returns the contents of an Entity's inventory
  36.      */
  37.     public void inventory() {
  38.         System.out.println("The " + name + " contains: ");
  39.         if (contains.size() > 0) {                      //If there's at least one Item in the inventory
  40.             for (int i = 0; i < contains.size(); ) {    //Then print all the items
  41.                 System.out.println(contains.get(i));
  42.                 i++;
  43.             }
  44.         } else {
  45.             System.out.println("Nothing");      //If there's no items in the inventory say "nothing"
  46.         }
  47.     }
  48.  
  49.     /**
  50.      * Places an Item into the Entity's inventory
  51.      *
  52.      * @param that What Item is being added to the inventory
  53.      */
  54.     public void ItemGet(Item that) {
  55.         if (isAlive != false) {
  56.             System.out.println(name + " has picked up " + that);
  57.             contains.add(that);
  58.         } else {
  59.             System.out.println(name + " cannot pickup the " + that + " because " + name + " is dead.");
  60.         }
  61.     }
  62.  
  63.     /**
  64.      * Removes an Item into the Entity's inventory
  65.      *
  66.      * @param that What Item is being removed from the inventory
  67.      */
  68.     public void ItemDrop(Item that) {
  69.         if (isAlive != false) {
  70.             System.out.println(name + " has dropped " + that);
  71.             contains.remove(that);
  72.         } else {
  73.             System.out.println(name + " cannot drop the " + that + " because " + name + " is dead.");
  74.         }
  75.     }
  76.  
  77.     /**
  78.      * Represents a passage of time, instructs Entity to update hunger, mood, etc.
  79.      */
  80.     public void update() {
  81.         if (this.isAlive != false)           //While this Entity is alive
  82.         {
  83.             this.hunger -= 5;
  84.             this.mood -= moodMult;
  85.             System.out.println("Hunger: " + hunger + " Mood: " + mood);
  86.             hunger();
  87.             mood();
  88.         } else {
  89.             System.out.println(name + " is dead.");
  90.         }
  91.     }
  92.  
  93.     /**
  94.      * Handles the hunger of Entity, updates String to represent the Entity's hunger
  95.      * turns "isAlive" to false if hunger drops to zero or below
  96.      */
  97.     private void hunger() {
  98.         if (this.hunger >= 90) {
  99.             System.out.println(name + " isn't hungry");
  100.             moodMult = 0.5;
  101.         } else if (this.hunger <= 85 && this.hunger >= 60) {
  102.             moodMult = 0.5;
  103.             System.out.println(name + " is getting fairly hungry");
  104.         } else if (this.hunger < 60 && this.hunger >= 40) {
  105.             moodMult = 1.0;
  106.             System.out.println(name + " is very hungry");
  107.         } else if (this.hunger < 40 && this.hunger >= 20) {
  108.             System.out.println(name + " is extremely hungry");
  109.             moodMult = 5.0;
  110.         } else if (this.hunger <= 0) {
  111.             this.isAlive = false;
  112.             System.out.println(name + " has died of starvation.");
  113.         } else {
  114.             moodMult = 10.0;
  115.             System.out.println(name + " is starving!");
  116.         }
  117.     }
  118.  
  119.     /**
  120.      * Handles the mood of an Entity
  121.      */
  122.     private void mood() {
  123.         if (this.mood >= 90) {
  124.             System.out.println(name + " is very happy");
  125.         } else if (this.mood <= 89 && this.mood >= 60) {
  126.             System.out.println(name + " is happy");
  127.         } else if (this.mood < 60 && this.mood >= 40) {
  128.             System.out.println(name + " is feeling a little uncomfortable");
  129.         } else if (this.mood < 40 && this.mood >= 20) {
  130.             System.out.println(name + " is feeling sad");
  131.         } else if (this.mood <= 0) {
  132.             this.isAlive = false;
  133.             System.out.println(name + " is devoid of happiness.");
  134.         } else {
  135.             System.out.println(name + " is incredibly depressed!");
  136.         }
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement