public void ShowBot( GameObject botPrefab ) { // clear any previous state Clear(); // spawn the bot (at current sandbox location) spawnedBot = Object.Instantiate(botPrefab, transform.position, transform.rotation); // set layer for all child parts var layer = LayerMask.NameToLayer("sandbox"); var transforms = PartUtil.GetComponentsInChildren <Transform>(spawnedBot); for (var i = 0; i < transforms.Length; i++) { transforms[i].gameObject.layer = layer; } var materialDistributor = spawnedBot.GetComponent <MaterialDistributor>(); if (materialDistributor != null) { materialDistributor.materialTag = botMaterial; } // attach the AI controller var ai = spawnedBot.AddComponent <OnOffAIController>(); ai.randomize = true; ai.delay = 2f; }
public override void Apply(PartConfig config, GameObject target) { if (target == null) { return; } // find rigid body gameobject under target var rigidbodyGo = PartUtil.GetBodyGo(target); if (rigidbodyGo == null) { return; } // add rigid body properties var rigidbody = rigidbodyGo.GetComponent <Rigidbody>(); if (rigidbody == null) { return; } rigidbody.mass = mass; rigidbody.drag = drag; rigidbody.angularDrag = angularDrag; }
// find the first component in children of part, following child link if necessary public static T GetComponentInChildren <T>(GameObject rootPartGo) { // find component of specified type in root's tree var component = rootPartGo.GetComponentInChildren <T>(); if (component != null) { return(component); } // find any part child link components in root var childLinks = rootPartGo.GetComponentsInChildren <ChildLink>(); for (var i = 0; i < childLinks.Length; i++) { // if part has external parts reference if (childLinks[i].childGo != null) { component = PartUtil.GetComponentInChildren <T>(childLinks[i].childGo); if (component != null) { return(component); } } } return(default(T)); }
void Clear() { if (partGo != null) { PartUtil.DestroyPartGo(partGo); partGo = null; } }
// Use this for initialization void Awake() { // assign player tag var rootGo = PartUtil.GetRootGo(gameObject); if (rootGo != null) { rootGo.tag = "enemy"; } }
void Discover() { var turretActuator = PartUtil.GetComponentInChildren <TurretActuator>(gameObject); if (turretActuator != null) { turretGo = turretActuator.gameObject; Debug.Log("turretGo: " + turretGo); } }
void Discover() { int totalHealth = 0; var components = PartUtil.GetComponentsInChildren <Health>(gameObject); for (var i = 0; i < components.Length; i++) { totalHealth += components[i].maxHealth; } Debug.Log(gameObject.name + " # components: " + components.Length + " totalHealth: " + totalHealth); }
void Discover() { float totalMass = 0; var rbs = PartUtil.GetComponentsInChildren <Rigidbody>(gameObject); for (var i = 0; i < rbs.Length; i++) { totalMass += rbs[i].mass; } Debug.Log(gameObject.name + " # rbs: " + rbs.Length + " totalMass: " + totalMass); }
public void SetMaterials(MaterialTag tag) { materialTag = tag; var actuators = PartUtil.GetComponentsInChildren <MaterialActuator>(gameObject); if (actuators == null) { return; } for (var i = 0; i < actuators.Length; i++) { actuators[i].Assign(tag); } }
public override GameObject Build( PartConfig config, GameObject root, string label ) { GameObject partsGo = null; GameObject bodyGo = null; GameObject steeringGo = null; GameObject hubBodyGo = null; // build out part model first partsGo = base.Build(config, root, label); bodyGo = partsGo.transform.Find(partsGo.name + ".body").gameObject; // now build out wheel parts hierarchy // frame is instantiated under parts.body if (frame != null) { frame.Build(config, bodyGo, "frame"); } // steering goes next (if specified) under parts if (steering != null) { steeringGo = steering.Build(config, partsGo, "steering"); } // hub/wheel goes next under parts if (hub != null) { var hubGo = hub.Build(config, (steeringGo != null) ? steeringGo : partsGo, "hub"); // if hub part isn't specified, build dummy hub rigidbody for wheel if (hubGo == null) { hubBodyGo = PartUtil.BuildGo(config, partsGo, "hub.body", typeof(Rigidbody), typeof(KeepInBounds)); } else { hubBodyGo = PartUtil.GetBodyGo(hubGo); } if (wheel != null) { wheel.Build(config, hubBodyGo, "wheel"); } } return(partsGo); }
public override void Apply(PartConfig config, GameObject target) { if (target == null) { return; } // find rigid body gameobject under target var rigidbodyGo = PartUtil.GetBodyGo(target); if (rigidbodyGo == null) { return; } // add fixed joint component to target var joint = ApplyJoint(rigidbodyGo, config, target); if (joint == null) { return; } //joint.enablePreprocessing = false; // apply break limits, as specified if (applyBreakForce) { joint.breakForce = breakForce; } if (applyBreakTorque) { joint.breakTorque = breakTorque; } // add damage actuator to joint rigidbody if joint can be damaged if (applyDamageToForce || applyDamageToTorque) { var actuator = rigidbodyGo.AddComponent <JointDamageActuator>(); actuator.applyDamageToForce = applyDamageToForce; actuator.applyDamageToTorque = applyDamageToTorque; actuator.gameEventChannel = gameEventChannel; } // add simple joiner script to target, allowing quick joint join var joiner = target.AddComponent <Joiner>(); joiner.joint = joint; }
public static GameRecordEvent GetEventChannel( GameObject gameObject ) { var rootGo = PartUtil.GetRootGo(gameObject); if (rootGo != null) { var botComp = rootGo.GetComponent <BotBuilder>(); if (botComp != null) { return(botComp.eventChannel); } } return(null); }
void Start() { sparksPrefab = (GameObject)Resources.Load("Sparks"); // link health to our on percent change handler health = GetComponent <Health>(); if (health == null) { health = PartUtil.GetComponentInParentBody <Health>(gameObject); } if (health != null) { health.onChangePercent.AddListener(OnHealthPercentChange); } materialActuators = PartUtil.GetComponentsInChildren <MaterialActuator>(gameObject); rb = GetComponent <Rigidbody>(); }
public override void Apply(PartConfig config, GameObject target) { if (target == null) { return; } // find rigid body gameobject under target var rigidbodyGo = PartUtil.GetBodyGo(target); if (rigidbodyGo == null) { return; } // apply impact damage actuator if (impactApplyDamage) { var actuator = rigidbodyGo.AddComponent <ImpactDamageActuator>(); actuator.minDamage = impactMinDamage; actuator.maxDamage = impactMaxDamage; actuator.damageModifier = impactDamageModifier; actuator.emitScrews = impactScrews; actuator.minScrewDamage = impactScrewDamage; actuator.smallImpactSfx = smallImpactSfx; actuator.smallImpactSfxThreshold = smallImpactSfxThreshold; actuator.mediumImpactSfx = mediumImpactSfx; actuator.mediumImpactSfxThreshold = mediumImpactSfxThreshold; actuator.largeImpactSfx = largeImpactSfx; actuator.largeImpactSfxThreshold = largeImpactSfxThreshold; actuator.debug = debug; } // apply fire damage applicator if (fireApplyDamage) { var actuator = rigidbodyGo.AddComponent <FlameDamageActuator>(); actuator.fireRate = fireRate; actuator.fireDamageDelay = fireDamageDelay; //actuator.plume = plume; //actuator.sparks = sparks; actuator.burntMaterial = burntMaterial; actuator.burnThreshold = burnThreshold; actuator.explodeThreshold = explodeThreshold; actuator.debug = debug; } }
void OnCollisionEnter(Collision coll) { if (spinPower > 0f) { // apply collision force if we hit a rigidbody var rbColl = coll.rigidbody; if (rbColl != null) { // don't apply collision force to self if (PartUtil.GetRootGo(gameObject) != PartUtil.GetRootGo(coll.gameObject)) { var f = (-transform.right) * throwForceLateral * spinPower + transform.up * throwForceUpward * spinPower; //Debug.Log("applying f: " + f + " to " + rbColl.gameObject.name); rbColl.AddForceAtPosition(f, coll.contacts[0].point); } } } }
void Discover() { leftScripts = new List <IMotorActuator>(); rightScripts = new List <IMotorActuator>(); var scripts = PartUtil.GetComponentsInChildren <IMotorActuator>(gameObject); for (var i = 0; i < scripts.Length; i++) { if (scripts[i].left) { leftScripts.Add(scripts[i]); } else { rightScripts.Add(scripts[i]); } } }
void Start() { rootGo = PartUtil.GetRootGo(gameObject); health = GetComponent <Health>(); if (health == null) { health = PartUtil.GetComponentInParentBody <Health>(gameObject); } // enforce max damage > min damage if (maxDamage < minDamage) { maxDamage = minDamage; } if (emitScrews) { screwBurstPrefab = (GameObject)Resources.Load("ScrewBurst"); } }
void Discover() { totalHealth = 0; healthModules = new List <Health>(); var components = PartUtil.GetComponentsInChildren <Health>(gameObject); for (var i = 0; i < components.Length; i++) { // only consider bot modules for overall bot health if (components[i].healthTag == HealthTag.Module) { totalHealth += components[i].maxHealth; healthModules.Add(components[i]); components[i].onTakeDamage.AddListener(OnTakeDamage); } } minHealth = (totalHealth * deathHealthPercent) / 100; Debug.Log(gameObject.name + " # healthModules: " + healthModules.Count + " totalHealth: " + totalHealth + " minHealth: " + minHealth); }
public override void Apply(PartConfig config, GameObject target) { if (target == null) { return; } // find rigid body gameobject under target var rigidbodyGo = PartUtil.GetBodyGo(target); if (rigidbodyGo == null) { return; } // add damage modifier var modifier = rigidbodyGo.AddComponent <ImpactDamageModifier>(); modifier.impactDamageMultiplier = impactDamageMultiplier; }
public static T[] GetComponentsInChildren <T>(GameObject rootPartGo) { var allComponents = new List <T>(); // find component of specified type in root's tree T[] components = rootPartGo.GetComponentsInChildren <T>(); allComponents.AddRange(components); // find any part child link components in root var childLinks = rootPartGo.GetComponentsInChildren <ChildLink>(); for (var i = 0; i < childLinks.Length; i++) { // if part has external parts reference if (childLinks[i].childGo != null) { allComponents.AddRange(PartUtil.GetComponentsInChildren <T>(childLinks[i].childGo)); } } return(allComponents.ToArray()); }
public override void Apply(PartConfig config, GameObject target) { if (target == null) { return; } // find rigid body gameobject under target var rigidbodyGo = PartUtil.GetBodyGo(target); if (rigidbodyGo == null) { return; } // add health component var actuator = rigidbodyGo.AddComponent <MaterialActuator>(); actuator.materials = materials; actuator.debug = debug; }
public override void Apply(PartConfig config, GameObject target) { if (target == null) { return; } // find rigid body gameobject under target var rigidbodyGo = PartUtil.GetBodyGo(target); if (rigidbodyGo == null) { return; } // add health component var health = rigidbodyGo.AddComponent <Health>(); health.maxHealth = (int)maxHealth; health.healthTag = healthTag; health.debug = debug; }
public GameObject Build( PartConfig config, GameObject root, string label ) { GameObject partGo = null; // instantiate part if (part != null) { partGo = part.Build(config, root, label != null ? label : part.name); if (partGo == null) { return(null); } partGo.transform.localPosition = offset; partGo.transform.localEulerAngles = rotation; } // joint specifies how part is to be attached to root, if specified, apply joint and join to root if (joint != null && partGo != null) { var partBodyGo = PartUtil.GetBodyGo(partGo); if (partBodyGo != null) { joint.Apply(config, partBodyGo); var joiner = partBodyGo.GetComponent <Joiner>(); var rootBodyGo = PartUtil.GetBodyGo(root); if (joiner != null && rootBodyGo != null) { joiner.Join(rootBodyGo.GetComponent <Rigidbody>()); } } } return(partGo); }
// Use this for initialization void Start() { mover = GetComponent <IMovement>(); weapon = GetComponent <IActuator>(); if (eventChannel == null) { eventChannel = PartUtil.GetEventChannel(gameObject); Debug.Log("eventChannel: " + eventChannel); } // determine when bot flips StartCoroutine(DetectFlip()); // determine when bot dies var botHealth = GetComponent <BotHealth>(); if (botHealth != null) { botHealth.onDeath.AddListener(OnBotDeath); } if (controlsActive) { EnableControls(); } }
void OnCollisionEnter(Collision coll) { if (power > 0f && impactForce > 0f) { // apply collision force if we hit a rigidbody var rbColl = coll.rigidbody; if (rbColl != null) { // don't apply collision force to self if (PartUtil.GetRootGo(gameObject) != PartUtil.GetRootGo(coll.gameObject)) { var f = -coll.contacts[0].normal * impactForce * power; if (debug) { Debug.Log("Flipper Collision" + "\napplying f: " + f + "(" + f.magnitude + ")" + "\npower: " + power + "\ncollided with: " + rbColl.gameObject.name); } rbColl.AddForceAtPosition(f, coll.contacts[0].point); } } } }
void Start() { joints = GetComponents <Joint>(); maxBreakForces = new float[joints.Length]; maxBreakTorques = new float[joints.Length]; // initialize max break values based on assigned values... for (var i = 0; i < joints.Length; i++) { maxBreakForces[i] = joints[i].breakForce; maxBreakTorques[i] = joints[i].breakTorque; } health = GetComponent <Health>(); if (health == null) { health = PartUtil.GetComponentInParentBody <Health>(gameObject); } if (health != null) { if (applyDamageToForce || applyDamageToTorque) { health.onChangePercent.AddListener(OnHealthPercentChange); } } }
public virtual GameObject Build( PartConfig config, GameObject root, string label ) { if (label == null || label == "") { label = name; } // creation check -- ensure part we are about to build is not a direct parent already in tree if (PartUtil.PartInParent(root, this)) { Debug.Log("detected part loop/invalid part chain, part: " + name + " already instantiated in parent"); return(null); } // create empty parts container var partsGo = PartUtil.BuildGo(config, root, label); var partId = partsGo.AddComponent <PartId>(); partId.partId = this.GetInstanceID(); // create new rigid body for this part, set parts container as parent var rigidbodyGo = PartUtil.BuildGo(config, partsGo, label + ".body", typeof(Rigidbody), typeof(KeepInBounds)); //PartUtil.ApplyRigidBodyProperties(rigidbodyGo, mass, drag, angularDrag); // apply part properties if (mass != null) { mass.Apply(config, partsGo); } if (health != null) { health.Apply(config, partsGo); } if (damage != null) { damage.Apply(config, partsGo); } // apply applicators to parts container if (applicators != null) { for (var i = 0; i < applicators.Length; i++) { if (applicators[i] != null) { applicators[i].Apply(config, partsGo); } } } // instantiate model under rigid body if (models != null) { for (var i = 0; i < models.Length; i++) { models[i].Build(config, rigidbodyGo, label + ".model"); } } // instantiate connected parts if (connectedParts != null && connectedParts.Length > 0) { for (var i = 0; i < connectedParts.Length; i++) { // check for self-references var childGo = connectedParts[i].Build(config, partsGo, label + ".part"); if (childGo != null) { // join child part to current rigidbody var joiner = childGo.GetComponent <Joiner>(); if (joiner != null) { joiner.Join(rigidbodyGo.GetComponent <Rigidbody>()); } } } } return(partsGo); }
void Discover() { motorScripts = PartUtil.GetComponentsInChildren <IMotorActuator>(gameObject); steeringScripts = PartUtil.GetComponentsInChildren <ISteeringActuator>(gameObject); Debug.Log("# motorSripts: " + motorScripts.Length + " steeringScripts: " + steeringScripts.Length); }
void Discover() { actuators = PartUtil.GetComponentsInChildren <IActuator>(gameObject); Debug.Log("# actuators: " + actuators.Length); }
public override GameObject Build( PartConfig config, GameObject root, string label ) { GameObject partsGo = null; GameObject bodyGo = null; Vector3 rotation = Vector3.zero; Vector3 offset = Vector3.zero; if (label == null || label == "") { label = name; } // compute merged config for root var mergedConfig = PartConfig.Merge(this.config, config); // for a bot, we want to attach rigidbody for frame directly to root // only do this in-game, not for in-editor preview if (root != null && Application.isPlaying) { if (root.GetComponent <Rigidbody>() == null) { root.AddComponent <Rigidbody>(); } if (root.GetComponent <KeepInBounds>() == null) { root.AddComponent <KeepInBounds>(); } bodyGo = root; // apply part properties if (mass != null) { mass.Apply(mergedConfig, root); } if (health != null) { health.Apply(mergedConfig, root); } if (damage != null) { damage.Apply(mergedConfig, root); } // apply applicators to parts container if (applicators != null) { for (var i = 0; i < applicators.Length; i++) { if (applicators[i] != null) { applicators[i].Apply(mergedConfig, root); } } } //PartUtil.ApplyRigidBodyProperties(bodyGo, mass, drag, angularDrag); // empty parts object to parent the rest of the bot partsGo = PartUtil.BuildGo(mergedConfig, null, label + ".parts"); partsGo.transform.position = root.transform.position; partsGo.transform.rotation = root.transform.rotation; // keep track of parent/child links var childLink = root.AddComponent <ChildLink>(); childLink.childGo = partsGo; var parentLink = partsGo.AddComponent <ParentLink>(); parentLink.parentGo = root; // otherwise, instantiate rigidbody/parts as a normal part } else { // build out frame model first partsGo = base.Build(mergedConfig, root, label); if (partsGo != null) { // set local position to match that of root bodyGo = partsGo.transform.Find(partsGo.name + ".body").gameObject; } } // frame is instantiated under parts.body if (frame != null) { frame.Build(mergedConfig, bodyGo, "frame"); } // now build out modules if (modules != null) { for (var i = 0; i < modules.Length; i++) { if (modules[i] != null && modules[i].part != null) { // build the module under the top level parts var moduleGo = modules[i].part.Build(PartConfig.Merge(modules[i].config, config), partsGo, modules[i].label); if (moduleGo != null) { var moduleBodyGo = PartUtil.GetBodyGo(moduleGo); if (moduleBodyGo != null) { // connect module to the var joiner = moduleBodyGo.GetComponent <Joiner>(); if (joiner != null) { joiner.Join(bodyGo.GetComponent <Rigidbody>()); } } } } } } return(partsGo); }