SHARE
TWEET

Untitled

a guest Nov 28th, 2016 22 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.shawn;
  2.  
  3. import com.badlogic.gdx.math.Vector2;
  4. import com.github.czyzby.websocket.serialization.SerializationException;
  5. import com.github.czyzby.websocket.serialization.Transferable;
  6. import com.github.czyzby.websocket.serialization.impl.Deserializer;
  7. import com.github.czyzby.websocket.serialization.impl.Serializer;
  8. import com.github.czyzby.websocket.serialization.impl.Size;
  9.  
  10. public class Entity implements Transferable<Entity>{
  11.  
  12.     public Entity() {
  13.         //Register the entity ID.
  14.         ID = -1;
  15.     }
  16.    
  17.     public Entity(EntityPosition pos, final int ID) {
  18.         this.entity_pos = pos;
  19.         this.ID = ID;
  20.         //Set the starting position to the EntityPosition.
  21.         this.position = pos.get();
  22.     }
  23.    
  24.     public void updateEntity(EntityPosition pa){
  25.         this.entity_pos = pa;
  26.     }
  27.     public void setTarget(MousePosition pos){
  28.         target_location = pos;
  29.     }
  30.     public MousePosition getTarget(){
  31.         return target_location;
  32.     }
  33.    
  34.     private EntityPosition entity_pos;
  35.     private MousePosition target_location;
  36.     private Vector2 position;
  37.     public final int ID;
  38.    
  39.     public Vector2 getPosition(){
  40.         return position;
  41.     }
  42.    
  43.     //In future iterations, make a "Connections" class to handle updating.
  44.    
  45.     //speed is equal to distance / time
  46.     public void moveTo(EntityPosition target, float time, float delta){
  47.         Vector2 direction = target.get().cpy().sub(position).nor();
  48.         position.add(direction.scl((target.get().dst(position)/time) * delta));
  49.     }
  50.    
  51.     public void updateLocally(float delta){
  52.         if(position.dst2(entity_pos.get()) >= 1f){
  53.             moveTo(entity_pos, 1f/20f, 1f);
  54.         }
  55.     }
  56.    
  57.     @Override
  58.     public void serialize(Serializer serializer) throws SerializationException {
  59.         serializer.
  60.         serializeTransferable(entity_pos).
  61.         serializeInt(ID);
  62.     }
  63.  
  64.     @Override
  65.     public Entity deserialize(Deserializer deserializer) throws SerializationException {
  66.         return new Entity(
  67.                 deserializer.deserializeTransferable(new EntityPosition()),
  68.                 deserializer.deserializeInt()
  69.         );
  70.     }
  71.    
  72.    
  73.  
  74. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top