package org.inaetics.dronessimulator.drone.components.gun;

import org.inaetics.dronessimulator.common.D3PoolCoordinate;
import org.inaetics.dronessimulator.common.D3Vector;
import org.inaetics.dronessimulator.common.protocol.*;
import org.inaetics.dronessimulator.drone.components.gps.GPS;
import org.inaetics.dronessimulator.drone.droneinit.DroneInit;
import org.inaetics.dronessimulator.pubsub.api.Message;
import org.inaetics.dronessimulator.pubsub.api.publisher.Publisher;
import org.inaetics.dronessimulator.pubsub.api.subscriber.Subscriber;

import java.io.IOException;
import java.util.UUID;

/**
 * Created by mart on 17-5-17.
 */
public class Gun {
    private volatile Subscriber m_subscriber;
    private volatile Publisher m_publisher;
    private volatile DroneInit m_drone;
    private volatile GPS m_gps;
    private final double GUN_SPEED = 50.0;
    private final double MAX_DISTANCE = 100;


    public double getMaxDistance(){
        return MAX_DISTANCE;
    }

    public void fireBullet(D3PoolCoordinate direction){
        FireBulletMessage msg = new FireBulletMessage();
        msg.setDamage(100);
        msg.setFiredById(m_drone.getIdentifier());
        msg.setIdentifier(UUID.randomUUID().toString());
        msg.setType(EntityType.BULLET);
        msg.setDirection(direction);
        msg.setVelocity(direction.toVector().scale(GUN_SPEED / direction.toVector().length()));
        msg.setPosition(m_gps.getPosition());
        msg.setAcceleration(new D3Vector());

        try{
            m_publisher.send(MessageTopic.MOVEMENTS, msg);
        } catch(IOException e){
            e.printStackTrace();
        }
    }
}

@@ -814,16 +814,50 @@
  m_gps;%0A
+    private long last_shot_at_ms;%0A
     priv
@@ -936,16 +936,66 @@
  = 100;%0A
+    private final double SHOT_TIME_BETWEEN = 1000;
 %0A%0A    pu
@@ -1060,16 +1060,113 @@
 %0A    %7D%0A%0A
+%0A    public long msSinceLastShot()%7B return this.last_shot_at_ms - System.currentTimeMillis(); %7D%0A%0A
     publ
@@ -1213,16 +1213,78 @@
 ction)%7B%0A
+        if (this.msSinceLastShot() %3E= SHOT_TIME_BETWEEN)%7B%0A    
         
@@ -1332,32 +1332,36 @@
 sage();%0A        
+    
 msg.setDamage(10
@@ -1364,16 +1364,20 @@
 e(100);%0A
+    
         
@@ -1423,24 +1423,28 @@
 ));%0A        
+   
+ 
 msg.setIdent
@@ -1484,24 +1484,28 @@
 ));%0A        
+    
 msg.setType(
@@ -1524,32 +1524,36 @@
 ULLET);%0A        
+ 
+   
 msg.setDirection
@@ -1569,24 +1569,28 @@
 n);%0A        
+   
+ 
 msg.setVeloc
@@ -1669,24 +1669,28 @@
 ));%0A        
+    
 msg.setPosit
@@ -1719,24 +1719,28 @@
 ));%0A        
+ 
+   
 msg.setAccel
@@ -1765,16 +1765,84 @@
 or());%0A%0A
+            this.last_shot_at_ms = System.currentTimeMillis();%0A%0A    
         
@@ -1858,16 +1858,20 @@
         
+   
+ 
 m_publis
@@ -1917,16 +1917,20 @@
         
+    
 %7D catch(
@@ -1957,16 +1957,20 @@
         
+ 
+   
 e.printS
@@ -1978,24 +1978,38 @@
 ackTrace();%0A
+            %7D%0A
         %7D%0A  

Set time between shots.