public void setCockpit(Body newCockpit) {
   if (cockpit != null) {
     if (cockpit.getUserData() instanceof ControlledBodyData) {
       ((ControlledBodyData) cockpit.getUserData()).removeSubPart(body);
     }
   }
   if (newCockpit != null) {
     ((ControlledBodyData) cockpit.getUserData()).addSubPart(body);
   }
   cockpit = newCockpit;
   calcDistFromCockpit();
 }
 public void startTick() {
   if (parent != null) {
     if (parent.getUserData() instanceof LivingBodyData) {
       if (((LivingBodyData) parent.getUserData()).health <= 0) health = 0;
     } else health = 0;
   }
   if (health <= 0) return;
   if (scripts == null) return;
   try {
     Game.e.put("scriptid", scripts.data.UUID);
     Game.e.eval(scripts.tickScript);
   } catch (ScriptException e) {
     e.printStackTrace();
   }
 }
 public void calcDistFromCockpit() {
   if (cockpit == null) return;
   distFromCockpit = -1;
   if (cockpit == body) distFromCockpit = 0;
   for (JointEdge j = body.getJointList(); j != null; j = j.next) {
     Body otherBody = j.other;
     if (otherBody.getUserData() instanceof ControlledBodyData) {
       ControlledBodyData data = (ControlledBodyData) otherBody.getUserData();
       if (data.cockpit == cockpit && data.distFromCockpit != -1) {
         data.calcDistFromCockpit();
         if (data.distFromCockpit != -1 && data.distFromCockpit < distFromCockpit)
           distFromCockpit = data.distFromCockpit;
       }
     }
   }
   if (distFromCockpit == -1) setCockpit(null);
 }
 public double requestResource(String name, double amount, boolean acceptPartial) {
   if (cockpit != null && cockpit != body) {
     if (cockpit.getUserData() instanceof ControlledBodyData) {
       ControlledBodyData data = (ControlledBodyData) cockpit.getUserData();
       return data.requestResource(name, amount, acceptPartial);
     }
   }
   if (acceptPartial) {
     double collected = removeResource(name, amount);
     int size = subparts.size();
     for (int a = 0; a < size && collected < amount; a++) {
       Body subpart = subparts.get(a);
       if (subpart.getUserData() instanceof ControlledBodyData) {
         ControlledBodyData data = (ControlledBodyData) subpart.getUserData();
         collected += data.removeResource(name, amount);
       }
     }
     return collected;
   } else {
     if (getResource(name).amount >= amount) return removeResource(name, amount);
     int size = subparts.size();
     for (int a = 0; a < size; a++) {
       Body subpart = subparts.get(a);
       if (subpart.getUserData() instanceof ControlledBodyData) {
         ControlledBodyData data = (ControlledBodyData) subpart.getUserData();
         if (data.getResource(name).amount >= amount) return data.removeResource(name, amount);
       }
     }
     return 0;
   }
 }
 public double depositResource(String name, double amount, boolean acceptPartial) {
   if (cockpit != null && cockpit != body) {
     if (cockpit.getUserData() instanceof ControlledBodyData) {
       ControlledBodyData data = (ControlledBodyData) cockpit.getUserData();
       return data.depositResource(name, amount, acceptPartial);
     }
   }
   if (acceptPartial) {
     double collected = addResource(name, amount, true);
     int size = subparts.size();
     for (int a = 0; a < size && collected < amount; a++) {
       Body subpart = subparts.get(a);
       if (subpart.getUserData() instanceof ControlledBodyData) {
         ControlledBodyData data = (ControlledBodyData) subpart.getUserData();
         collected += data.addResource(name, amount - collected, true);
       }
     }
     return collected;
   } else {
     double deposited = addResource(name, amount, false);
     if (deposited != 0) return amount;
     int size = subparts.size();
     for (int a = 0; a < size; a++) {
       Body subpart = subparts.get(a);
       if (subpart.getUserData() instanceof ControlledBodyData) {
         ControlledBodyData data = (ControlledBodyData) subpart.getUserData();
         if (data.allowedResource(name)) {
           deposited = data.addResource(name, amount, false);
           if (deposited != 0) return amount;
         }
       }
     }
     return 0;
   }
 }
Пример #6
0
 @Override
 public boolean onTouchEvent(MotionEvent event) {
   Vec2 p = new Vec2(event.getX(), event.getY());
   switch (event.getAction()) {
     case MotionEvent.ACTION_DOWN:
       break;
     case MotionEvent.ACTION_UP:
       if (mouseJoint != null) {
         myWorld.getWorld().destroyJoint(mouseJoint);
         mouseJoint = null;
       }
       break;
     case MotionEvent.ACTION_MOVE:
       if (mouseJoint != null) {
         mouseJoint.setTarget(p);
         break;
       }
       queryAABB.lowerBound.set(p.x - .001f, p.y - .001f);
       queryAABB.upperBound.set(p.x + .001f, p.y + .001f);
       callback.point.set(p);
       callback.fixture = null;
       myWorld.getWorld().queryAABB(callback, queryAABB);
       if (callback.fixture != null) {
         Body body = callback.fixture.getBody();
         XKPPhysicBody physicBody = (XKPPhysicBody) body.getUserData();
         if (!physicBody.getMouseJoint()) break;
         MouseJointDef def = new MouseJointDef();
         def.bodyA = groundBody;
         def.bodyB = body;
         def.target.set(p);
         def.maxForce = 3000f * body.getMass();
         def.dampingRatio = 0;
         def.frequencyHz = 1000;
         mouseJoint = (MouseJoint) myWorld.getWorld().createJoint(def);
         body.setAwake(true);
       }
       break;
   }
   return super.onTouchEvent(event);
 }