public void EnterVehicle(Vehicle vehicle) { if (vehicle.IsFull == true) { return; } if (body != null) { body.transform.SetParent(vehicle.seat); body.transform.SetPositionAndRotation(vehicle.seat.position, vehicle.seat.rotation); body.transform.localScale = Vector3.one; //this.transform.SetParent(vehicle.seat); if (body is PhysicalBody) { PhysicalBody pBody = body as PhysicalBody; pBody.Simulate = false; pBody.Move = false; } } vehicle.SetBodyInside(body); vehicle.IsFull.AddValueListener <bool>(OnVehicleIsFullChange); vehicle.destroyBody.AddTriggerListener(OnVehicleDestruction); //foreach (BodyPart part in transferPartsToVehicle) // part.AttachedBody = vehicle; currentVehicle.Value = vehicle; isPilotingVehicle.Value = true; }
public override void Initialize() { base.Initialize(); Renderer = World.Visualization.CreateRenderer(() => new FoodEntityRenderer(this)); PhysicalBody = BodyFactory.CreateBody(World.PhysicalWorld, Position.ToFPVector2(), 0, FarseerPhysics.Dynamics.BodyType.Static); PhysicalBody.CreateFixture(new CircleShape(FoodSize, 1)); }
public void ApplyGravityWith(PhysicalBody other) { Vector3 direction = (other.transform.position - transform.position).normalized; //Debug.DrawLine(transform.position, (direction * gravitationalPull) / mass, Color.green); Debug.DrawLine(transform.position, other.transform.position, Color.green); velocity += (direction * gravitationalPull) / mass; }
public override void Dispose() { base.Dispose(); if (PhysicalBody != null) { PhysicalBody.Dispose(); } }
private void OnSpawnUnit() { if (units == null || units.Length != maxUnits) { ResizeUnitArray(); } if (magazineCount <= 0) { return; } Transform spawnPoint; int spawnPointCount = spawnPoints != null ? spawnPoints.Length : 0; spawnPoint = spawnPointCount > 0 ? spawnPoints[UnityEngine.Random.Range(0, spawnPointCount)] : transform; for (int i = 0; i < maxUnits; i++) { Body spawnedUnit = null; if (units[i] == null) { spawnedUnit = Instantiate(unitPrefab, spawnPoint.position, spawnPoint.rotation, transform); ValueChangeEvent.InitializeVCEs(spawnedUnit.gameObject); spawnedUnit.destroyBody.AddTriggerListener(OnUnitDeath); units[i] = spawnedUnit; } else if (units[i].IsAlive == false) { spawnedUnit = units[i]; spawnedUnit.transform.position = spawnPoint.position; spawnedUnit.transform.rotation = spawnPoint.rotation; spawnedUnit.Respawn(); } if (spawnedUnit != null) { CurrentSpawnedUnitCount += 1; if (magazineSize >= 0) { magazineCount.Value = magazineCount - 1; } if (spawnedUnit is PhysicalBody) { PhysicalBody physicalUnit = spawnedUnit as PhysicalBody; physicalUnit.AttachedRigidBody.velocity = transform.up * spawnVelocity; physicalUnit.AttachedRigidBody.angularVelocity = 0f; } break; } } }
public float GravityBetween(PhysicalBody other) { float d_squared = Mathf.Pow(Vector3.Distance(transform.position, other.transform.position), 2); if (d_squared == 0) { d_squared = 1 / float.MaxValue; } float pull = (G * mass * other.mass / d_squared) * worldSettings.gravityMultiplier; return(pull); }
public Projectile(WeaponManager weaponManager, PhysicalBody physicalBody) : base(weaponManager.MobManager, physicalBody) { WeaponManager = weaponManager; WeaponManager.RegisterProjectile(this); IsComplete = false; TimeAlive = TimeSpan.FromSeconds(0); TimeDie = TimeSpan.FromSeconds(10); CancelCollisionMobs = new List <Mob>(); }
protected virtual bool OnMobCollision(PhysicalBody f1, PhysicalBody f2) { Mob f1Mob = MobBodies[f1]; Mob f2Mob = MobBodies[f2]; if (f1Mob != null && f2Mob != null) { if (f1Mob.WorldLayer == f2Mob.WorldLayer) { return(f1Mob.Collide(f2Mob)); } } return(false); }
private void OnGrabTarget() { if (target == null) { return; } PhysicalBody body = target.GetComponent <PhysicalBody>(); if (body != null) { body.Move = false; } target.position = transform.position; target.rotation = transform.rotation; }
private IEnumerator ExitVehicleCoroutine(bool immediate) { if (isPilotingVehicle == true) { //exitingVehicle.Stop(); Vehicle exitVehicle = currentVehicle.Value as Vehicle; if (immediate == false) { yield return(new WaitForSeconds(exitVehicle.exitAnimationDelay)); } if (body != null) { if (body is PhysicalBody) { PhysicalBody pBody = body as PhysicalBody; pBody.Simulate = true; pBody.Move = true; } //this.transform.SetParent(body.transform); body.transform.SetParent(exitVehicle.transform.parent); body.transform.position = exitVehicle.exit.position; if (body.AttachedRigidBody != null) { body.AttachedRigidBody.AddForce(exitVehicle.exitForce * exitVehicle.exit.up); } } exitVehicle.IsFull.RemoveValueListener <bool>(OnVehicleIsFullChange); exitVehicle.destroyBody.RemoveTriggerListener(OnVehicleDestruction); exitVehicle.SetBodyInside(null); //foreach (BodyPart part in transferPartsToVehicle) // part.AttachedBody = body; currentVehicle.Value = body; isPilotingVehicle.Value = false; } }
/// <summary> /// Spawn event is something that should be called once after the game object is ready with all components. /// </summary> public void CallSpawnEvent() { // was already spawned? skip if (_wasSpawned) { return; } // prepare children and components array before iteration PrepareChildrenAndComponentsArray(); // call all components spawned foreach (var component in _componentsArray) { component.Spawn(); } // prepare children and components array before iteration PrepareChildrenAndComponentsArray(); // iterate and spawn children foreach (var child in _childrenArray) { child.CallSpawnEvent(); } // mark that we were already spawned _wasSpawned = true; // special handling physical body - if we got physical body upon spawn, call its update after completing all other spawn callbacks. // this is to avoid flickering if physical body transformations are set from one of the components spawn event if (PhysicalBody != null) { PhysicalBody.UpdateNodeTransforms(); } // after spawn event also update scene node, so that if the spawn triggered changes to transform they will apply before first draw. SceneNode.UpdateTransformations(false); }
private void OnHide(bool hide) { if (hide) { stopHiding.AddTriggerListener(OnStopHiding, false); } else { stopHiding.RemoveTriggerListener(OnStopHiding); } if (AttachedBody != null) { AttachedBody.Visible = !hide; if (AttachedBody is PhysicalBody) { PhysicalBody physical = AttachedBody as PhysicalBody; physical.Simulate = !hide; physical.Move = !hide; } } }
override public void CallDestroyOnComponents() { PhysicalBody.Destroy(); }
override public void CallCreateOnComponents() { PhysicalBody.Create(); }
override public void CallInitOnComponents() { PhysicalBody.Init(); }
public Vector3 GravityVectorTowards(PhysicalBody other) { return((other.transform.position - transform.position).normalized); }
private void LoadFromString(string[] lines) { Materials = new List <GenericMaterial>(); Meshes = new List <Mesh3d>(); Lights = new List <Light>(); Cameras = new List <Camera>(); var regx = new Regex("(.+?)[ ]+(.+)"); var currentMaterial = new GenericMaterial(Vector3.One); Light tempLight = null; GenericMaterial tempMaterial = null; Mesh3d tempMesh = null; Camera tempCamera = null; Action flush = () => { if (tempLight != null) { Lights.Add(tempLight); if (OnObjectFinish != null) { OnObjectFinish.Invoke(this, tempLight); } } if (tempMaterial != null) { Materials.Add(tempMaterial); if (OnObjectFinish != null) { OnObjectFinish.Invoke(this, tempMaterial); } } if (tempMesh != null) { Meshes.Add(tempMesh); if (OnObjectFinish != null) { OnObjectFinish.Invoke(this, tempMesh); } } if (tempCamera != null) { Cameras.Add(tempCamera); if (OnObjectFinish != null) { OnObjectFinish.Invoke(this, tempCamera); } } tempLight = null; tempMaterial = null; tempMesh = null; tempCamera = null; }; string vertexbuffer = ""; PhysicalBody physob = null; foreach (var l in lines) { if (l.StartsWith("//") || l.Trim().Length == 0) { continue; } var regout = regx.Match(l); if (!regout.Success) { throw new ArgumentException("Invalid line in scene string: " + l); } string command = regout.Groups[1].Value.Trim(); string data = regout.Groups[2].Value.Trim(); if (command.Length == 0 || data.Length == 0) { throw new ArgumentException("Invalid line in scene string: " + l); } switch (command) { // Mesh3d start case "mesh": { flush(); tempMesh = Mesh3d.Empty; tempMesh.AddInstance(new Mesh3dInstance(new TransformationManager(Vector3.Zero), data)); //tempMesh.Name = data; break; } case "usematerial": { tempMesh.AddLodLevel(null, Materials.First((a) => a.Name == data), 0, 999999); break; } case "scaleuv": { string[] literals = data.Split(' '); float x, y; if (!float.TryParse(literals[0], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out x)) { throw new ArgumentException("Invalid line in scene string: " + l); } if (!float.TryParse(literals[1], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out y)) { throw new ArgumentException("Invalid line in scene string: " + l); } tempMesh.GetLodLevel(0).Info3d.Manager.ScaleUV(x, y); var obj = tempMesh.GetLodLevel(0).Info3d.Manager; tempMesh.GetLodLevel(0).SetInfo3d(new Object3dInfo(tempMesh.GetLodLevel(0).Info3d.Manager.Vertices)); tempMesh.GetLodLevel(0).Info3d.Manager = obj; break; } case "vbo": { vertexbuffer = data; ApplyVboIbo(tempMesh, vertexbuffer); vertexbuffer = ""; break; } case "physics": { var datas = data.Split(' '); if (datas[0] == "convexhull") { if (tempMesh == null) { throw new ArgumentException("Invalid line in scene string: " + l); } var shape = tempMesh.GetLodLevel(0).Info3d.Manager.GetConvexHull(); float mass; if (!float.TryParse(datas[1], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out mass)) { throw new ArgumentException("Invalid line in scene string: " + l); } physob = Game.World.Physics.CreateBody(mass, tempMesh.GetInstance(0), shape); } if (datas[0] == "boundingbox") { if (tempMesh == null) { throw new ArgumentException("Invalid line in scene string: " + l); } var shape = Physics.CreateBoundingBoxShape(tempMesh.GetLodLevel(0).Info3d.Manager); float mass; if (!float.TryParse(datas[1], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out mass)) { throw new ArgumentException("Invalid line in scene string: " + l); } physob = Game.World.Physics.CreateBody(mass, tempMesh.GetInstance(0), shape); } if (datas[0] == "accurate") { if (tempMesh == null) { throw new ArgumentException("Invalid line in scene string: " + l); } var shape = tempMesh.GetLodLevel(0).Info3d.Manager.GetAccurateCollisionShape(); float mass; if (!float.TryParse(datas[1], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out mass)) { throw new ArgumentException("Invalid line in scene string: " + l); } physob = Game.World.Physics.CreateBody(mass, tempMesh.GetInstance(0), shape); } if (datas[0] == "enable") { if (physob == null) { throw new ArgumentException("Invalid line in scene string: " + l); } physob.Enable(); } break; } case "translate": { string[] literals = data.Split(' '); if (literals.Length != 3) { throw new ArgumentException("Invalid line in scene string: " + l); } float x, y, z; if (!float.TryParse(literals[0], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out x)) { throw new ArgumentException("Invalid line in scene string: " + l); } if (!float.TryParse(literals[1], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out y)) { throw new ArgumentException("Invalid line in scene string: " + l); } if (!float.TryParse(literals[2], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out z)) { throw new ArgumentException("Invalid line in scene string: " + l); } if (tempMesh != null) { tempMesh.GetInstance(0).Transformation.Translate(x, y, z); } if (tempCamera != null) { tempCamera.Transformation.Translate(x, y, z); } if (tempLight != null) { tempLight.Transformation.Translate(x, y, z); } break; } case "scale": { if (tempMesh == null) { throw new ArgumentException("Invalid line in scene string: " + l); } string[] literals = data.Split(' '); if (literals.Length != 3) { throw new ArgumentException("Invalid line in scene string: " + l); } float x, y, z; if (!float.TryParse(literals[0], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out x)) { throw new ArgumentException("Invalid line in scene string: " + l); } if (!float.TryParse(literals[1], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out y)) { throw new ArgumentException("Invalid line in scene string: " + l); } if (!float.TryParse(literals[2], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out z)) { throw new ArgumentException("Invalid line in scene string: " + l); } tempMesh.GetInstance(0).Transformation.Scale(x, y, z); break; } case "rotate": { string[] literals = data.Split(' '); if (literals.Length < 3 || literals.Length > 4) { throw new ArgumentException("Invalid line in scene string: " + l); } float x, y, z; if (!float.TryParse(literals[0], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out x)) { throw new ArgumentException("Invalid line in scene string: " + l); } if (!float.TryParse(literals[1], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out y)) { throw new ArgumentException("Invalid line in scene string: " + l); } if (!float.TryParse(literals[2], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out z)) { throw new ArgumentException("Invalid line in scene string: " + l); } Quaternion rot = Quaternion.Identity; if (literals.Length == 3) { var rotx = Matrix3.CreateRotationX(MathHelper.DegreesToRadians(x)); var roty = Matrix3.CreateRotationY(MathHelper.DegreesToRadians(y)); var rotz = Matrix3.CreateRotationZ(MathHelper.DegreesToRadians(z)); rot = Quaternion.FromMatrix(rotx * roty * rotz); } if (literals.Length == 4) { float w; if (!float.TryParse(literals[3], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out w)) { throw new ArgumentException("Invalid line in scene string: " + l); } rot = new Quaternion(x, y, z, w); } if (tempMesh != null) { tempMesh.GetInstance(0).Transformation.Rotate(rot); } if (tempCamera != null) { tempCamera.Transformation.Rotate(rot); } if (tempLight != null) { tempLight.Transformation.Rotate(rot); } break; } // Mesh3d end Material start case "material": { flush(); tempMaterial = new GenericMaterial(Vector3.One); tempMaterial.Name = data; break; } case "type": { if (tempMaterial == null) { throw new ArgumentException("Invalid line in scene string: " + l); } tempMaterial.Type = (GenericMaterial.MaterialType)Enum.Parse(typeof(GenericMaterial.MaterialType), data, true); break; } case "invertnormalmap": { if (tempMaterial == null) { throw new ArgumentException("Invalid line in scene string: " + l); } // tempMaterial.InvertNormalMap = data == "true" ? true : false; break; } case "transparency": { if (tempMaterial == null) { throw new ArgumentException("Invalid line in scene string: " + l); } //tempMaterial.SupportTransparency = data == "true" ? true : false; break; } case "normalmap": { if (tempMaterial == null) { throw new ArgumentException("Invalid line in scene string: " + l); } tempMaterial.NormalsTexture = new Texture(Media.Get(data)); break; } case "bumpmap": { if (tempMaterial == null) { throw new ArgumentException("Invalid line in scene string: " + l); } tempMaterial.BumpTexture = new Texture(Media.Get(data)); break; } case "roughnessmap": { if (tempMaterial == null) { throw new ArgumentException("Invalid line in scene string: " + l); } tempMaterial.RoughnessTexture = new Texture(Media.Get(data)); break; } case "metalnessmap": { if (tempMaterial == null) { throw new ArgumentException("Invalid line in scene string: " + l); } // tempMaterial.MetalnessMap = new Texture(Media.Get(data)); break; } case "roughness": { if (tempMaterial == null) { throw new ArgumentException("Invalid line in scene string: " + l); } float f; if (!float.TryParse(data, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out f)) { throw new ArgumentException("Invalid line in scene string: " + l); } tempMaterial.Roughness = f; break; } case "metalness": { if (tempMaterial == null) { throw new ArgumentException("Invalid line in scene string: " + l); } float f; if (!float.TryParse(data, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out f)) { throw new ArgumentException("Invalid line in scene string: " + l); } //tempMaterial.Metalness = f; break; } case "color": { if (tempMaterial == null) { throw new ArgumentException("Invalid line in scene string: " + l); } string[] literals = data.Split(' '); if (literals.Length != 3) { throw new ArgumentException("Invalid line in scene string: " + l); } float x, y, z; if (!float.TryParse(literals[0], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out x)) { throw new ArgumentException("Invalid line in scene string: " + l); } if (!float.TryParse(literals[1], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out y)) { throw new ArgumentException("Invalid line in scene string: " + l); } if (!float.TryParse(literals[2], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out z)) { throw new ArgumentException("Invalid line in scene string: " + l); } tempMaterial.DiffuseColor = new Vector3(x, y, z); break; } case "texture": { if (tempMaterial == null) { throw new ArgumentException("Invalid line in scene string: " + l); } tempMaterial.DiffuseTexture = new Texture(Media.Get(data)); tempMaterial.SpecularTexture = tempMaterial.DiffuseTexture; //tempMaterial.Mode = GenericMaterial.DrawMode.TextureMultipleColor; break; } // Material end default: throw new ArgumentException("Invalid line in scene string: " + l); } } flush(); }