Thread: Better way to handle objects

Results 1 to 7 of 7
  1. #1 Different way to handle objects 
    Registered Member

    Join Date
    Aug 2008
    Posts
    2,823
    Thanks given
    362
    Thanks received
    448
    Rep Power
    965
    Code:
    /**
     * An object
     * @author Fritz
     *
     */
    public class Object {
    
    	/**
    	 * Object's id
    	 */
    	private int primaryId;
    	
    	/**
    	 * Object id that will replace the object after clicked
    	 */
    	private int secondaryId;
    	
    	/**
    	 * Duration the object will be transformed
    	 */
    	private int transformDuration;
    	
    	/**
    	 * Location of the object
    	 */
    	private Location loc;
    
    	/**
    	 * Actions when the object is clicked
    	 */
    	private Interaction[] actions;
    	
    	/**
    	 * Delay between interactions
    	 */
    	private final static long ACTION_DELAY = 1000;
    	
    	/**
    	 * Last interaction
    	 */
    	private static long lastAction;
    	
    	/**
    	 * If the object event has been triggered
    	 */
    	private static boolean triggered;
    	
    	/**
    	 * Creates an object
    	 * @param objectId - Object ID
    	 * @param actions - Actions to be performed when the object is clicked
    	 */
    	public Object(int objectId, Location loc, Interaction ... actions) {
    		this.primaryId = objectId;
    		this.loc = loc;
    		this.actions = actions;
    	}
    	
    	/**
    	 * Creates an object that will be replaced by the secondary object
    	 * and then reverted after the tick
    	 * @param primaryId - Object ID
    	 * @param secondaryId - Object to turn into
    	 * @param tick - Duration the object is transformed
    	 * @param actions - What happens when clicked on
    	 */
    	public Object(int primaryId, int secondaryId, int transformDuration,
    			Location loc, Interaction ... actions) {
    		this.primaryId = primaryId;
    		this.secondaryId = secondaryId;
    		this.transformDuration = transformDuration;
    		this.loc = loc;
    		this.actions = actions;
    	}
    	
    	/**
    	 * Spawns the object for a player
    	 * @param c - Player shown the object
    	 */
    	public void show(Client c) {
    		c.getPA().checkObjectSpawn(primaryId, loc.getX(), loc.getY(), 0, 10);
    	}
    	
    	/**
    	 * Called when the object is clicked on
    	 * If the object has a transformation, it will transform if
    	 * the interacction was successful
    	 * @param index - Option clicked (ex first = 1, second = 2 ...)
    	 */
    	public boolean interactWith(final Client c, int index) {
    		if (System.currentTimeMillis() - ACTION_DELAY <= lastAction
    				|| triggered)
    			return false;
    		index--;
    		lastAction = System.currentTimeMillis();
    		if (actions[index].interact(c)) {
    			triggered = true;
    			c.getPA().checkObjectSpawn(secondaryId, loc.getX(), loc.getY(), 0, 10);
    			if (transformDuration > 0) {
    				EventManager.getSingleton().addEvent(new Event() {
    	
    					@Override
    					public void execute(EventContainer container) {
    						show(c);
    						triggered = false;
    						container.stop();
    					}
    					
    				}, transformDuration);
    			} else
    				triggered = false;
    			return true;
    		}
    		return false;
    	}
    	
    	/**
    	 * Gets the object id
    	 * @return
    	 */
    	public int getObjectId() {
    		return primaryId;
    	}
    	
    	/**
    	 * Generates a hash
    	 * @return - Object hash
    	 */
    	public static int getHash(int id, Location loc) {
    		return loc.getX() + loc.getY() << 4 + id << 8;
    	}
    	
    	/**
    	 * Gets the object's location
    	 * @return - Object's location
    	 */
    	public Location getLoc() {
    		return loc;
    	}
    	
    	/**
    	 * An interaction with the object
    	 * @author Fritz
    	 *
    	 */
    	public interface Interaction {
    		
    		/**
    		 * Action on object click
    		 * @param c - Player interacting
    		 * @return - Whether the interaction was successful
    		 */
    		public abstract boolean interact(Client c);
    		
    	}
    }
    Code:
    	/**
    	 * Creates the area and initializes the objects
    	 */
    	public Area() {
    		initObjects();
    	}
    	
    	/**
    	 * List of objects in the area
    	 */
    	public HashMap<Integer, Object> objects = 
    			new HashMap<Integer, Object>();
    	
    	/**
    	 * Adds an object to the objects HashMap
    	 * @param object - Object to add to the map
    	 */
    	public void addObject(Object object) {
    		objects.put(Object.getHash(object.getObjectId(),
    				object.getLoc()), object);
    	}
    	
    	/**
    	 * Generates a hash for the inputs and attempts to locate an object
    	 * in the hashmap with the requested hash
    	 * @param objectId - ID of the object
    	 * @param loc - Location of the object
    	 * @return - Object corresponding to the hash
    	 */
    	public Object getObject(int objectId, Location loc) {
    		return objects.get(Object.getHash(objectId, loc));
    	}
    Code:
    		for (Area area : AreaHandler.areas)
    			if (area.getObject(objectType, new Location(obX, obY, c.heightLevel))
    					.interactWith(c, 1)) // 2 for 2nd click and 3 for 3rd click
    				return;
    Code:
    		for(Area area : AreaHandler.areas)
    			for (Object object : area.objects.values())
    				object.show(c);

    Usage:
    Code:
    	public void initObjects() {
    		addObject(new Object(2929, -1, 5000, new Location(3096, 9881, 0),
    				new Interaction() {
    					@Override
    					public boolean interact(Client c) {
    						c.sendMessage("You picked up the bones");
    						c.getItems().addItem(526, 1);
    						return true;
    					}
    				}
    		));
    	}
    Reply With Quote  
     

  2. #2  
    Banned

    Join Date
    Aug 2007
    Posts
    4,130
    Thanks given
    0
    Thanks received
    64
    Rep Power
    0
    Just went through the code, 10/10
    Reply With Quote  
     

  3. #3  
    Registered Member
    Zivik's Avatar
    Join Date
    Oct 2007
    Age
    28
    Posts
    4,421
    Thanks given
    891
    Thanks received
    1,527
    Rep Power
    3285
    Looks good. Thanks for this.
    Reply With Quote  
     

  4. #4  
    Boolean


    Join Date
    Feb 2014
    Posts
    733
    Thanks given
    91
    Thanks received
    22
    Rep Power
    253
    Lookin good.
    Reply With Quote  
     

  5. #5  
    Banned

    Join Date
    Dec 2011
    Posts
    967
    Thanks given
    234
    Thanks received
    208
    Rep Power
    0
    Looks really nice

    Spoiler for Opinion:

    I'd say something like this would be neater, inner class
    Code:
    	public class ObjectBuilder {
    		private int primaryId;
    		private int secondaryId;
    		private int transformationDuration;
    		public ObjectBuilder() {
    			
    		}
    		public ObjectBuilder primaryId(int primaryId) {
    			this.primaryId = primaryId;
    			return this;
    		}
    		public ObjectBuilder secondaryId(int secondaryId) {
    			this.secondaryId = secondaryId;
    			return this;
    		}
    		public ObjectBuilder transformationDuration(int t) {
    			this.transformationDuration = t;
    			return this;
    		}
    	}
    Code:
    	public class Object {
    		//...
    		public Object(ObjectBuilder o) {
    			this.primaryId = o.primaryId;
    			this.secondaryId = o.secondaryId;
    			this.transformationDuration = o.transformationDuration;
    			//...
    		}
    	}
    Something I read, effectively you won't have to memorize the order of the values, and you know exactly what's happening when you look back at the code

    new Object(int, int, int, Location, Interaction)

    You'd have

    new Object(new ObjectBuilder().primaryId(10).secondaryId(11).tran sformationDuration(5).location(new Location(x, y)).interactions(Interactions[]))

    Especially good for anything that's going to be open source.
    Reply With Quote  
     

  6. #6  
    Registered Member
    Dogreen's Avatar
    Join Date
    Jan 2014
    Posts
    600
    Thanks given
    55
    Thanks received
    142
    Rep Power
    105
    Seems great, good job
    Reply With Quote  
     

  7. #7  
    強い者は生き残る
    Ashpire's Avatar
    Join Date
    Mar 2012
    Age
    27
    Posts
    2,721
    Thanks given
    914
    Thanks received
    1,897
    Rep Power
    2231
    Nice good job
    Attached image
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. [PI] Better Way To Handle XP Rates?
    By The Reason in forum Snippets
    Replies: 15
    Last Post: 11-30-2013, 01:53 AM
  2. Better way to handle quest and there stages
    By Purple Helix in forum Snippets
    Replies: 8
    Last Post: 08-29-2013, 09:50 PM
  3. [667/***] Better way to handle staff
    By asteria-X in forum Tutorials
    Replies: 24
    Last Post: 07-19-2012, 03:18 AM
  4. 562 Better way to handle overloads
    By Makar in forum Snippets
    Replies: 17
    Last Post: 11-01-2011, 10:16 PM
  5. Adding a nice way to handle objects (emulous)
    By Livinglife in forum Tutorials
    Replies: 15
    Last Post: 03-23-2009, 10:15 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •