Added event method, changed Fireball to projectile type

This commit is contained in:
Anuken
2018-05-09 10:52:51 -04:00
parent 55cd0c2bae
commit 83d1707b56
11 changed files with 109 additions and 79 deletions

View File

@@ -5,32 +5,54 @@ import com.badlogic.gdx.utils.reflect.ClassReflection;
import com.badlogic.gdx.utils.reflect.Method;
import com.badlogic.gdx.utils.reflect.ReflectionException;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.game.Team;
import io.anuke.mindustry.net.Net.SendMode;
import io.anuke.mindustry.net.Packets.InvokePacket;
import io.anuke.mindustry.world.Tile;
import io.anuke.ucore.entities.Entities;
import io.anuke.ucore.entities.Entity;
import io.anuke.ucore.util.IOUtils;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.nio.ByteBuffer;
/**Class for invoking static methods of other classes remotely.*/
public class Invoke {
private static ObjectMap<Class, ObjectMap<String, Method>> methods = new ObjectMap<>();
private static ObjectMap<String, Class> classes = new ObjectMap<>();
/**
* Invokes a method remotely (on all clients) and locally.
* @param type Type of class to invoke method on
* @param methodName Name of method to invoke. Overloads are not allowed!
* @param args List of arguments to invoke the method with.
*/
public static void on(Class<?> type, String methodName, Object... args){
try {
Method method = getMethod(type, methodName);
method.invoke(null, args);
InvokePacket packet = new InvokePacket();
packet.args = args;
packet.type = type;
packet.method = method;
packet.args = args;
Net.send(packet, SendMode.tcp);
method.invoke(null, args);
}catch (ReflectionException e){
throw new RuntimeException(e);
}
}
/**
* Invokes an event in the {@link NetEvents} class by name.
* @param methodName the name of the event method to invoke
* @param args the method arguments
*/
public static void event(String methodName, Object... args){
on(NetEvents.class, methodName, args);
}
//TODO refactor to serializer map!
static void writeObjects(ByteBuffer buffer, Object[] objects){
for(Object o : objects){
Class type = o.getClass();
@@ -51,6 +73,11 @@ public class Invoke {
buffer.putShort((Short)o);
}else if(type == Tile.class){
buffer.putInt(((Tile)o).packedPosition());
}else if(type == Entity.class){
buffer.put((byte)((Entity)o).getGroup().getID());
buffer.putInt(((Entity)o).id);
}else if(type == Team.class){
buffer.put((byte)((Team)o).ordinal());
}else if(type == String.class){
IOUtils.writeString(buffer, (String)o);
}
@@ -79,6 +106,12 @@ public class Invoke {
obj = buffer.getShort();
}else if(type == Tile.class){
obj = Vars.world.tile(buffer.getInt());
}else if(type == Entity.class){
byte group = buffer.get();
int id = buffer.getInt();
obj = Entities.getGroup(group).getByID(id);
}else if(type == Team.class){
obj = Team.values()[buffer.get()];
}else if(type == String.class){
obj = IOUtils.readString(buffer);
}
@@ -113,7 +146,10 @@ public class Invoke {
Method method = map.get(methodname);
if(method == null){
method = ClassReflection.getMethod(type, methodname, null);
method = ClassReflection.getMethod(type, methodname, (Class[])null);
if(method.getDeclaredAnnotation(Remote.class) == null){
throw new RuntimeException("Attempt to invoke method '" + methodname + "', which is not Invokable!");
}
map.put(methodname, method);
}
@@ -122,4 +158,9 @@ public class Invoke {
}
@Retention(RetentionPolicy.RUNTIME)
@interface Remote{
}
}

View File

@@ -5,7 +5,6 @@ import com.badlogic.gdx.utils.TimeUtils;
import com.badlogic.gdx.utils.reflect.ClassReflection;
import com.badlogic.gdx.utils.reflect.Method;
import com.badlogic.gdx.utils.reflect.ReflectionException;
import com.sun.tools.internal.ws.wsdl.document.Import;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.entities.Player;
import io.anuke.mindustry.entities.SyncEntity;