/// <summary> /// Attaches the Root transform of this entity to another. An entity can only attach to one other. /// TODO: Since Renderer2DObjects are also entities, how is it possible for Renderer2DObjects to use this? /// </summary> /// <param name="entity">The entity to attach to</param> public virtual void Attach(Object3 entity, string mesh) { position = entity.MountPosition(mesh); attachedTo = entity; mountMeshName = mesh; following = false; attached = true; orbiting = false; }
/// <summary> /// Unattach a model from it's attachee. /// </summary> public virtual void Unattach() { if (attached) { attachedTo = null; attached = false; } if (mimicPosition || mimicRotation) { mimicRotation = false; mimicPosition = false; targetEntity = null; } }
public void FlagPickedUp(GameSession.Alliance alliance, Object3 tank) { if (!flags.ContainsKey(alliance)) { this.AddFlag(alliance, tank.Position); } flags[alliance].ParticleEmitter0.Emitting = true; flags[alliance].ParticleEmitter1.Emitting = true; flags[alliance].MimicPosition(tank, new Vector3(0,0,70)); flags[alliance].Hidden = false; //TODO add continuous particle emitter to flag //TODO stop emitter from base }
/// <summary> /// Remove sword or shield if present. /// </summary> private void RemoveSwordOrShield() { if (swordOrShieldRenderId != -1) { ServiceManager.Scene.Delete(swordOrShieldRenderId); swordOrShieldRenderId = -1; SwordOrShield = null; } }
/// <summary> /// Rotate the arrow or swords/shield if present. /// </summary> public void RotateObjects() { float deltaTime = (float)ServiceManager.Game.DeltaTime; if (this.arrowRenderId != -1) { if (Arrow == null) Arrow = ServiceManager.Scene.Access3D(arrowRenderId); Arrow.RotateZ(ROTATION_VELOCITY * deltaTime); if (arrowVerticalPosition >= 120) { this.bobstate = ArrowBobState.Down; } else if (arrowVerticalPosition <= 80) { this.bobstate = ArrowBobState.Up; } if (this.bobstate == ArrowBobState.Up) { Arrow.Translate(upVector); this.arrowVerticalPosition++; } else if (this.bobstate == ArrowBobState.Down) { Arrow.Translate(downVector); this.arrowVerticalPosition--; } } if ( this.swordOrShieldRenderId != -1) { if (SwordOrShield == null) SwordOrShield = ServiceManager.Scene.Access3D(swordOrShieldRenderId); this.SwordOrShield.RotateZ(ROTATION_VELOCITY * deltaTime); } }
void TurretSelectionChanged(object sender, TomShane.Neoforce.Controls.EventArgs e) { Weapon selectedWeapon = weapons[form.TurretIndex]; form.TurretPower = Utils.ConvertPowerToProgressBar( selectedWeapon.Projectile.AverageDamage); form.TurretRange = Utils.ConvertRangeToProgressBar(selectedWeapon.Projectile.Range); form.TurretRate = Utils.ConvertRateToProgressBar( selectedWeapon.Cooldown); Renderer.SceneTools.Scene scene = ServiceManager.Scene; scene.Delete(selectedTurretRenderID); selectedTurretRenderID = ServiceManager.Scene.Add( ServiceManager.Resources.GetModel("weapons\\" + selectedWeapon.Model), Vector3.Zero, 0); float oldRotation = selectedTurret.ZRotation; selectedTurret = scene.Access3D(selectedTurretRenderID); selectedTurret.Attach(selectedTank, Constants.TURRET_MOUNT); selectedTurret.ZRotation = oldRotation; selectedTurretName = selectedWeapon.Name; ApplySkin(); }
void TankSelectionChanged(object sender, TomShane.Neoforce.Controls.EventArgs e) { Renderer.SceneTools.Scene scene = ServiceManager.Scene; scene.Delete(selectedTankRenderID); selectedTankRenderID = scene.Add( ServiceManager.Resources.GetModel("tanks\\" + form.Tanks[form.TankIndex]), Vector3.Zero, 0); float oldRotation = selectedTank.ZRotation; selectedTank = scene.Access3D(selectedTankRenderID); selectedTurret.Attach(selectedTank, Constants.TURRET_MOUNT); selectedTank.ZRotation = oldRotation; selectedTankModelName = (string)form.Tanks[form.TankIndex]; ApplySkin(); }
/// <summary> /// Populate the listboxes with turret and tank models /// </summary> private void PopulateBoxes() { List<object> turrets = new List<object>(); //Populate turrets weapons = WeaponLoader.GetWeaponsAsList(); foreach (Weapon weapon in weapons) { turrets.Add(weapon.Name); } form.Turrets = turrets; form.Tanks = Utils.GetTankModels(); Renderer.SceneTools.Scene scene = ServiceManager.Scene; selectedTankRenderID = scene.Add( ServiceManager.Resources.GetModel("tanks\\" + tank.model), Vector3.Zero, 0); selectedTank = scene.Access3D(selectedTankRenderID); selectedTurretRenderID = scene.Add( ServiceManager.Resources.GetModel("weapons\\" + WeaponLoader.GetWeapon(tank.weaponID).Model), Vector3.Zero, 0); selectedTurret = scene.Access3D(selectedTurretRenderID); selectedTurret.Attach(selectedTank, Constants.TURRET_MOUNT); }
/// <summary> /// Event handler for when the user selects a different tank /// </summary> void TankSelectionChanged(object sender, TomShane.Neoforce.Controls.EventArgs e) { Scene scene = ServiceManager.Game.Renderer.ActiveScene; scene.Delete(selectedTankRenderID); selectedTankRenderID = scene.Add( ServiceManager.Resources.GetModel("tanks\\" + form.Tanks[form.TankIndex]), Vector3.Zero, 0); float oldRotation = selectedTank.ZRotation; selectedTank = scene.Access3D(selectedTankRenderID); selectedTurret.Attach(selectedTank, "Mount"); selectedTank.ZRotation = oldRotation; ApplySkin(); }
/// <summary> /// Adds an Object3 to the layer. /// </summary> /// <param name="_entityID">The id to assign to the Object3.</param> /// <param name="_entity">The Object3 object.</param> public void Add(int _entityID, Object3 _entity) { entityDictionary3D.Add(_entityID, _entity); }
/// <summary> /// Set the object which acts as this tank's turret. /// </summary> /// <param name="turretObject">Turret object to add.</param> public void SetTurret(int id, Object3 turretObject) { TurretRenderID = id; if (Team == GameSession.Alliance.BLUE) { turretObject.MeshColor = Color.Blue; } else if (Team == GameSession.Alliance.RED) { turretObject.MeshColor = Color.Red; } else { turretObject.MeshColor = Toolkit.GetColor(attributes.color); } Turret = turretObject; }
/// <summary> /// Constructor /// </summary> /// <param name="_model">Projectile model</param> /// <param name="_position">Position of the missile</param> /// <param name="_angle">Angle at which the missile will be fired</param> /// <param name="_velocity">Velocity of the missile</param> /// <param name="_alive">Alive</param> /// <param name="_firedBy">Who fired the missile</param> public Projectile(Vector3 _position, VTankObject.Point target, double _angle, double _velocity, float _alive, PlayerTank _firedBy, ProjectileData projectileData) { this.data = projectileData; this.position = _position; position = _position + Vector3.UnitZ; ; base.ZRotation = (float)_angle; velocity = (float)_velocity; timeAlive = _alive; elapsed = 0; this.boundingSphere.Radius = projectileData.CollisionRadius; SetBoundingSpherePosition(); owner = _firedBy; RenderID = -1; ID = projectileData.ID; origin = position; if (_firedBy.Weapon.HasFiringArc) { this.usingLaunchAngle = true; float swivelAngle = ZRotation; float tiltAngle = _firedBy.Weapon.LaunchAngle; float projection = DEFAULT_CANNON_LENGTH * (float)Math.Cos(tiltAngle); float tipX = -projection * (float)Math.Cos(swivelAngle); float tipY = -projection * (float)Math.Sin(swivelAngle); float tipZ = Math.Abs(DEFAULT_CANNON_LENGTH * (float)Math.Sin(swivelAngle)); tip = new float[] { tipX, tipY, tipZ }; Vector3 newPosition = position + new Vector3(tipX, tipY, tipZ); // Calculate initial velocity based on the distance we travel. float distance = (float)Math.Sqrt( Math.Pow(target.x - newPosition.X, 2) + Math.Pow(target.y - newPosition.Y, 2)); float maxDistance = (int)projectileData.Range; if (distance > maxDistance) distance = maxDistance; // TODO: This is a temporary work-around until we figure out what velocity component // is missing from the formula. float offset = 1.1f; if (tiltAngle > MathHelper.ToRadians(45.0f)) offset = 1.6f; float V = (float)Math.Sqrt(-gravity * distance * offset); float Vx = -V * (float)(Math.Cos(tiltAngle) * Math.Cos(swivelAngle)); float Vy = -V * (float)(Math.Cos(tiltAngle) * Math.Sin(swivelAngle)); float Vz = V * (float)Math.Sin(tiltAngle); componentVelocity = new float[] { Vx, Vy, Vz }; elapsedDelta = 0f; /*this.verticalVelocity = this.FindVerticalVelocity(_firedBy.Weapon.LaunchAngle, velocity); float flightTime = this.FindFlightTime(verticalVelocity, gravity); float horizontalVelocity = this.FindHorizontalVelocity( distance, flightTime); velocity = horizontalVelocity;*/ } Object3 turret = owner.Turret; ModelBoneCollection.Enumerator collection = turret.Model.Bones.GetEnumerator(); List<ModelBone> emitters = new List<ModelBone>(); while (collection.MoveNext()) { if (collection.Current.Name.StartsWith("Emitter")) { emitters.Add(collection.Current); } } if (emitters.Count == 0) { ServiceManager.Game.Console.DebugPrint( "Warning: Can't attach to owner tank, no emitter exists."); } else { int emitter = _firedBy.Weapon.GetNextEmitterIndex(); this.Attach(turret, emitters[emitter].Name); this.MimicRotation(turret); this.Unattach(); Vector3 forward = emitters[emitter].Transform.Forward; forward.Z = Math.Abs(forward.Z); //position *= forward; } if (!String.IsNullOrEmpty(projectileData.Model)) { model = new Object3(ServiceManager.Resources.GetModel("projectiles\\" + projectileData.Model), position); model.MimicPosition(this, Vector3.Zero); model.MimicRotation(this); modelRenderID = ServiceManager.Scene.Add(model, 3); } else { model = null; } if (!String.IsNullOrEmpty(projectileData.ParticleEffect) && projectileData.Model == null) { ParticleEmitter.MimicPosition(this, Vector3.Zero); ParticleEmitter.MimicRotation(this); particleEmitterRenderID = ServiceManager.Scene.Add(ParticleEmitter, 3); } else if (!String.IsNullOrEmpty(projectileData.ParticleEffect)) { ParticleEmitter = new ParticleEmitter(projectileData.ParticleEffect, this.Position); this.particleEmitterRenderID = ServiceManager.Scene.Add(ParticleEmitter, 3); ParticleEmitter.Follow(this); } }
/// <summary> /// Calculates parameters for tiles with height and creates DrawableTiles from them /// </summary> private void FillTiles() { Random random = new Random(); Dictionary<uint, int> borderTileCounter = new Dictionary<uint, int>(); for (uint y = 0; y < map.Height; y++) { if (borderTileCounter.ContainsKey(map.GetTile(0, y).ID) && map.GetTile(0, y).Height ==0) borderTileCounter[map.GetTile(0, y).ID]++; else if( map.GetTile(0, y).Height ==0) borderTileCounter.Add(map.GetTile(0, y).ID, 1); if (borderTileCounter.ContainsKey(map.GetTile(map.Width - 1, y).ID) && map.GetTile(map.Width - 1, y).Height == 0) borderTileCounter[map.GetTile(map.Width-1, y).ID]++; else if (map.GetTile(map.Width - 1, y).Height == 0) borderTileCounter.Add(map.GetTile(map.Width-1, y).ID, 1); } for (uint x = 0; x < map.Width-1; x++) { if (borderTileCounter.ContainsKey(map.GetTile(x, 0).ID) && map.GetTile(x, 0).Height ==0) borderTileCounter[map.GetTile(x, 0).ID]++; else if (map.GetTile(x, 0).Height == 0) borderTileCounter.Add(map.GetTile(x, 0).ID, 1); if (borderTileCounter.ContainsKey(map.GetTile(x, map.Height - 1).ID) && map.GetTile(x, map.Height - 1).Height == 0) borderTileCounter[map.GetTile(x, map.Height - 1).ID]++; else if (map.GetTile(x, map.Height - 1).Height == 0) borderTileCounter.Add(map.GetTile(x, map.Height - 1).ID, 1); } int maxValue = 0; uint maxValueTile = 0; foreach (KeyValuePair<uint, int> element in borderTileCounter) { if (element.Value > maxValue) { maxValue = element.Value; maxValueTile = element.Key; } } //Load floor float minX = -10000f; float maxX = 100000f; float minY = -100000f; float maxY = 10000f; float Xspread = (maxX - minX) / Constants.TILE_SIZE; float Yspread = (maxY - minY) / Constants.TILE_SIZE; List<VertexPositionNormalTexture> verts = new List<VertexPositionNormalTexture>(); verts.Add(new VertexPositionNormalTexture(new Vector3(minX, minY, -0.75f), Vector3.UnitZ, Vector2.UnitY * Yspread)); verts.Add(new VertexPositionNormalTexture(new Vector3(minX, maxY, -0.75f), Vector3.UnitZ, Vector2.Zero)); verts.Add(new VertexPositionNormalTexture(new Vector3(maxX, minY, -0.75f), Vector3.UnitZ, new Vector2(Xspread, Yspread))); verts.Add(new VertexPositionNormalTexture(new Vector3(maxX, maxY, -0.75f), Vector3.UnitZ, Vector2.UnitX * Xspread)); verts.Add(new VertexPositionNormalTexture(new Vector3(maxX, minY, -0.75f), Vector3.UnitZ, new Vector2(Xspread, Yspread))); verts.Add(new VertexPositionNormalTexture(new Vector3(minX, maxY, -0.75f), Vector3.UnitZ, Vector2.Zero)); VertexGroup floor = new VertexGroup(TileList.GetTile((int)maxValueTile), verts); floor.Technique = RendererAssetPool.UniversalEffect.Techniques.TexturedWrap; floor.CastsShadow = false; floor.TransparencyEnabled = false; floor.Ready(); renderer.ActiveScene.Add(floor, 0); //Loads the tiles into the background array for (uint y = 0; y < map.Height; y++) { for (uint x = 0; x < map.Width; x++) { Tile tempTile = map.GetTile(x, y); // Figure out the height of each tile in each direction relative to this one. int positionNorth = (int)(y - 1); int positionSouth = (int)(y + 1); int positionWest = (int)(x - 1); int positionEast = (int)(x + 1); if (positionNorth >= 0) tempTile.heightN = map.GetTile(x, y - 1).Height; if (positionSouth < map.Height) tempTile.heightS = map.GetTile(x, y + 1).Height; if (positionWest >= 0) tempTile.heightW = map.GetTile(x - 1, y).Height; if (positionEast < map.Width) tempTile.heightE = map.GetTile(x + 1, y).Height; map.SetTile(x, y, tempTile); Texture2D texture = TileList.GetTile((int)tempTile.ID); DrawableTile tile = new DrawableTile( texture, tempTile, (int)x, (int)y, tempTile.heightN, tempTile.heightW, tempTile.heightE, tempTile.heightS); Vector3 pos = new Vector3( x * Constants.TILE_SIZE + (Constants.TILE_SIZE / 2), -(y * Constants.TILE_SIZE + (Constants.TILE_SIZE / 2)), 0); if (tempTile.ObjectID != 0) { Model objectModel = TileList.GetObject(tempTile.ObjectID); Object3 newTileObj = new Object3(objectModel, pos + (Vector3.UnitZ * tempTile.Height * Constants.TILE_SIZE)); newTileObj.TransparencyEnabled = true; Scene.Add(newTileObj, 1); } switch (tempTile.EventID) { case 4: case 5: GameSession.Alliance team = tempTile.EventID == 4 ? GameSession.Alliance.RED : GameSession.Alliance.BLUE; if (currentGameMode == VTankObject.GameMode.CAPTURETHEFLAG) { Flags.AddFlag(team, pos); } break; case 8: case 9: case 10: if (currentGameMode == VTankObject.GameMode.CAPTURETHEBASE) { Bases.AddBase(GameSession.Alliance.BLUE, tempTile.EventID, pos); } break; case 11: case 12: case 13: if (currentGameMode == VTankObject.GameMode.CAPTURETHEBASE) { Bases.AddBase(GameSession.Alliance.RED, tempTile.EventID, pos); } break; default: break; } visibleTiles[y * map.Width + x] = tile; } } #region Make Flat Tiles for (uint y = 0; y < map.Height; y++) { for (uint x = 0; x < map.Width; x++) { Tile tempTile = map.GetTile(x, y); Vector3 pos = new Vector3(x * Constants.TILE_SIZE, (-(y + 1) * Constants.TILE_SIZE), 0); Tiles.AddFloor(pos, tempTile.Height, (int)tempTile.ID); } } #endregion #region Make North Walls int height = 0; int tileID = 0; int width = 0; int Hdir = 0; //Make north facing walls for (uint y = 0; y < map.Height; y++) { for (uint x = 0; x < map.Width; x++) { Tile tempTile = map.GetTile(x, y); if ((width == 0 || (tempTile.Height == height && tempTile.ID == tileID && Hdir == tempTile.heightN)) && (x + 1) < map.Width) { height = tempTile.Height; tileID = (int)tempTile.ID; Hdir = tempTile.heightN; width++; } else { if (height > Hdir) { Vector3 pos = new Vector3(x * Constants.TILE_SIZE, (-y * Constants.TILE_SIZE), 0); Tiles.AddWall(pos, Vector3.UnitY, width, height, tileID); } width = 1; height = tempTile.Height; tileID = (int)tempTile.ID; Hdir = tempTile.heightN; } } width = 0; } #endregion #region Make South Walls height = 0; tileID = 0; width = 0; Hdir = 0; uint startX = 0; //Make south facing walls for (uint y = 0; y < map.Height; y++) { for (uint x = 0; x < map.Width; x++) { Tile tempTile = map.GetTile(x, y); if ((width == 0 || (tempTile.Height == height && tempTile.ID == tileID && Hdir == tempTile.heightS)) && (x + 1) < map.Width) { if (width == 0) { startX = x; } height = tempTile.Height; tileID = (int)tempTile.ID; Hdir = tempTile.heightS; width++; } else { if (height > Hdir) { Vector3 pos = new Vector3(startX * Constants.TILE_SIZE, (-(y+1) * Constants.TILE_SIZE), 0); Tiles.AddWall(pos, -Vector3.UnitY, width, height, tileID); } width = 1; height = tempTile.Height; tileID = (int)tempTile.ID; Hdir = tempTile.heightS; startX = x; } } width = 0; } #endregion #region Make East Walls height = 0; tileID = 0; width = 0; Hdir = 0; //Make east facing walls for (uint x = 0; x < map.Width; x++) { for (uint y = 0; y < map.Height; y++) { Tile tempTile = map.GetTile(x, y); if ((width == 0 || (tempTile.Height == height && tempTile.ID == tileID && Hdir == tempTile.heightE)) && (y + 1) < map.Height) { height = tempTile.Height; tileID = (int)tempTile.ID; Hdir = tempTile.heightE; width++; } else { if (height > Hdir) { Vector3 pos = new Vector3((x+1) * Constants.TILE_SIZE, (-y * Constants.TILE_SIZE), 0); Tiles.AddWall(pos, Vector3.UnitX, width, height, tileID); } width = 1; height = tempTile.Height; tileID = (int)tempTile.ID; Hdir = tempTile.heightE; } } width = 0; } #endregion #region Make West Walls height = 0; tileID = 0; width = 0; Hdir = 0; uint startY = 0; //Make south facing walls for (uint x = 0; x < map.Width; x++) { for (uint y = 0; y < map.Height; y++) { Tile tempTile = map.GetTile(x, y); if ((width == 0 || (tempTile.Height == height && tempTile.ID == tileID && Hdir == tempTile.heightW)) && (y+1) < map.Height) { if (width == 0) { startY = y; } height = tempTile.Height; tileID = (int)tempTile.ID; Hdir = tempTile.heightW; width++; } else { if (height > Hdir) { Vector3 pos = new Vector3(x * Constants.TILE_SIZE, (-startY * Constants.TILE_SIZE), 0); Tiles.AddWall(pos, -Vector3.UnitX, width, height, tileID); } width = 1; height = tempTile.Height; tileID = (int)tempTile.ID; Hdir = tempTile.heightW; startY = y; } } width = 0; } #endregion Tiles.AllReady(); //////////////////////////////////////////// ///TODO:::: FOR EACH TILE GOING LEFT TO RIGHT // // IF height = height and tileID = tileID // if Hnorth = Hnorth // NorthWidth ++; // else // TexturedTileGroupManager.AddWall(lowerLeftNorth, Vector3.UnitY, NorthWidth, height, tileID) // NorthWidth = 0; // height = -1 // // if Hsouth = Hsouth // SouthWidth ++; // else // TexturedTileGroupManager.AddWall(lowerLeftSouth, -Vector3.UnitY, SouthhWidth, height, tileID) //etc }
/// <summary> /// Starts tracking a new Renderer3DObject /// </summary> /// <param name="_entity">The Renderer3DObject to add</param> /// <param name="_position">The position to assign the object</param> /// <param name="_layer">The layer to associate the entity with. Lower layer numbers are drawn first.</param> /// <returns>The unique entity id used to keep track of each entity</returns> public int Add(Object3 _entity, int _layer) { RendererAssetPool.UniversalEffect.RemapModel(_entity.Model); int x = entityID.GetID(); entityList.Add(x, _entity); AddLayers(_layer); layers[_layer].Add(x, _entity); return x; }
/// <summary> /// Converts a model to a RendererEntity and starts tracking it /// </summary> /// <param name="_entity">The Model to insert</param> /// <param name="_position">The position to insert the Model</param> /// <param name="_layer">The layer to associate the entity with. Lower layer numbers are drawn first.</param> /// <returns>The unique entity id used to keep track of each entity</returns> public int Add(Model _entity, Vector3 _position, int _layer) { RendererAssetPool.UniversalEffect.RemapModel(_entity); int x = entityID.GetID(); Object3 obj = new Object3(_entity, _position); entityList.Add(x, obj); AddLayers(_layer); layers[_layer].Add(x, obj); return x; }
/// <summary> /// Populate the listboxes with turret and tank models /// </summary> private void PopulateBoxes() { //Populate turrets weapons = WeaponLoader.GetWeaponsAsList(); List<object> weaponNames = new List<object>(); foreach (Weapon weapon in weapons) { weaponNames.Add(weapon.Name); } form.Turrets = weaponNames; form.Tanks = Utils.GetTankModels(); Scene scene = ServiceManager.Game.Renderer.ActiveScene; selectedTankRenderID = scene.Add( ServiceManager.Resources.GetModel("tanks\\" + form.Tanks[0]), Vector3.Zero, 0); selectedTank = scene.Access3D(selectedTankRenderID); selectedTurretRenderID = scene.Add( ServiceManager.Resources.GetModel("weapons\\" + weapons[0].Model), Vector3.Zero, 0); selectedTurret = scene.Access3D(selectedTurretRenderID); selectedTurret.Attach(selectedTank, "Mount"); }