public EntityFormation(List<Entity> entities, DisFieldMixer disFieldMixer, int2 goalPos) { this.entities = entities; this.disFieldMixer = disFieldMixer; this.goalPos = goalPos; int formationIndex = 0; entities.Sort(delegate(Entity a, Entity b) { // sort by formationIndex return a.GetComponent<component.Formation>().FormationIndex.CompareTo(b.GetComponent<component.Formation>().FormationIndex); }); foreach(Entity e in entities) { if (!e.HasComponent<component.Physics>()) continue; e.GetComponent<component.Physics>().Body.OnCollision += this.onEntityCollision; e.GetComponent<component.Formation>().FormationIndex = formationIndex; formationIndex++; } }
public DisField(int2 size) { this.size = size; disField = new float[size.x, size.y]; tiles = new bool[size.x, size.y]; updateTiles = new bool[size.x, size.y]; for (int y = 0; y < size.y; ++y) { for (int x = 0; x < size.x; ++x) { disField[x, y] = 3f; tiles[x, y] = false; updateTiles[x, y] = false; } } }
public Entity BuildEntity(Entity entity, EntityWorld entityWorld, params object[] args) { TextureManager textureManager = EntitySystem.BlackBoard.GetEntry<TextureManager>("TextureManager"); FarseerPhysics.Dynamics.World world = EntitySystem.BlackBoard.GetEntry<FarseerPhysics.Dynamics.World>("PhysicsWorld"); int2 pos = new int2(0, 0); float rotation = 0.0f; int resourceAmount = 500; String resourceType = "No type"; int textureId = 0; Random r = new Random(); if (args.Length >= 1) pos = (int2)args[0]; if (args.Length >= 2) rotation = (float)args[1]; if (args.Length >= 3) resourceType = (String)args[2]; if (args.Length >= 4) resourceAmount = (int)args[3]; if (args.Length >= 5) textureId = (int)args[4]; Texture2D texture = textureManager.getTexture(textureId); Rectangle col; if (resourceType.Equals("wood")) col = new Rectangle(pos.x + 1, pos.y + 1, 2, 2); else col = new Rectangle(pos.x, pos.y, texture.Width / Global.tileSize, texture.Height / Global.tileSize); entity.AddComponent(new component.TileEntity(entity, new Rectangle(pos.x, pos.y, texture.Width / Global.tileSize, texture.Height / Global.tileSize), col, rotation)); entity.AddComponent(new component.Size(texture.Width, texture.Height)); entity.AddComponent(new component.Drawable(texture)); entity.AddComponent(new component.DepletableResource(resourceAmount, resourceType)); return entity; }
public float getDis(Vector2 pos) { int2 floorPos = new int2((int)pos.X, (int)pos.Y); int2 ceilPos = new int2((int)pos.X + 1, (int)pos.Y + 1); if (floorPos.x < 0 || floorPos.y < 0 || ceilPos.x >= size.x || ceilPos.y >= size.y) return 5f; Vector2 localPos = new Vector2(pos.X % 1f, pos.Y % 1f); float a = 1f - localPos.X; float b = 1f - localPos.Y; float c = 1f - a; float d = 1f - b; float[] dis = { disField[floorPos.x, floorPos.y], disField[ceilPos.x, floorPos.y], disField[floorPos.x, ceilPos.y], disField[ceilPos.x, ceilPos.y] }; float dis2 = a * b * dis[0] + c * b * dis[1] + a * d * dis[2] + c * d * dis[3]; return dis2; }
public void setTile(int x, int y, bool isSolid) { if (x < 0 || y < 0 || x >= size.x || y >= size.y) return; if (tiles[x, y] == isSolid) return; tiles[x, y] = isSolid; for (int xx = -2; xx <= 2; ++xx) { for (int yy = -2; yy <= 2; ++yy) { int2 pos = new int2(x + xx, y + yy); if (pos.x < 0 || pos.y < 0 || pos.x >= size.x || pos.y >= size.y) continue; if (updateTiles[pos.x, pos.y]) continue; updateTiles[pos.x, pos.y] = true; updateQueue.Enqueue(pos); } } }
public void update() { MouseState mouseState; if (entitiesInSelection.Count > 0) { if (inputState.IsNewRightMouseClick(out mouseState)) { if (Global.Viewport.Contains(mouseState.Position)) { Vector2 pos = Global.Camera.ScreenToWorld(mouseState.Position.ToVector2()); //Bag<Entity> entities = entityWorld.EntityManager.GetEntities(Aspect.All(typeof(component.Goal), typeof(component.Physics), typeof(component.Formation))); int2 goalPos = new int2((int)pos.X / Global.tileSize, (int)pos.Y / Global.tileSize); Bag<Entity> bag = new Bag<Entity>(); PathGoal pathGoal = new PathGoal(entitiesInSelection, disFieldMixer, new int2(Global.mapWidth, Global.mapHeight), goalPos); setPathGoal(pathGoal); pathGoal.updatePath(); EntityFormation formation = new EntityFormation(entitiesInSelection, disFieldMixer, goalPos); foreach (Entity e in entitiesInSelection) { e.GetComponent<component.Formation>().EntityFormation = formation; } formation.update(); } } } if (Mouse.GetState().LeftButton == ButtonState.Pressed && !isSelecting) { isSelecting = true; firstCorner = Global.Camera.ScreenToWorld(Mouse.GetState().Position.ToVector2()); foreach (Entity old in entitiesInSelection) { old.GetComponent<component.HealthComponent>().Visible = false; } entitiesInSelection.Clear(); } else if (Mouse.GetState().LeftButton == ButtonState.Released && isSelecting) { isSelecting = false; Vector2 secondCorner = Global.Camera.ScreenToWorld(Mouse.GetState().Position.ToVector2()); Vector2 topLeft = new Vector2(Math.Min(firstCorner.X, secondCorner.X), Math.Min(firstCorner.Y, secondCorner.Y)); Vector2 bottomRight = new Vector2(Math.Max(firstCorner.X, secondCorner.X), Math.Max(firstCorner.Y, secondCorner.Y)); RectangleF rect = new RectangleF(topLeft.X, topLeft.Y, bottomRight.X - topLeft.X, bottomRight.Y - topLeft.Y); Console.WriteLine(rect.X / 32 + " - " + rect.Y / 32); if (rect.Width > 0 && rect.Height > 0) { Bag<Entity> entities = entityWorld.EntityManager.GetEntities(Aspect.All(typeof(component.Physics), typeof(component.Formation), typeof(component.HealthComponent), typeof(component.Unit))); foreach (Entity e in entities) { if (e.GetComponent<component.Unit>().Lord != lord) continue; component.Physics phys = e.GetComponent<component.Physics>(); Vector2 newPos = new Vector2(phys.Position.X * Global.tileSize, phys.Position.Y * Global.tileSize); RectangleF entityRect = new RectangleF(newPos.X - 0.5f * 32f, newPos.Y - 0.5f * 32f, 1f * 32f, 1f * 32f); if (rect.IntersectsWith(entityRect)) { e.GetComponent<component.HealthComponent>().Visible = true; entitiesInSelection.Add(e); } } if (onSelectUnit != null) onSelectUnit(); } } if (isSelecting) { Vector2 secondCorner = Global.Camera.ScreenToWorld(Mouse.GetState().Position.ToVector2()); Vector2 topLeft = new Vector2(Math.Min(firstCorner.X, secondCorner.X), Math.Min(firstCorner.Y, secondCorner.Y)); Vector2 bottomRight = new Vector2(Math.Max(firstCorner.X, secondCorner.X), Math.Max(firstCorner.Y, secondCorner.Y)); RectangleF rect = new RectangleF(topLeft.X, topLeft.Y, bottomRight.X - topLeft.X, bottomRight.Y - topLeft.Y); foreach (Entity e in tempEntitiesInSelection) { e.GetComponent<component.HealthComponent>().Visible = false; } tempEntitiesInSelection.Clear(); if (rect.Width > 0 && rect.Height > 0) { Bag<Entity> entities = entityWorld.EntityManager.GetEntities(Aspect.All(typeof(component.Physics), typeof(component.Formation), typeof(component.HealthComponent))); foreach (Entity e in entities) { component.Physics phys = e.GetComponent<component.Physics>(); Vector2 newPos = new Vector2(phys.Position.X * Global.tileSize, phys.Position.Y * Global.tileSize); RectangleF entityRect = new RectangleF(newPos.X - 0.5f * 32f, newPos.Y - 0.5f * 32f, 1f * 32f, 1f * 32f); if (rect.IntersectsWith(entityRect)) { e.GetComponent<component.HealthComponent>().Visible = true; tempEntitiesInSelection.Add(e); } } } } }
public EntityTileMap(EntityWorld entityWorld, int2 size) { this.entityWorld = entityWorld; this.size = size; }
public TileMap(TileManager tileManager, int2 size) { this.tileManager = tileManager; this.size = size; }
public Area generateArea(int2 pos, float minRadius, float maxRadius) { Area area = new Area(); Rectangle rect = new Rectangle(pos.x - (int)maxRadius, pos.y - (int)maxRadius, (int)(2 * maxRadius), (int)(2 * maxRadius)); Land land = new Land(); float noiseRadius = maxRadius - minRadius; Graphics.Tools.Noise.Primitive.BevinsGradient noise = new Graphics.Tools.Noise.Primitive.BevinsGradient(random.Next(), NoiseQuality.Best); for (int y = rect.Y; y < rect.Y + rect.Height; ++y) { for (int x = rect.X; x < rect.X + rect.Width; ++x) { if (x < 0 || y < 0 || x >= tileMap.Size.x || y >= tileMap.Size.y) continue; float dis = new Vector2(x - pos.x, y - pos.y).Length() / maxRadius; float nvalue = Math.Abs(noise.GetValue((float)x / 64f, (float)y / 64f, 0f)); if (dis + noiseRadius / maxRadius * (nvalue) < 1f) { area[x, y] = true; } } } return area; }
public void generate(EntityWorld entityWorld) { occupied = new bool[tileMap.Size.x, tileMap.Size.y]; // Create border forest fillArea(generateArea(tileMap.Size / 2, (float)tileMap.Size.x + 1, (float)tileMap.Size.x + 1), 4); fillArea(generateArea(tileMap.Size / 2, 0f, (float)tileMap.Size.x / 2), 1); // Forest for (int i = 0; i < 16; ++i) { int2 pos = new int2(random.Next(tileMap.Size.x), random.Next(tileMap.Size.y)); fillArea(generateArea(pos, 8f, 16f), 4); } // Small Forest for (int i = 0; i < 32; ++i) { int2 pos = new int2(random.Next(tileMap.Size.x), random.Next(tileMap.Size.y)); fillArea(generateArea(pos, 2f, 8f), 4); } // Create player land fillArea(generateArea(new int2(18, 18), 4, 16), 2); fillArea(createPath(new Point(18, 18), new Point(tileMap.Size.x/2, tileMap.Size.y/2), 2, 0.5f), 2); // Paths: //float curliness = 0.75f; //for (int i = 0; i < 3; ++i) //{ // fillArea(createPath(new Point(0, 0), new Point(tileMap.Size.x - 1, tileMap.Size.y - 1), i + 2, curliness), 2); // fillArea(createPath(new Point(0, 0), new Point(tileMap.Size.x - 1, tileMap.Size.y - 1), i + 2, curliness), 2); // fillArea(createPath(new Point(0, 0), new Point(tileMap.Size.x - 1, tileMap.Size.y - 1), i + 2, curliness), 2); // curliness *= 0.85f; //} List<Point> players = new List<Point>(); players.Add(new Point(tileMap.Size.x / 6, tileMap.Size.y / 6)); players.Add(new Point(tileMap.Size.x*3 / 6, tileMap.Size.y / 6)); players.Add(new Point(tileMap.Size.x*5 / 6, tileMap.Size.y / 6)); players.Add(new Point(tileMap.Size.x*5 / 6, tileMap.Size.y*5 / 6)); players.Add(new Point(tileMap.Size.x*3 / 6, tileMap.Size.y*5 / 6)); players.Add(new Point(tileMap.Size.x / 6, tileMap.Size.y*5 / 6)); for (int i = 0; i < players.Count; ++i) { fillArea(createPath(players[i], players[(i+1)%players.Count], 5, 0.25f), 2); fillArea(generateArea(new int2(players[i].X, players[i].Y), 2, 16), 2); fillArea(generateArea(new int2(players[i].X, players[i].Y), 2, 4), 5); } fillArea(createPath(players[1], players[4], 5, 0.25f), 2); // River: Area riverArea = createPath(new Point(tileMap.Size.x - 1, 0), new Point(0, tileMap.Size.y - 1), 8, 0.5f); replaceArea(riverArea, 1, 3); replaceArea(riverArea, 2, 5); for (int x = 0; x < tileMap.Size.x; x++) { for (int y = 0; y < tileMap.Size.y; y++) { float zoom = 0.05f; if (tileMap.getTileID(x, y) == 4) { int treeWidth = 2; int treeHeight = 2; bool canPlaceTree = true; for (int xt = 0; xt < treeWidth; xt++) { for (int yt = 0; yt < treeHeight; yt++) { int checkX = xt + x; int checkY = yt + y; if (checkX >= tileMap.Size.x || checkY >= tileMap.Size.y || occupied[checkX, checkY] || !tileMap.getTile(checkX, checkY).Name.Equals("tree")) { canPlaceTree = false; break; } } if (!canPlaceTree) break; } if (!canPlaceTree) continue; entityWorld.CreateEntityFromTemplate("Resource", new object[] { new int2(x, y), (float)random.Next(360), "wood", 500, 5 }); for (int xt = 0; xt < treeWidth; xt++) { for (int yt = 0; yt < treeHeight; yt++) { int checkX = xt + x; int checkY = yt + y; occupied[checkX, checkY] = true; } } } } } for (int x = 0; x < tileMap.Size.x; x++) { for (int y = 0; y < tileMap.Size.y; y++) { if (tileMap.getTile(x, y).Name.Equals("tree")) tileMap.setTile(x, y, 1); } } }