/// <summary> /// Initializes a resource using tile as position /// </summary> /// <param id="entry">Entry defining the type of this game object</param> /// <param id="game">Game this game object is in</param> /// <param id="player">Owner of this game object</param> /// <param id="mapTiles">Tile where this game object is placed</param> public override void Initialize(EntryDb entry, WildmenGame game, Player player, Tile mapTile) { AssignUID(game); this.entry = (ResourceDb)entry; this.Amount = this.entry.InitialAmount; this.game = game; this.Owner = player; player.Assign(this); this.NearestTile = mapTile; this.MapPosition = mapTile.MapPosition; this.RecalculatePosition = true; }
public void MakeMap(int width, int height, int players, int unitCap) { UnitCap = unitCap; if (Map != null) { if (NaturePlayer != null) { NaturePlayer.Dispose(); } Map.Dispose(); Effects.Clear(); } NaturePlayer = new Player(-1, this, "Nature"); Map = new MapBoard(this, width, height); Map.Initialize(); Map.Generate(random, players); NaturePlayer.Initialize(); }
/// <summary> /// Method called when the screen is switched to; creates MapUI and assigns controlling player /// </summary> /// <param id="screenParams">Parameters passed from the last screen, WildmenGame, NetworkServer, NetworkClient and Player in this order</param> public override void OnActivate(object[] screenParams) { game = screenParams[0] as WildmenGame; server = screenParams[1] as NetworkServer; client = screenParams[2] as NetworkClient; player = screenParams[3] as Player; serverView = player == null; currentClientGameState = GameState.Started; MakeMapUI(game); mapUI.AssignControllingPlayer(player); messageQueue.Clear(); if (serverView) { sm.CallSubscreen("escapemenu", client, server, player); } }
/// <summary> /// Method called when the screen is switched from /// </summary> public override void OnDeactivate() { client = null; player = null; }
public void PlacePlayer(int n, Player player) { foreach (var res in Db.Instance.Resources) { player.ResourceStockpiles[res.Value] += res.Value.StartWith; } Players.Add(player); Tile loc = Map.StartLocations[n]; int x = loc.X; int y = loc.Y; // Place buildings foreach (var bldgEntry in Db.Instance.Buildings.Where(p => p.Value.BuiltOnStart)) { bool tileOk = false; int c = 0; Tile t = null; // Get random location around center while (!tileOk) { int rx = x + (int)(random.NextDouble() * 8) - 4; int ry = y + (int)(random.NextDouble() * 8) - 4; t = Map.Tiles[rx, ry]; if (!Building.CanPlace(Map, t)) { c++; if (c > 100) { Logger.Log("Unable to place building " + bldgEntry.Key + " - no free space", "MapGen"); break; } } else { tileOk = true; } } if (tileOk) { Building bldg = new Building(); bldg.Initialize(bldgEntry.Value, this, player, t); bldg.InitializeGraphics(); bldg.Completed(false); } } // Place units foreach (var unitEntry in Db.Instance.Units.Where(p => p.Value.GivenOnStart > 0)) { for (int i = 0; i < unitEntry.Value.GivenOnStart; i++) { int rx = x + (int)(random.NextDouble() * 8) - 4; int ry = y + (int)(random.NextDouble() * 8) - 4; Tile t = Map.Tiles[rx, ry]; Unit u = new Unit(); u.Initialize(unitEntry.Value, this, player, t.MapPosition); u.InitializeGraphics(); } } }
private void DrawGameEffectsOverlay(Vector2 offset, Player controlPlayer) { if (controlPlayer == null) return; foreach (var item in Effects) { if (item.GraphicalOverlay && controlPlayer.Fow[item.Tile.X, item.Tile.Y] == 2) item.Draw(offset); } }
// DRAWING private void DrawMap(Vector2 offset, Player controlPlayer, Tile mouseoverTile) { for (int y = 0; y < Map.Height; y++) { for (int x = 0; x < Map.Width; x++) { Tile tile = Map.Tiles[x, y]; if (UI.MakeRect(tile.GetDrawPosition(offset), MapBoard.TILE_SIZE).Intersects(UI.Instance.ScreenRectangle)) { tile.Draw(offset, tile == mouseoverTile, controlPlayer); } } } }
/// <summary> /// Initializes this unit /// </summary> /// <param id="entry">Entry defining the type of this game object</param> /// <param id="game">Game this game object is in</param> /// <param id="player">Owner of this game object</param> /// <param id="mapPosition">Vector where this game object is placed</param> public override void Initialize(EntryDb entry, WildmenGame game, Player player, Vector2 mapPosition) { AssignUID(game); Logger.Log(string.Format("Unit #{0} initialized", this.ObjectUID), "Server update"); player.Assign(this); this.State = GameObjectState.Idle; this.Order = GameObjectOrder.Idle; this.game = game; this.Owner = player; this.entry = ((UnitDb)entry).Clone(); this.MapPosition = mapPosition; this.RecalculatePosition = true; this.IsMovable = true; this.Health = this.entry.Health; }
/// <summary> /// Draws the game object /// </summary> /// <param id="offset">Current screen offset</param> /// <param id="highlight">Highlighting mode</param> /// <param id="activePlayer">Player for whom the game object is rendered</param> public override void Draw(SharpDX.Vector2 offset, GameObjectHighlightMode highlight, Player activePlayer) { if (NearestTile == null) return; if (activePlayer != null && activePlayer.Fow[NearestTile.X, NearestTile.Y] != 2) return; if (Removed) return; if (!cache.drawPosition.HasValue) cache.drawPosition = Position - offset + AlignmentOffset; float zLevel = NearestTile.DrawZLevel; float zLevelStep = NearestTile.DrawZLevelRange / 3; UI.Instance.Draw(cache.drawPosition.Value, entry.Texture, Owner.PlayerColor, zLevel + zLevelStep); if ((State == GameObjectState.MovingToOrder || State == GameObjectState.DoOrder) && activePlayer == Owner) { Texture2D tex = (State == GameObjectState.MovingToOrder) ? textureBullet2 : textureBullet; Color c = (Order == GameObjectOrder.Attack) ? Color.Yellow : (Order == GameObjectOrder.Construct) ? Color.Cyan : (Order == GameObjectOrder.Gather) ? Color.Cyan : Color.Lime; if (!cache.drawOrderPosition.HasValue && OrderTarget == null) { Tile tile = game.Map.GetTileFlat(OrderPosition); cache.drawOrderPosition = tile.MapPosition - new Vector2(0, (float)tile.Elevation * MapBoard.ELEVATION_SCALING) + MapBoard.TILE_SIZE / 2; } Vector2 vector = (OrderTarget == null ? cache.drawOrderPosition.Value : OrderTarget.Position) - this.Position; DrawTrail(tex, c, Position - offset, vector); if (Order == GameObjectOrder.Gather && OrderTarget != lastGatheredResource) { vector = lastGatheredResource.Position - this.Position; DrawTrail(tex, c, Position - offset, vector); } } }
/// <summary> /// Draws the game object /// </summary> /// <param id="offset">Current screen offset</param> /// <param id="highlight">Highlighting mode</param> /// <param id="activePlayer">Player for whom the game object is rendered</param> public abstract void Draw(Vector2 offset, GameObjectHighlightMode highlight, Player activePlayer);
/// <summary> /// Draws the game object's overlay /// </summary> /// <param id="offset">Current screen offset</param> /// <param id="highlight">Highlighting mode</param> /// <param id="activePlayer">Player for whom the game object is rendered</param> public override void DrawOverlay(Vector2 offset, GameObjectHighlightMode highlight, Player activePlayer) { if (NearestTile == null) return; if (activePlayer != null && activePlayer.Fow[NearestTile.X, NearestTile.Y] != 2) return; if (Removed) return; int maxWidth = (int)(Size.X * 1.25); int hpHeight = 5; int conHeight = 3; Color c = Health > entry.Health * .66f ? Color.Green : Health > entry.Health * .33f ? Color.Orange : Color.Red; Vector2 position = Position - offset + new Vector2(-maxWidth / 2, -hpHeight - 5) + new Vector2(-AlignmentOffset.X, AlignmentOffset.Y); UI.Instance.DrawBox(position + new Vector2(-1, -1), new Vector2(maxWidth + 2, hpHeight + 2), Color.Black, Color.Black, 0); UI.Instance.DrawBox(position, new Vector2(Health * maxWidth / entry.Health, hpHeight), c, Color.Black, 0); if (!Constructed) { position += new Vector2(0, -conHeight - 2); UI.Instance.DrawBox(position + new Vector2(-1, -1), new Vector2(maxWidth + 2, conHeight + 2), Color.Black, Color.Black, 0); UI.Instance.DrawBox(position, new Vector2((Construction / (float)entry.ConstructionAmount) * maxWidth, conHeight), Color.Cyan, Color.Black, 0); } else if (Spawner != null && Spawner.Active) { position += new Vector2(0, -conHeight - 2); UI.Instance.DrawBox(position + new Vector2(-1, -1), new Vector2(maxWidth + 2, conHeight + 2), Color.Black, Color.Black, 0); UI.Instance.DrawBox(position, new Vector2((UI.Instance.UpdateNumber + Spawner.UpdateOffset) % Spawner.Speed * maxWidth / Spawner.Speed, conHeight), Color.Goldenrod, Color.Black, 0); } //Texture2D tx = UI.Instance.GetTexture("pointer"); //UI.Instance.Draw(Position - offset - tx.Size() / 2, tx, FontColor.White, 1f); if (IsSelected) { UI.Instance.DrawBox(Position - offset + AlignmentOffset, Size, UI.Instance.GetGuiTex("selectionOverlay"), 0); } if (highlight != GameObjectHighlightMode.None) { UI.Instance.DrawBox(Position - offset + AlignmentOffset, Size, UI.Instance.GetGuiTex("targetOverlay"), 0); } }
/// <summary> /// Draws the game object /// </summary> /// <param id="offset">Current screen offset</param> /// <param id="highlight">Highlighting mode</param> /// <param id="activePlayer">Player for whom the game object is rendered</param> public override void Draw(Vector2 offset, GameObjectHighlightMode highlight, Player activePlayer) { if (NearestTile == null) return; if (activePlayer != null && activePlayer.Fow[NearestTile.X, NearestTile.Y] != 2) return; if (Removed) return; Vector2 drawPosition = Position - offset + AlignmentOffset; Color c = Color.White; if (Owner != activePlayer) { c = Color.Lerp(Color.White, Color.Red, 0.25f); } UI.Instance.Draw(drawPosition, Constructed ? entry.Texture : entry.TextureConstruction, c, FurtestTile.DrawZLevel + FurtestTile.DrawZLevelRange / 2); }
/// <summary> /// Initializes this instance and claims the tiles /// </summary> /// <param name="entry">Type of this building</param> /// <param name="game">The game this building is in</param> /// <param name="player">The owner of this building</param> /// <param name="baseTile">The base tile determining the position of this building</param> public override void Initialize(EntryDb entry, WildmenGame game, Player player, Tile baseTile) { AssignUID(game); Logger.Log(string.Format("Building #{0} initialized", this.ObjectUID), "Server update"); player.Assign(this); this.game = game; this.Owner = player; this.entry = (BuildingDb)entry; this.NearestTile = baseTile; ClaimTiles(baseTile); this.MapPosition = baseTile.MapPosition; this.RecalculatePosition = true; this.Health = this.entry.Health; this.Construction = 0; if (Construction >= this.entry.ConstructionAmount) { Completed(false); } }
/// <summary> /// Draws the resource /// </summary> /// <param id="offset">Current screen offset</param> /// <param id="highlight">Highlighting mode</param> /// <param id="activePlayer">Player for whom the game object is rendered</param> public override void Draw(Vector2 offset, GameObjectHighlightMode highlight, Player activePlayer) { if (NearestTile == null) return; if (activePlayer != null && activePlayer.Fow[NearestTile.X, NearestTile.Y] == 0) return; if (Removed) return; float zLevel = NearestTile.DrawZLevel; float zLevelStep = NearestTile.DrawZLevelRange / 3; Color c = Color.White; if (Amount == 0) c = Color.Gray; UI.Instance.Draw(GetDrawPosition(offset), entry.Texture, c, zLevel + zLevelStep); }
/// <summary> /// Method called when the screen is switched to; creates MapUI and assigns controlling player /// </summary> /// <param id="screenParams">Parameters passed from the last screen, NetworkClient, NetworkServer and Player in this order</param> public override void OnActivate(object[] screenParams) { client = screenParams[0] as NetworkClient; server = screenParams[1] as NetworkServer; player = screenParams[2] as Player; }
/// <summary> /// Disposes resources used by this instance /// </summary> public override void Dispose() { Building bldg; if ((bldg = spawner as Building) != null) if (bldg.Spawner == this) bldg.ResetSpawner(null); spawner = null; Spawnee = null; owner = null; base.Dispose(); }
/// <summary> /// Method that deserializes the data from the line starting at given position in given context depending on the MessageType /// </summary> /// <param id="mt">MessageType defining the extend of deserialization which is to be performed</param> /// <param id="line">String array containing the serialized data</param> /// <param id="position">Current position in the array</param> /// <param id="context">Context of the serialized data, game where this INetworkSerializable object is in</param> public override void Deserialize(MessageType mt, string[] line, ref int position, WildmenGame context) { base.Deserialize(mt, line, ref position, context); switch (mt) { case MessageType.GameTransfer: case MessageType.GameEffectCreate: spawner = (Building)context.GetGameObjectById(int.Parse(line[position++])); ((Building)spawner).ResetSpawner(this); Spawnee = Db.Instance.Units[line[position++]]; int playerId = int.Parse(line[position++]); owner = context.Players.First(u => u.Id == playerId); spawnPoint = new Vector2(float.Parse(line[position++], CultureInfo.InvariantCulture), float.Parse(line[position++], CultureInfo.InvariantCulture)); break; default: throw new Exception(); } }
/// <summary> /// Draws the game object's overlay /// </summary> /// <param id="offset">Current screen offset</param> /// <param id="highlight">Highlighting mode</param> /// <param id="activePlayer">Player for whom the game object is rendered</param> public virtual void DrawOverlay(Vector2 offset, GameObjectHighlightMode highlight, Player activePlayer) { }
/// <summary> /// Draws the game object's overlay /// </summary> /// <param id="offset">Current screen offset</param> /// <param id="highlight">Highlighting mode</param> /// <param id="activePlayer">Player for whom the game object is rendered</param> public override void DrawOverlay(Vector2 offset, GameObjectHighlightMode highlight, Player activePlayer) { if (NearestTile == null) return; if (activePlayer != null && activePlayer.Fow[NearestTile.X, NearestTile.Y] != 2) return; if (Removed) return; int maxWidth = (int)(Size.X * 1.25); int hpHeight = 5; int conHeight = 3; Color c = Health * .5f > entry.Health ? Color.White : Health > entry.Health ? Color.LightGreen : Health > entry.Health * .66f ? Color.Green : Health > entry.Health * .33f ? Color.Orange : Color.Red; int displayHp = Math.Min(entry.Health, Health); Vector2 position = Position - offset + new Vector2(-maxWidth / 2, AlignmentOffset.Y - hpHeight - 5); UI.Instance.DrawBox(position + new Vector2(-1, -1), new Vector2(maxWidth + 2, hpHeight + 2), Color.Black, Color.Black, 0); UI.Instance.DrawBox(position, new Vector2(displayHp * maxWidth / entry.Health, hpHeight), c, Color.Black, 0); if (actionCooldown > 0 && lastOrderTimeout > 100) { position += new Vector2(0, -conHeight - 2); UI.Instance.DrawBox(position + new Vector2(-1, -1), new Vector2(maxWidth + 2, conHeight + 2), Color.Black, Color.Black, 0); UI.Instance.DrawBox(position, new Vector2(maxWidth * actionCooldown / lastOrderTimeout, conHeight), Color.Goldenrod, Color.Black, 0); } else if (State == GameObjectState.DoOrder) { switch (Order) { case GameObjectOrder.Attack: case GameObjectOrder.Construct: case GameObjectOrder.Gather: position += new Vector2(0, -conHeight - 2); UI.Instance.DrawBox(position + new Vector2(-1, -1), new Vector2(maxWidth + 2, conHeight + 2), Color.Black, Color.Black, 0); UI.Instance.DrawBox(position, new Vector2(maxWidth, conHeight), Color.Cyan, Color.Black, 0); break; default: break; } } if (!cache.drawPosition.HasValue) cache.drawPosition = Position - offset + AlignmentOffset; if (IsSelected) { UI.Instance.DrawBox(cache.drawPosition.Value, Size, UI.Instance.GetGuiTex("selectionOverlay"), 0); } if (highlight != GameObjectHighlightMode.None) { UI.Instance.DrawBox(cache.drawPosition.Value, Size, UI.Instance.GetGuiTex("targetOverlay"), 0); } if (UI.Instance.KeyHold(SharpDX.Toolkit.Input.Keys.Shift)) { UI.Instance.DrawBox(cache.drawPosition.Value, Size, UI.Instance.GetGuiTex("selectionOverlay"), 0); } }
/// <summary> /// Initializes a game object using tile as position /// </summary> /// <param id="entry">Entry defining the type of this game object</param> /// <param id="game">Game this game object is in</param> /// <param id="player">Owner of this game object</param> /// <param id="mapTiles">Tile where this game object is placed</param> public abstract void Initialize(EntryDb entry, WildmenGame game, Player player, Tile mapTiles);
private GameObject GetGameObjectById(Player player, long id) { GameObject g; g = player.Units.FirstOrDefault(p => p.ObjectUID == id); if (g != null) return g; g = player.Buildings.FirstOrDefault(p => p.ObjectUID == id); if (g != null) return g; g = player.Resources.FirstOrDefault(p => p.ObjectUID == id); if (g != null) return g; return null; }
/// <summary> /// Initializes a game object using vector as position /// </summary> /// <param id="entry">Entry defining the type of this game object</param> /// <param id="game">Game this game object is in</param> /// <param id="player">Owner of this game object</param> /// <param id="mapPosition">Vector where this game object is placed</param> public abstract void Initialize(EntryDb entry, WildmenGame game, Player player, Vector2 mapPosition);
private void DrawGameObjects(Vector2 offset, Player controlPlayer, GameObject mouseoverGameObj) { foreach (var gObj in GetAllGameObjectsEnumerable()) { GameObjectHighlightMode hl = GameObjectHighlightMode.None; if (mouseoverGameObj == gObj) { if (gObj.Owner == controlPlayer) hl = GameObjectHighlightMode.FriendlyHighlight; else if (controlPlayer != null) hl = GameObjectHighlightMode.HostileHighlight; } if (UI.MakeRect(gObj.GetDrawPosition(offset), gObj.Size).Intersects(UI.Instance.ScreenRectangle)) { gObj.Draw(offset, hl, controlPlayer); } } }
/// <summary> /// Constructor of this class /// </summary> /// <param name="unit">Unit, the death effect should be created from</param> public UnitDeathEffect(Unit unit) { owner = unit.Owner; color = owner.PlayerColor; unitEntry = (UnitDb)unit.Entry; position = unit.Position + unit.AlignmentOffset; tile = unit.NearestTile; Speed = 10; Duration = 10; Graphical = true; }
private void DrawGameObjectsOverlay(Vector2 offset, Player controlPlayer, GameObject mouseoverGameObj) { foreach (var gObj in GetAllGameObjectsEnumerable()) { GameObjectHighlightMode hl = GameObjectHighlightMode.None; if (mouseoverGameObj == gObj) { if (gObj.Owner == controlPlayer) hl = GameObjectHighlightMode.FriendlyHighlight; else hl = GameObjectHighlightMode.HostileHighlight; } gObj.DrawOverlay(offset, hl, controlPlayer); } }
/// <summary> /// Disposes resources used by this instance /// </summary> public override void Dispose() { owner = null; unitEntry = null; tile = null; base.Dispose(); }
public void DrawOverlay(Vector2 offset, Player controlPlayer, Tile mouseoverTile, GameObject mouseoverGameObj) { DrawGameObjectsOverlay(offset, controlPlayer, mouseoverGameObj); DrawGameEffectsOverlay(offset, controlPlayer); }
/// <summary> /// Method that deserializes the data from the line starting at given position in given context depending on the MessageType /// </summary> /// <param id="mt">MessageType defining the extend of deserialization which is to be performed</param> /// <param id="line">String array containing the serialized data</param> /// <param id="position">Current position in the array</param> /// <param id="context">Context of the serialized data, game where this INetworkSerializable object is in</param> public override void Deserialize(MessageType mt, string[] line, ref int position, WildmenGame context) { int id; base.Deserialize(mt, line, ref position, context); switch (mt) { case MessageType.GameTransfer: case MessageType.GameEffectCreate: id = int.Parse(line[position++]); owner = context.Players.First(p => p.Id == id); unitEntry = Db.Instance.Units[line[position++]]; color = new Color(byte.Parse(line[position++]), byte.Parse(line[position++]), byte.Parse(line[position++])); this.position = new Vector2(float.Parse(line[position++], CultureInfo.InvariantCulture), float.Parse(line[position++], CultureInfo.InvariantCulture)); tile = context.Map.Tiles[int.Parse(line[position++]), int.Parse(line[position++])]; break; default: throw new Exception(); } }
/// <summary> /// Method that deserializes the data from the line starting at given position in given context depending on the MessageType /// </summary> /// <param id="mt">MessageType defining the extend of deserialization which is to be performed</param> /// <param id="line">String array containing the serialized data</param> /// <param id="position">Current position in the array</param> /// <param id="context">Context of the serialized data, game where this INetworkSerializable object is in</param> public void Deserialize(MessageType mt, string[] line, ref int position, WildmenGame context) { int n; switch (mt) { case MessageType.GameTransfer: Map = new MapBoard(context, int.Parse(line[position + 0]), int.Parse(line[position + 1])); Map.Initialize(); Map.Deserialize(mt, line, ref position, context); Map.LoadContent(); int evCount = int.Parse(line[position++]); for (int i = 0; i < evCount; i++) { Effects.Add(GameEffect.Create(line, ref position, context)); } UnitCap = int.Parse(line[position++]); n = int.Parse(line[position++]); for (int i = 0; i < n; i++) { var p = new Player(i, context, null); Players.Add(p); p.Initialize(); } for (int i = 0; i < n; i++) { Players[i].Deserialize(mt, line, ref position, context); } NaturePlayer = new Player(-1, context, null); NaturePlayer.Initialize(); NaturePlayer.Deserialize(mt, line, ref position, context); List<Player> tempPlayers = new List<Player>(Players); tempPlayers.Add(NaturePlayer); foreach (var player in tempPlayers) { foreach (var unit in player.Units) { unit.ResolveReferences(GetGameObjectById); } foreach (var bldg in player.Buildings) { bldg.ResolveReferences(GetGameObjectById); } } NextUID = int.Parse(line[position++]); break; default: throw new Exception("WildmenGame deserialization error"); } }
/// <summary> /// Constructor of this class /// </summary> /// <param name="spawner">Building doing the spawning</param> /// <param name="spawnee">Unit type to spawn</param> /// <param name="owner">Owner of the new unit</param> /// <param name="spawnPoint">Position of the new unit</param> /// <param name="speed">Spawning speed</param> public SpawnerEffect(Building spawner, UnitDb spawnee, Player owner, Vector2 spawnPoint, int speed) { this.spawner = spawner; this.Spawnee = spawnee; this.owner = owner; this.spawnPoint = spawnPoint; this.Speed = speed; this.Duration = 0; }