AtkinsSJ

Interactable.java

Jan 6th, 2014
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 KB | None | 0 0
  1. package uk.co.samatkins.roguelike.play;
  2.  
  3. import com.badlogic.gdx.graphics.Color;
  4. import com.badlogic.gdx.utils.Json;
  5. import com.badlogic.gdx.utils.JsonValue;
  6.  
  7. public class Interactable implements Json.Serializable {
  8.     public enum Type {
  9.         //          Description         Glyph   Color                   Solid   Opaque
  10.         DOOR_OPEN   ("an Open Door",    1,4,    new Color(0x6c2d00ff),  false,  false),
  11.         DOOR_CLOSED ("a Closed Door",   0,4,    new Color(0x6c2d00ff),  true,   true);
  12.        
  13.         public int col, row;
  14.         public Color color;
  15.         public boolean solid,
  16.                         opaque;
  17.         public String description;
  18.        
  19.         Type(String description, int col, int row, Color color, boolean solid, boolean opaque) {
  20.             this.description = description;
  21.             this.col = col;
  22.             this.row = row;
  23.             this.color = color;
  24.             this.solid = solid;
  25.             this.opaque = opaque;
  26.         }
  27.     }
  28.    
  29.     public int x,y;
  30.     public Type type;
  31.    
  32.     public Interactable() {
  33.        
  34.     }
  35.    
  36.     public Interactable(Type type) {
  37.         this.type = type;
  38.     }
  39.    
  40.     /**
  41.      * The actor attempts to interact with this Iteractable
  42.      * @param actor
  43.      * @return Whether the actor was successful.
  44.      */
  45.     public boolean interact(Creature actor) {
  46.         switch (type) {
  47.         case DOOR_CLOSED:
  48.             type = Type.DOOR_OPEN;
  49.             return true;
  50.         case DOOR_OPEN:
  51.             if (actor.world.getTileAt(x, y).creature != null) {
  52.                 return false;
  53.             }
  54.             type = Type.DOOR_CLOSED;
  55.             return true;
  56.         }
  57.        
  58.         return false;
  59.     }
  60.  
  61.     public String describe() {
  62.         return type.description;
  63.     }
  64.  
  65.     @Override
  66.     public void write(Json json) {
  67.         json.writeValue("x", x);
  68.         json.writeValue("y", y);
  69.         json.writeValue("type", type.name());
  70.     }
  71.  
  72.     @Override
  73.     public void read(Json json, JsonValue jsonData) {
  74.         x = jsonData.getInt("x");
  75.         y = jsonData.getInt("y");
  76.         type = Type.valueOf(jsonData.getString("type"));
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment