Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package formation;
- import java.util.ArrayList;
- import java.util.List;
- import net.minecraft.block.Block;
- import net.minecraft.entity.Entity;
- import net.minecraft.entity.EntityLiving;
- import net.minecraft.util.ChunkCoordinates;
- import net.minecraft.util.MathHelper;
- import net.minecraft.util.Vec3;
- import CoroAI.componentAI.ICoroAI;
- import CoroAI.forge.CoroAI;
- public class Formation {
- public double distMax = 128D;
- public List<ICoroAI> listEntities = new ArrayList<ICoroAI>();
- //Used for updating where the formation wants to go
- public ICoroAI leader;
- public EntityLiving leaderEnt;
- //Leader based stuff
- public Vec3 targ = null;
- public Vec3 pos = null;
- public double yaw;
- //rename this to shared target, other entities can set it if its null
- public EntityLiving leaderTarget;
- public double smoothYaw;
- public double speedSlowest = 0.28D;
- public PathNavigateFormation pathNav = null;
- public int sizeColumns = 10;
- public int sizeRows = 10;
- public static Formation newFormation(ICoroAI parLeader, ICoroAI secondEnt) {
- Formation fm = new Formation();
- fm.joinLeader(parLeader);
- if (secondEnt != null) fm.join(secondEnt);
- fm.pathNav = new PathNavigateFormation(fm.leaderEnt.width, fm.leaderEnt.height, fm.leaderEnt.worldObj, 256);
- System.out.println("new formation: " + fm);
- return fm;
- }
- public Formation() {
- }
- public void cleanup() {
- listEntities.clear();
- }
- //used for maintaining the active list of entities, if ones stray out and dont follow protocal and call leave, this could trim them out based on range
- public void tickUpdate() {
- //every n ticks
- //check list for
- //- distant entities
- //-
- sizeColumns = 10;
- sizeRows = 10;
- if (leaderTarget != null && leaderTarget.isDead) leaderTarget = null;
- if (leaderEnt != null) {
- pos = leaderEnt.getPosition(1F);
- yaw = leaderEnt.rotationYaw;
- if (leaderTarget == null) {
- leaderTarget = leaderEnt.getAttackTarget();
- }
- float bestMove = MathHelper.wrapAngleTo180_float((float) (smoothYaw - yaw));
- float camRotateSpeed = 0.6F/*bestMove * 0.03F*/;
- if (camRotateSpeed < 0.03F) camRotateSpeed = 0F;
- if (bestMove > camRotateSpeed * 1) {
- if (bestMove < camRotateSpeed * 2) {
- smoothYaw = yaw;
- } else {
- smoothYaw -= camRotateSpeed;
- }
- } else if (bestMove < camRotateSpeed * 1) {
- if (bestMove > camRotateSpeed * 2) {
- smoothYaw = yaw;
- } else {
- smoothYaw += camRotateSpeed;
- }
- }
- while (smoothYaw >= 180.0F) smoothYaw -= 360.0F;
- while (smoothYaw < -180.0F) smoothYaw += 360.0F;
- if (leaderEnt.worldObj.getWorldTime() % 40 == 0) {
- for (int i = 0; i < listEntities.size(); i++) {
- ICoroAI ent = listEntities.get(i);
- if (ent != null) {
- if (((EntityLiving)ent).isDead || ((EntityLiving)ent).getDistance(pos.xCoord, pos.yCoord, pos.zCoord) > distMax) {
- leave(ent);
- }
- }
- }
- }
- }
- }
- public void setDestination(ChunkCoordinates coords) {
- System.out.println("TODO");
- }
- //this should be called by entities already added, to get their position thats managed by this class
- public Vec3 getPosition(Entity ent) {
- if (listEntities.contains(ent)) {
- if (pos != null) {
- int squareSize = (int) Math.sqrt(listEntities.size());
- sizeColumns = squareSize;
- int index = listEntities.indexOf(ent);
- int row = index / sizeColumns;
- int col = index % sizeColumns;
- int rows = listEntities.size() / sizeColumns;
- double spacing = 1.5D/* + Math.sin(ent.worldObj.getWorldTime() * 0.1D)*/;
- double adjAngle = 0D;//listEntities.indexOf(ent) % 2 == 0 ? 0D : 180D;
- double dist = spacing * row - ((spacing*rows)/2);//(listEntities.indexOf(ent));
- adjAngle -= 90D;
- //depth wise, rows
- double posX = Math.cos((-smoothYaw + adjAngle) * 0.01745329D) * dist;
- double posZ = Math.sin((-smoothYaw + adjAngle) * 0.01745329D) * dist;
- dist = (spacing*col) - ((spacing*(sizeColumns+1))/2);
- adjAngle += 90D;
- //length wise, colums
- posX += Math.cos((-smoothYaw + adjAngle) * 0.01745329D) * dist;
- posZ += Math.sin((-smoothYaw + adjAngle) * 0.01745329D) * dist;
- posX = pos.xCoord - posX;
- double posY = (pos.yCoord/* - 0.3D - Math.sin((center.rotationPitch) / 180.0F * 3.1415927F) * dist*/);
- posZ = pos.zCoord + posZ;
- Vec3 tryPos = Vec3.createVectorHelper(posX, posY, posZ);
- int tryID = ent.worldObj.getBlockId((int)tryPos.xCoord, (int)tryPos.yCoord + 1, (int)tryPos.zCoord);
- //if clear (check ent height too), if not return center formation for safety
- if (tryID == 0 || !Block.blocksList[tryID].blockMaterial.isSolid()) {
- } else {
- return pos;
- }
- //to fix movehelper trying to make them jump, might need a better fix
- tryPos = Vec3.createVectorHelper(posX, ent.posY, posZ);
- return tryPos;
- } else {
- //System.out.println("DO I REPEAT FOR SAME ENT?!" + ent);
- return null;
- }
- } else {
- return null;
- }
- }
- public void joinLeader(ICoroAI ent) {
- join(ent);
- leader = ent;
- leaderEnt = (EntityLiving)ent;
- System.out.println("Formation: leader assigned to " + this);
- }
- public void join(ICoroAI ent) {
- listEntities.add(ent);
- ent.getAIAgent().activeFormation = this;
- System.out.println("Formation: ent joined " + this);
- }
- public void leave(ICoroAI ent) {
- listEntities.remove(ent);
- ent.getAIAgent().activeFormation = null;
- checkLeader(ent);
- }
- public void checkLeader(ICoroAI ent) {
- if (ent == leader && listEntities.size() > 0) {
- System.out.println("Formation: new leader assigned!");
- leader = listEntities.get(0);
- leaderEnt = (EntityLiving)leader;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment