Changeset 4581
- Timestamp:
- 2013-07-23 14:56:43 (10 years ago)
- Location:
- 2013/30/DenisZ
- Files:
-
- 23 added
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
2013/30/DenisZ/CastleMaster/CastleMaster/CastleMaster/CastleMaster.csproj
r4550 r4581 74 74 </ItemGroup> 75 75 <ItemGroup> 76 <Compile Include="Ai\AStar.cs" /> 77 <Compile Include="Ai\Node.cs" /> 78 <Compile Include="Ai\Orders\Order.cs" /> 79 <Compile Include="Ai\Orders\OrderMove.cs" /> 76 80 <Compile Include="Entities\Entity.cs" /> 77 <Compile Include="Entities\EntityTestMob.cs" />78 81 <Compile Include="Entities\TileEntities\TileEntity.cs" /> 79 82 <Compile Include="Entities\TileEntities\TileEntityBlock.cs" /> … … 95 98 <Compile Include="Program.cs" /> 96 99 <Compile Include="Game.cs" /> 100 <Compile Include="Units\Mobs\Mob.cs" /> 101 <Compile Include="Units\Mobs\MobWoodcutter.cs" /> 97 102 <Compile Include="Units\Unit.cs" /> 98 103 <Compile Include="World\Level.cs" /> … … 142 147 </BootstrapperPackage> 143 148 </ItemGroup> 149 <ItemGroup /> 144 150 <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> 145 151 <Import Project="$(MSBuildExtensionsPath)\Microsoft\XNA Game Studio\Microsoft.Xna.GameStudio.targets" /> -
2013/30/DenisZ/CastleMaster/CastleMaster/CastleMaster/Entities/Entity.cs
r4550 r4581 17 17 protected Vector2 renderOffset, screenPos; 18 18 protected bool isSolid = true; 19 protected float moveSpeed = 0.0F; 19 20 20 21 public Entity(Level level) … … 40 41 set { z = value; } 41 42 } 43 44 public float Width { get { return width; } } 45 46 public float Depth { get { return depth; } } 42 47 43 48 public BoundingRectangle BoundingRectangle { get { return boundingRect; } } … … 68 73 public bool Blocks(Entity ent) 69 74 { 70 return isSolid || ent.isSolid || IsSolidTo(ent) ||ent.IsSolidTo(this);75 return isSolid && ent.isSolid && IsSolidTo(ent) && ent.IsSolidTo(this); 71 76 } 72 77 … … 91 96 screenPos.X = (x - z) * Viewport.X_SCALE - renderOffset.X; 92 97 screenPos.Y = (x + z) * Viewport.Y_SCALE - renderOffset.Y; 93 BoundingRectangle.Update(x, z, x, z).AddSelf(rectOffset);94 98 } 95 99 … … 99 103 private bool MovePart(float xd, float zd) 100 104 { 105 if (xd != 0 && zd != 0) return false; 106 101 107 List<BoundingRectangle> collidables = level.GetCollidables(this, BoundingRectangle + new Vector2(xd, zd)); 102 108 -
2013/30/DenisZ/CastleMaster/CastleMaster/CastleMaster/Game.cs
r4550 r4581 82 82 83 83 input.RegisterMouseKey(MouseButtons.Middle); 84 input.RegisterMouseKey(MouseButtons.Right); 85 input.RegisterMouseKey(MouseButtons.Left); 84 86 } 85 87 … … 118 120 camera = new Camera(level); 119 121 AddPlayer(new PlayerReal(new Team("The men", 0), level, camera)); 122 level.InitLevel(); 123 camera.CenterOn(level.Width / 2 * Viewport.TILESIZE, level.Height / 2 * Viewport.TILESIZE); 120 124 } 121 125 -
2013/30/DenisZ/CastleMaster/CastleMaster/CastleMaster/Graphics/Camera.cs
r4550 r4581 46 46 set { yOffs = value - Game.HEIGHT; } 47 47 } 48 49 public Vector2 MouseWorldPos { get { return mouseWorldPos; } } 48 50 #endregion 49 51 -
2013/30/DenisZ/CastleMaster/CastleMaster/CastleMaster/Physics/BoundingRectangle.cs
r4550 r4581 39 39 public bool Intersects(float x0, float z0, float x1, float z1) 40 40 { 41 if (x0 >= this.x1 && x1 <= this.x0 && z0 >= this.z1 &&z1 <= this.z0) return false;41 if (x0 >= this.x1 || x1 <= this.x0 || z0 >= this.z1 || z1 <= this.z0) return false; 42 42 return true; 43 43 } … … 72 72 return new BoundingRectangle(br.x0 + vec.X, br.z0 + vec.Y, br.x1 + vec.X, br.z1 + vec.Y, br.owner); 73 73 } 74 75 public BoundingRectangle Scale(float s) 76 { 77 this.x0 *= s; 78 this.z0 *= s; 79 this.x1 *= s; 80 this.z1 *= s; 81 82 return this; 83 } 74 84 } 75 85 } -
2013/30/DenisZ/CastleMaster/CastleMaster/CastleMaster/Players/Player.cs
r4550 r4581 21 21 } 22 22 23 public Level Level { get { return level; } } 24 25 public Camera Camera { get { return camera; } } 26 23 27 public Team Team { get { return team; } } 24 28 -
2013/30/DenisZ/CastleMaster/CastleMaster/CastleMaster/Players/PlayerReal.cs
r4550 r4581 8 8 using System.Windows.Forms; 9 9 using Microsoft.Xna.Framework; 10 using CastleMaster.Units; 10 11 11 12 namespace CastleMaster.Players … … 15 16 private bool moveWorldWithMouse = false; 16 17 private Point oldMousePos; 18 private Unit selectedUnit; 17 19 18 20 public PlayerReal(Team team, Level level, Camera camera) … … 41 43 else moveWorldWithMouse = false; 42 44 } 45 46 if (InputHandler.HasMouseButtonBeenPressed(MouseButtons.Right)) 47 { 48 Unit u = SelectUnit(); 49 if (selectedUnit != null) 50 selectedUnit.OnSelectLost(); 51 selectedUnit = u; 52 if (selectedUnit != null) 53 selectedUnit.OnSelectGain(); 54 } 55 56 if (InputHandler.HasMouseButtonBeenPressed(MouseButtons.Left)) 57 { 58 if (selectedUnit != null) 59 selectedUnit.OnFunctionClick(); 60 } 61 } 62 63 public Unit SelectUnit() 64 { 65 float mouseX = InputHandler.MouseX + camera.XLeft; 66 float mouseY = InputHandler.MouseY + camera.YTop; 67 68 float r = 1.0F; 69 List<Unit> units = level.GetUnitsWithinScreenSpace(mouseX - r, mouseY - r, mouseX + r, mouseY + r, this); 70 71 Unit current = null; 72 float dist = 0.0F; 73 foreach (Unit u in units) 74 { 75 float udist = u.DistanceFromScreenSpaceSqr(mouseX, mouseY); 76 if (current == null || udist < dist) 77 { 78 current = u; 79 dist = udist; 80 } 81 } 82 83 return current; 43 84 } 44 85 } -
2013/30/DenisZ/CastleMaster/CastleMaster/CastleMaster/Units/Unit.cs
r4550 r4581 6 6 using CastleMaster.World; 7 7 using CastleMaster.Players; 8 using CastleMaster.Physics; 9 using Microsoft.Xna.Framework; 10 using CastleMaster.Graphics; 8 11 9 12 namespace CastleMaster.Units … … 12 15 { 13 16 protected bool isSelectable, isSelected; 17 protected BoundingRectangle screenRect; 18 protected BoundingRectangle screenRectOffset; 19 protected Vector2 highlightOffset = Vector2.Zero; 20 protected Point spriteSize; 21 private float timer = -MathHelper.TwoPi, arrowSpeed = 0.1F; 22 private Vector2 arrowOffs = Vector2.Zero; 23 private Player owner; 14 24 15 25 public Unit(Level level, Player owner) 16 26 : base(level) 17 27 { 28 this.owner = owner; 29 30 spriteSize = new Point(32, 32); 31 screenRectOffset = new BoundingRectangle(0, 0, spriteSize.X, spriteSize.Y, null); 18 32 isSelectable = true; 19 33 isSelected = false; 20 34 } 35 36 public bool IsSelectable { get { return isSelectable; } } 37 38 public Player Owner { get { return owner; } } 39 40 public override void Init() 41 { 42 base.Init(); 43 } 44 45 public virtual void OnSelectGain() 46 { 47 isSelected = true; 48 } 49 50 public virtual void OnSelectLost() 51 { 52 isSelected = false; 53 } 54 55 public virtual void OnFunctionClick() { } 56 57 public override void Update() 58 { 59 if (isSelected) 60 { 61 timer += arrowSpeed; 62 if (timer >= MathHelper.TwoPi) timer = -MathHelper.TwoPi; 63 arrowOffs.Y = (float)Math.Sin(timer) * 2.0F; 64 } 65 } 66 67 public bool IntersectsWithScreenSpace(float x0, float y0, float x1, float y1) 68 { 69 if (!isSelectable) return false; 70 screenRect = new BoundingRectangle(screenPos.X, screenPos.Y, screenPos.X, screenPos.Y, this).AddSelf(screenRectOffset).Scale(Viewport.ZOOM); 71 return screenRect.Intersects(x0, y0, x1, y1); 72 } 73 74 public float DistanceTo(float x, float z) 75 { 76 float xd = X - x; 77 float zd = Z - z; 78 79 return (float)(Math.Sqrt(xd * xd + zd * zd)); 80 } 81 82 public float DistanceFromScreenSpaceSqr(float x, float y) 83 { 84 float dx = screenPos.X * Viewport.ZOOM - x; 85 float dy = screenPos.Y * Viewport.ZOOM - y; 86 87 return dx * dx + dy * dy; 88 } 89 90 public virtual void RenderHighLight(RenderHelper renderer) 91 { 92 renderer.Render((screenPos + highlightOffset + arrowOffs) * Viewport.ZOOM, 1, 0, Resources.SPRITESHEET_ICONS, Viewport.ZOOM); 93 } 94 95 public override void Render(RenderHelper renderer) 96 { 97 if (isSelected) 98 RenderHighLight(renderer); 99 } 21 100 } 22 101 } -
2013/30/DenisZ/CastleMaster/CastleMaster/CastleMaster/World/Level.cs
r4550 r4581 9 9 using Viewport = CastleMaster.Graphics.Viewport; 10 10 using CastleMaster.Players; 11 using CastleMaster.Units; 12 using CastleMaster.Units.Mobs; 11 13 12 14 namespace CastleMaster.World … … 53 55 private List<TileEntity> tileEntities; 54 56 private List<Entity>[] entitiesInTiles; 55 private SortedSet<Entity> entitesToRender; 57 private List<Entity> entitesToRender; 58 private List<Unit>[] units; 56 59 private int width, height; 57 60 protected Texture2D tileMap; … … 74 77 InitTiles(); 75 78 79 units = new List<Unit>[2]; 80 units[0] = new List<Unit>(); 81 units[1] = new List<Unit>(); 82 76 83 entities = new List<Entity>(); 77 84 tileEntities = new List<TileEntity>(); … … 79 86 for (int i = 0; i < width * height; i++) 80 87 entitiesInTiles[i] = new List<Entity>(); 81 entitesToRender = new SortedSet<Entity>(comparer); 82 83 InitLevel(); 88 entitesToRender = new List<Entity>(); 84 89 } 85 90 … … 96 101 protected virtual void InitTiles() { } 97 102 98 p rotectedvirtual void InitLevel() { }103 public virtual void InitLevel() { } 99 104 100 105 public void RenderBackground(Camera camera, RenderHelper renderer) … … 135 140 if (entitesToRender.Count > 0) 136 141 { 142 entitesToRender.Sort(comparer); 143 137 144 renderer.SetOffset(camera); 138 145 … … 168 175 ent.Update(); 169 176 170 if (ent.Removed)171 {172 RemoveEntity(ent);173 TakeEntity(ent, xTile_old, zTile_old);174 continue;175 }177 //if (ent.Removed) 178 //{ 179 // RemoveEntity(ent); 180 // TakeEntity(ent, xTile_old, zTile_old); 181 // continue; 182 //} 176 183 177 184 int xTile = (int)(ent.X / Viewport.TILESIZE); … … 230 237 entities.Add(ent); 231 238 239 if(typeof(Unit).IsAssignableFrom(ent.GetType())) 240 { 241 Unit u = (Unit)ent; 242 units[u.Owner.Team.ID].Add(u); 243 } 244 232 245 InsertEntity(ent, xTile, zTile); 233 246 } … … 252 265 } 253 266 254 public List<BoundingRectangle> GetCollidables(Entity ent, BoundingRectangle entBR) 267 public List<Unit> GetUnitsWithinScreenSpace(float x0, float y0, float x1, float y1, Player player) 268 { 269 List<Unit> result = new List<Unit>(); 270 271 foreach (Unit u in units[player.Team.ID]) 272 { 273 if (u.IsSelectable && u.IntersectsWithScreenSpace(x0, y0, x1, y1)) 274 result.Add(u); 275 } 276 277 return result; 278 } 279 280 public List<BoundingRectangle> GetCollidables(Entity ent, BoundingRectangle br = null) 255 281 { 256 282 List<BoundingRectangle> result = new List<BoundingRectangle>(); 257 283 258 int x0 = (int)(entBR.XLeft / Viewport.TILESIZE); 259 int z0 = (int)(entBR.ZFar / Viewport.TILESIZE); 260 int x1 = (int)(entBR.XRight / Viewport.TILESIZE); 261 int z1 = (int)(entBR.ZNear / Viewport.TILESIZE); 284 BoundingRectangle entBR = br == null ? ent.BoundingRectangle : br; 285 286 int x0 = (int)(entBR.XLeft / Viewport.TILESIZE) - 1; 287 int z0 = (int)(entBR.ZFar / Viewport.TILESIZE) - 1; 288 int x1 = (int)(entBR.XRight / Viewport.TILESIZE) + 1; 289 int z1 = (int)(entBR.ZNear / Viewport.TILESIZE) + 1; 262 290 263 291 for (int z = z0; z <= z1; z++) … … 268 296 if (x < 0 || x >= width) continue; 269 297 270 foreach (Entity e in entitiesInTiles[x + z * width]) 298 List<Entity> entits = entitiesInTiles[x + z * width]; 299 300 foreach (Entity e in entits) 271 301 { 272 302 if (e != ent && entBR.Intersects(e.BoundingRectangle)) … … 275 305 276 306 Tile t = registeredTiles[tiles[x + z * width]]; 277 if (t.I D != TILE_VOID && t.IsSolid && t.GetBoundingRect(x, z).Intersects(entBR))307 if (t.IsSolid && t.GetBoundingRect(x, z).Intersects(entBR)) 278 308 t.AddBoundingRect(ref result, x, z); 279 309 } … … 287 317 return (tileX >= 0 && tileZ >= 0 && tileX < width && tileZ < height); 288 318 } 319 320 public bool[] BuildSolidnessTable(Mob mob) 321 { 322 bool[] result = new bool[tiles.Length]; 323 324 for (int i = 0; i < tiles.Length; i++) 325 { 326 result[i] = tiles[i] == TILE_VOID || registeredTiles[tiles[i]].IsSolid; 327 328 List<Entity> entInTiles = entitiesInTiles[i]; 329 330 if (entInTiles.Count > 0) 331 { 332 TileEntity te = entInTiles[0] as TileEntity; 333 result[i] = te != null && te.Blocks(mob); 334 } 335 } 336 337 return result; 338 } 289 339 } 290 340 } -
2013/30/DenisZ/CastleMaster/CastleMaster/CastleMaster/World/LevelTest.cs
r4550 r4581 3 3 using Microsoft.Xna.Framework.Graphics; 4 4 using CastleMaster.Entities; 5 using CastleMaster.Units; 6 using CastleMaster.Units.Mobs; 5 7 6 8 namespace CastleMaster.World … … 21 23 } 22 24 23 p rotectedoverride void InitLevel()25 public override void InitLevel() 24 26 { 25 27 LevelBuilder lb = new LevelBuilder(this, tileMap); … … 44 46 lb.AddEntity(0xFF00E500, typeof(TileEntityBlock), 8.0F, 8.0F, this, 0, 2); 45 47 lb.AddEntity(0xFF00FF00, typeof(TileEntityBlock), 8.0F, 8.0F, this, 6, 0); 46 lb.AddEntity(0xFFFF0000, typeof( EntityTestMob), 0.0F, 0.0F, this);48 lb.AddEntity(0xFFFF0000, typeof(MobWoodcutter), 8.0F, 8.0F, this, Players[0]); 47 49 48 50 lb.BuildLevel(); -
2013/30/DenisZ/CastleMaster/CastleMaster/CastleMaster/World/Tiles/Tile.cs
r4550 r4581 24 24 public virtual BoundingRectangle GetBoundingRect(int xTile, int zTile) 25 25 { 26 return new BoundingRectangle(xTile * Viewport.ZOOM, zTile * Viewport.ZOOM, (xTile + 1) * Viewport.ZOOM, (zTile + 1) * Viewport.ZOOM, this); 26 float x = xTile * Viewport.TILESIZE; 27 float z = zTile * Viewport.TILESIZE; 28 29 return new BoundingRectangle(x, z, x + Viewport.TILESIZE, z + Viewport.TILESIZE, this); 27 30 } 28 31 29 32 public virtual void AddBoundingRect(ref List<BoundingRectangle> list, int xTile, int zTile) 30 33 { 31 list.Add( new BoundingRectangle(xTile * Viewport.ZOOM, zTile * Viewport.ZOOM, (xTile + 1) * Viewport.ZOOM, (zTile + 1) * Viewport.ZOOM, this));34 list.Add(GetBoundingRect(xTile, zTile)); 32 35 } 33 36 -
2013/30/DenisZ/CastleMaster/CastleMaster/CastleMaster/World/Tiles/TileWater.cs
r4535 r4581 9 9 : base(level) 10 10 { 11 IsSolid = true; 11 12 } 12 13
Note: See TracChangeset
for help on using the changeset viewer.