public Weapon(SPRWorld sprWorld, Bot bot, Vector2 relativePosition, float relativeRotation, String textureName, Vector2 scale, WeaponType weaponType, float health, float power) { m_SPRWorld = sprWorld; m_Time = 0; this.m_owner = bot; this.m_firing = false; this.m_reloadTime = weaponType == WeaponType.melee ? 0 : .2f; this.m_reloading = 0; this.weaponType = weaponType; this.m_power = power; m_Position = relativePosition; m_Rotation = relativeRotation; m_Texture = TextureStatic.Get(textureName); m_Scale = scale; this.m_health = health; Vertices v = SPRWorld.computedSpritePolygons[textureName]; // Simplify the object until it has few enough verticies. while (v.Count > Physics.Settings.MaxPolygonVertices) // Infinite loop potential? { v = SimplifyTools.DouglasPeuckerSimplify(v, 2); // Where 2 is a completely arbitrary number? } v.Scale(ref scale); //v.Translate(ref relativePosition); v.Rotate(relativeRotation); Fixture f = FixtureFactory.CreatePolygon(v, 1f, bot.Body, relativePosition); m_Fixture = f; f.Friction = 0.5f; f.Restitution = 0f; f.UserData = this; if (this.weaponType == WeaponType.melee) { Body tempBody = BodyFactory.CreateBody(m_SPRWorld.World); tempBody.BodyType = BodyType.Dynamic; Vertices v2 = SPRWorld.computedSpritePolygons["Axe"]; // Simplify the object until it has few enough verticies. while (v2.Count > Physics.Settings.MaxPolygonVertices) // Infinite loop potential? { v2 = SimplifyTools.DouglasPeuckerSimplify(v2, 2); // Where 2 is a completely arbitrary number? } Fixture f2 = FixtureFactory.CreatePolygon(SPRWorld.computedSpritePolygons[textureName], 0.1f, tempBody); f2.Friction = 0.5f; f2.Restitution = 0f; tempBody.SetTransform(this.GetAbsPosition(), this.GetAbsRotation()); Projectile justFired = new Projectile(m_SPRWorld, tempBody, TextureStatic.Get("Axe"), new Vector2(0, 0), this.GetRelRotation(), 5, Settings.MetersPerPixel * 80, 80 * Settings.MetersPerPixel, m_power, m_health); f2.UserData = justFired; f2.OnCollision += Projectile.OnMeleeHit; RevoluteJoint joint = JointFactory.CreateRevoluteJoint(m_SPRWorld.World, this.m_owner.Body, tempBody, Vector2.Zero); joint.MaxMotorTorque = 160; joint.LimitEnabled = true; joint.MotorEnabled = true; joint.LowerLimit = - (float)Math.PI / 4f; joint.UpperLimit = (float)Math.PI / 4f; m_AxeJoint = joint; m_SPRWorld.AddEntity(justFired); } }
public void Update(float dTime) { m_Time += dTime; if (m_reloading > 0) { m_reloading -= dTime; } if (this.m_firing && weaponType == WeaponType.gun) { Body tempBody = BodyFactory.CreateBody(m_SPRWorld.World); tempBody.BodyType = BodyType.Dynamic; tempBody.IsBullet = true; float rotation = this.GetAbsRotation(); tempBody.Position = this.GetAbsPosition() + 45 * Settings.MetersPerPixel * (new Vector2((float) Math.Cos(rotation), (float)Math.Sin(rotation))); tempBody.SetTransform(tempBody.Position, 0); Fixture f = FixtureFactory.CreateCircle(4 * Settings.MetersPerPixel, 1f, tempBody); f.OnCollision += Projectile.OnBulletHit; Vector2 initialVelocity = new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation))/10; Projectile justFired = new Projectile(m_SPRWorld, tempBody, TextureStatic.Get("Projectile"), initialVelocity, this.GetAbsRotation(), 5, 5 * Settings.MetersPerPixel, 5 * Settings.MetersPerPixel, m_power, 1); f.UserData = justFired; this.m_SPRWorld.AddEntity(justFired); this.m_firing = false; } if (!this.m_firing && weaponType == WeaponType.melee) m_AxeJoint.MotorSpeed = 0; if (this.m_firing && weaponType == WeaponType.melee) { m_AxeJoint.MotorSpeed = (float)Math.Cos(m_Time * 10) * 10; this.m_firing = false; } // Similar to Entity.Update() if (this.m_health <= 0f) { this.m_owner.RemoveWeapon(this); } this.m_SPRWorld.World.ProcessChanges(); }