Changeset 4550
- Timestamp:
- 2013-07-23 11:06:44 (10 years ago)
- Location:
- 2013/30/DenisZ/CastleMaster/CastleMaster
- Files:
-
- 8 added
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
2013/30/DenisZ/CastleMaster/CastleMaster/CastleMaster/CastleMaster.csproj
r4535 r4550 75 75 <ItemGroup> 76 76 <Compile Include="Entities\Entity.cs" /> 77 <Compile Include="Entities\EntityTestMob.cs" /> 77 78 <Compile Include="Entities\TileEntities\TileEntity.cs" /> 78 79 <Compile Include="Entities\TileEntities\TileEntityBlock.cs" /> … … 88 89 <Compile Include="Physics\BoundingRectangle.cs" /> 89 90 <Compile Include="Physics\BoundingRectangleOwner.cs" /> 91 <Compile Include="Players\Player.cs" /> 92 <Compile Include="Players\PlayerReal.cs" /> 93 <Compile Include="Players\Team.cs" /> 90 94 <Compile Include="Properties\AssemblyInfo.cs" /> 91 95 <Compile Include="Program.cs" /> 92 96 <Compile Include="Game.cs" /> 97 <Compile Include="Units\Unit.cs" /> 93 98 <Compile Include="World\Level.cs" /> 94 99 <Compile Include="World\LevelBuilder.cs" /> -
2013/30/DenisZ/CastleMaster/CastleMaster/CastleMaster/Entities/Entity.cs
r4535 r4550 3 3 using CastleMaster.World; 4 4 using Microsoft.Xna.Framework; 5 using System.Collections.Generic; 6 using System; 7 using CastleMaster.World.Tiles; 5 8 6 9 namespace CastleMaster.Entities … … 10 13 protected float x, z, width = 16.0F, depth = 16.0F; 11 14 protected Level level; 12 protected BoundingRectangle boundingRect; 15 private BoundingRectangle boundingRect; 16 protected BoundingRectangle rectOffset; 13 17 protected Vector2 renderOffset, screenPos; 14 18 protected bool isSolid = true; … … 20 24 screenPos = Vector2.Zero; 21 25 Removed = false; 26 rectOffset = new BoundingRectangle(-width / 2, -depth / 2, width / 2, depth / 2, null); 22 27 } 23 28 … … 47 52 screenPos.Y = (x + z) * Viewport.Y_SCALE - renderOffset.Y; 48 53 49 boundingRect = new BoundingRectangle(x - width / 2, z - depth / 2, x + width / 2, z + depth / 2, this); 54 boundingRect = new BoundingRectangle(x, z, x, z, this).AddSelf(rectOffset); 55 } 56 57 public void AddBoundingRect(ref List<BoundingRectangle> list, Entity ent) 58 { 59 if (ent != this) 60 list.Add(BoundingRectangle); 61 } 62 63 public virtual bool IsSolidTo(Entity ent) 64 { 65 return isSolid; 66 } 67 68 public bool Blocks(Entity ent) 69 { 70 return isSolid || ent.isSolid || IsSolidTo(ent) || ent.IsSolidTo(this); 71 } 72 73 public bool Move(float xd, float zd) 74 { 75 if (BoundingRectangle.XRight + xd > level.Width * Viewport.TILESIZE) return false; 76 if (BoundingRectangle.XLeft + xd < 0.0F) return false; 77 if (BoundingRectangle.ZFar + zd < 0.0F) return false; 78 if (BoundingRectangle.ZNear + zd > level.Height * Viewport.TILESIZE) return false; 79 80 int moveSteps = (int)(Math.Sqrt(xd * xd + zd * zd) + 1); 81 82 bool hasMoved = false; 83 for (int i = 0; i < moveSteps; i++) 84 { 85 hasMoved |= MovePart(xd / moveSteps, 0); 86 hasMoved |= MovePart(0, zd / moveSteps); 87 } 88 89 if (hasMoved) 90 { 91 screenPos.X = (x - z) * Viewport.X_SCALE - renderOffset.X; 92 screenPos.Y = (x + z) * Viewport.Y_SCALE - renderOffset.Y; 93 BoundingRectangle.Update(x, z, x, z).AddSelf(rectOffset); 94 } 95 96 return hasMoved; 97 } 98 99 private bool MovePart(float xd, float zd) 100 { 101 List<BoundingRectangle> collidables = level.GetCollidables(this, BoundingRectangle + new Vector2(xd, zd)); 102 103 foreach (BoundingRectangle collidable in collidables) 104 { 105 collidable.Owner.OnTouchedBy(this); 106 OnTouched(collidable.Owner); 107 } 108 109 collidables.RemoveAll(br => SkipCollisionCheck(br.Owner)); 110 111 if (collidables.Count > 0) 112 { 113 foreach (BoundingRectangle collidable in collidables) 114 { 115 OnCollidedWith(collidable.Owner); 116 collidable.Owner.OnCollidedBy(this); 117 } 118 119 return false; 120 } 121 122 x += xd; 123 z += zd; 124 125 BoundingRectangle.Update(x, z, x, z).AddSelf(rectOffset); 126 127 return true; 128 } 129 130 private bool SkipCollisionCheck(BoundingRectangleOwner collidableOwner) 131 { 132 if (typeof(Entity).IsAssignableFrom(collidableOwner.GetType())) 133 return !((Entity)collidableOwner).Blocks(this); 134 if (typeof(Tile).IsAssignableFrom(collidableOwner.GetType())) 135 return false; 136 return true; 50 137 } 51 138 … … 63 150 64 151 public virtual void OnRemoved() { } 152 65 153 } 66 154 } -
2013/30/DenisZ/CastleMaster/CastleMaster/CastleMaster/Entities/TileEntities/TileEntityBlock.cs
r4535 r4550 14 14 this.spriteY = spriteY; 15 15 16 renderOffset.Y = 8;16 renderOffset.Y = 16; 17 17 } 18 18 -
2013/30/DenisZ/CastleMaster/CastleMaster/CastleMaster/Game.cs
r4535 r4550 10 10 using Keys = System.Windows.Forms.Keys; 11 11 using Viewport = CastleMaster.Graphics.Viewport; 12 using CastleMaster.Players; 12 13 13 14 namespace CastleMaster … … 20 21 public const int WIDTH = 800; 21 22 public const int HEIGHT = 600; 22 public const string TITLE = "Castle Master Alpha 0. 1";23 public const string TITLE = "Castle Master Alpha 0.2"; 23 24 24 25 private const string UPS_TEXT = " UPS: "; … … 31 32 private Camera camera; 32 33 private LevelTest level; 33 private int cameraSpeed = 10;34 private Player[] players; 34 35 35 36 #region Counter … … 47 48 input = new InputHandler(Window); 48 49 Random = new FastRandom(); 50 players = new Player[2]; 49 51 } 50 52 … … 59 61 graphics.PreferredBackBufferWidth = WIDTH; 60 62 graphics.PreferredBackBufferHeight = HEIGHT; 61 graphics.SynchronizeWithVerticalRetrace = false;63 graphics.SynchronizeWithVerticalRetrace = true; 62 64 graphics.ApplyChanges(); 63 65 Window.Title = TITLE; … … 80 82 81 83 input.RegisterMouseKey(MouseButtons.Middle); 84 } 85 86 private void AddPlayer(Player p) 87 { 88 players[p.Team.ID] = p; 89 level.SetPlayer(p, p.Team.ID); 82 90 } 83 91 … … 109 117 level = new LevelTest(Resources.LEVEL_TEST); 110 118 camera = new Camera(level); 119 AddPlayer(new PlayerReal(new Team("The men", 0), level, camera)); 111 120 } 112 121 … … 119 128 { 120 129 input.Update(); 121 camera.Update();122 130 123 131 if (InputHandler.HasKeyBeenPressed(Keys.F4)) 124 132 IsFixedTimeStep = !IsFixedTimeStep; 125 126 133 if (InputHandler.HasKeyBeenPressed(Keys.Escape)) 127 134 this.Exit(); 128 if (InputHandler.IsKeyDown(Keys.W))129 camera.YTop -= cameraSpeed;130 if (InputHandler.IsKeyDown(Keys.S))131 camera.YTop += cameraSpeed;132 if (InputHandler.IsKeyDown(Keys.A))133 camera.XLeft -= cameraSpeed;134 if (InputHandler.IsKeyDown(Keys.D))135 camera.XLeft += cameraSpeed;136 135 137 136 if (InputHandler.MouseScrollDelta > 1) … … 139 138 else if (InputHandler.MouseScrollDelta < 0) 140 139 camera.Zoom(-Viewport.ZOOM_STEP); 140 141 foreach (Player p in players) 142 if (p != null) 143 p.Update(); 144 145 camera.Update(); 141 146 142 147 level.Update(); -
2013/30/DenisZ/CastleMaster/CastleMaster/CastleMaster/Graphics/Camera.cs
r4535 r4550 10 10 private int xOffs = 0, yOffs = 0; 11 11 private Vector2 mouseWorldPos, selectorPos, selectorPosZoomed; 12 private Point mouseTilePos , oldMousePos;12 private Point mouseTilePos; 13 13 private Level level; 14 private bool moveWorldWithMouse = false;15 14 16 15 public Camera(Level level) … … 21 20 selectorPosZoomed = Vector2.Zero; 22 21 mouseTilePos = Point.Zero; 23 oldMousePos = Point.Zero;24 22 } 25 23 … … 52 50 public void Update() 53 51 { 54 if (InputHandler.HasMouseButtonBeenPressed(MouseButtons.Middle) || moveWorldWithMouse) 55 { 56 if (!moveWorldWithMouse) 57 { 58 moveWorldWithMouse = true; 59 oldMousePos = InputHandler.MousePos; 60 } 61 62 if (InputHandler.IsMouseButtonDown(MouseButtons.Middle)) 63 { 64 xOffs -= (InputHandler.MousePos.X - oldMousePos.X); 65 yOffs -= (InputHandler.MousePos.Y - oldMousePos.Y); 66 67 oldMousePos = InputHandler.MousePos; 68 } 69 else moveWorldWithMouse = false; 70 } 52 71 53 72 54 mouseWorldPos = Viewport.ScreenToWorld(InputHandler.MouseX + xOffs, InputHandler.MouseY + yOffs); -
2013/30/DenisZ/CastleMaster/CastleMaster/CastleMaster/Graphics/Resources.cs
r4535 r4550 9 9 public static int SPRITESHEET_ICONS { get; private set; } 10 10 public static Texture2D LEVEL_TEST { get; private set; } 11 public static int SPRITESHEET_WOODCUTTER { get; private set; } 11 12 12 13 public static void LoadResources(ContentManager cm, RenderHelper renderer) … … 14 15 SPRITESHEET_TILES = renderer.RegisterSpriteSheet(new SpriteSheet(cm.Load<Texture2D>("tiles/tilesheet"), 32, 32)); 15 16 SPRITESHEET_ICONS = renderer.RegisterSpriteSheet(new SpriteSheet(cm.Load<Texture2D>("misc/icons"), 16, 16)); 17 SPRITESHEET_WOODCUTTER = renderer.RegisterSpriteSheet(new SpriteSheet(cm.Load<Texture2D>("mobs/woodcutter"), 32, 32)); 16 18 LEVEL_TEST = cm.Load<Texture2D>("levels/levelTest"); 17 19 } -
2013/30/DenisZ/CastleMaster/CastleMaster/CastleMaster/Physics/BoundingRectangle.cs
r4535 r4550 1 1 2 using Microsoft.Xna.Framework; 2 3 namespace CastleMaster.Physics 3 4 { … … 47 48 } 48 49 50 public BoundingRectangle AddSelf(float x0, float z0, float x1, float z1) 51 { 52 this.x0 += x0; 53 this.z0 += z0; 54 this.x1 += x1; 55 this.z1 += z1; 56 57 return this; 58 } 59 60 public BoundingRectangle AddSelf(BoundingRectangle br) 61 { 62 this.x0 += br.x0; 63 this.z0 += br.z0; 64 this.x1 += br.x1; 65 this.z1 += br.z1; 66 67 return this; 68 } 69 70 public static BoundingRectangle operator +(BoundingRectangle br, Vector2 vec) 71 { 72 return new BoundingRectangle(br.x0 + vec.X, br.z0 + vec.Y, br.x1 + vec.X, br.z1 + vec.Y, br.owner); 73 } 49 74 } 50 75 } -
2013/30/DenisZ/CastleMaster/CastleMaster/CastleMaster/World/Level.cs
r4535 r4550 8 8 using System.Collections.Generic; 9 9 using Viewport = CastleMaster.Graphics.Viewport; 10 using CastleMaster.Players; 10 11 11 12 namespace CastleMaster.World … … 57 58 private EntityComprarer comparer = new EntityComprarer(); 58 59 private AnimationHelper waterAnimation = new AnimationHelper(10, 2); 60 private Player[] players; 59 61 60 62 public Level(Texture2D tileMap) … … 64 66 height = tileMap.Height; 65 67 68 players = new Player[2]; 66 69 tiles = new int[width * height]; 67 70 data = new byte[width * height]; … … 80 83 InitLevel(); 81 84 } 85 86 public Player[] Players { get { return players; } } 82 87 83 88 public int Width { get { return width; } } … … 181 186 } 182 187 188 public void SetPlayer(Player player, int id) 189 { 190 players[id] = player; 191 } 192 183 193 public void SetTile(int tileX, int tileZ, int tileID) 184 194 { … … 242 252 } 243 253 254 public List<BoundingRectangle> GetCollidables(Entity ent, BoundingRectangle entBR) 255 { 256 List<BoundingRectangle> result = new List<BoundingRectangle>(); 257 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); 262 263 for (int z = z0; z <= z1; z++) 264 { 265 if (z < 0 || z >= height) continue; 266 for (int x = x0; x <= x1; x++) 267 { 268 if (x < 0 || x >= width) continue; 269 270 foreach (Entity e in entitiesInTiles[x + z * width]) 271 { 272 if (e != ent && entBR.Intersects(e.BoundingRectangle)) 273 e.AddBoundingRect(ref result, ent); 274 } 275 276 Tile t = registeredTiles[tiles[x + z * width]]; 277 if (t.ID != TILE_VOID && t.IsSolid && t.GetBoundingRect(x, z).Intersects(entBR)) 278 t.AddBoundingRect(ref result, x, z); 279 } 280 } 281 282 return result; 283 } 284 244 285 private bool IsValidPos(int tileX, int tileZ) 245 286 { -
2013/30/DenisZ/CastleMaster/CastleMaster/CastleMaster/World/LevelTest.cs
r4535 r4550 2 2 using CastleMaster.World.Tiles; 3 3 using Microsoft.Xna.Framework.Graphics; 4 using CastleMaster.Entities; 4 5 5 6 namespace CastleMaster.World … … 24 25 LevelBuilder lb = new LevelBuilder(this, tileMap); 25 26 27 lb.AddTile(0xFFFF0000, TILE_FLOOR); 26 28 lb.AddTile(0xFF404040, TILE_FLOOR); 27 29 lb.AddTile(0xFF0094FF, TILE_WATER); … … 36 38 lb.AddTile(0xFFFF0000, TILE_FLOOR); 37 39 lb.AddTile(0xFFFF0001, TILE_FLOOR); 38 lb.AddEntity(0xFFFF6A00, typeof(TileEntityBlock), 0.0F, 0.0F, this, 0, 0); 39 lb.AddEntity(0xFFE55B00, typeof(TileEntityBlock), 0.0F, 0.0F, this, 4, 1); 40 lb.AddEntity(0xFF00B200, typeof(TileEntityBlock), 0.0F, 0.0F, this, 2, 2); 41 lb.AddEntity(0xFF00CC00, typeof(TileEntityBlock), 0.0F, 0.0F, this, 1, 2); 42 lb.AddEntity(0xFF00E500, typeof(TileEntityBlock), 0.0F, 0.0F, this, 0, 2); 43 lb.AddEntity(0xFF00FF00, typeof(TileEntityBlock), 0.0F, 0.0F, this, 6, 0); 40 lb.AddEntity(0xFFFF6A00, typeof(TileEntityBlock), 8.0F, 8.0F, this, 0, 0); 41 lb.AddEntity(0xFFE55B00, typeof(TileEntityBlock), 8.0F, 8.0F, this, 4, 1); 42 lb.AddEntity(0xFF00B200, typeof(TileEntityBlock), 8.0F, 8.0F, this, 2, 2); 43 lb.AddEntity(0xFF00CC00, typeof(TileEntityBlock), 8.0F, 8.0F, this, 1, 2); 44 lb.AddEntity(0xFF00E500, typeof(TileEntityBlock), 8.0F, 8.0F, this, 0, 2); 45 lb.AddEntity(0xFF00FF00, typeof(TileEntityBlock), 8.0F, 8.0F, this, 6, 0); 46 lb.AddEntity(0xFFFF0000, typeof(EntityTestMob), 0.0F, 0.0F, this); 44 47 45 48 lb.BuildLevel(); -
2013/30/DenisZ/CastleMaster/CastleMaster/CastleMaster/World/Tiles/Tile.cs
r4535 r4550 1 1 using CastleMaster.Graphics; 2 2 using Microsoft.Xna.Framework; 3 using CastleMaster.Physics; 4 using CastleMaster.Entities; 5 using System.Collections.Generic; 3 6 4 7 namespace CastleMaster.World.Tiles 5 8 { 6 public class Tile 9 public class Tile : BoundingRectangleOwner 7 10 { 8 11 private int id; … … 10 13 public Tile(Level level) 11 14 { 15 IsSolid = false; 12 16 level.RegisteredTiles.Add(this); 13 17 id = level.RegisteredTiles.Count - 1; … … 16 20 public int ID { get { return id; } } 17 21 22 public bool IsSolid { get; protected set; } 23 24 public virtual BoundingRectangle GetBoundingRect(int xTile, int zTile) 25 { 26 return new BoundingRectangle(xTile * Viewport.ZOOM, zTile * Viewport.ZOOM, (xTile + 1) * Viewport.ZOOM, (zTile + 1) * Viewport.ZOOM, this); 27 } 28 29 public virtual void AddBoundingRect(ref List<BoundingRectangle> list, int xTile, int zTile) 30 { 31 list.Add(new BoundingRectangle(xTile * Viewport.ZOOM, zTile * Viewport.ZOOM, (xTile + 1) * Viewport.ZOOM, (zTile + 1) * Viewport.ZOOM, this)); 32 } 33 18 34 public virtual void Update(Level level, int tileX, int tileZ) { } 19 35 20 36 public virtual void Render(RenderHelper renderer, Level level, Vector2 screenPos, int tileX, int tileZ, byte dataVal) { } 37 38 public virtual void OnTouched(BoundingRectangleOwner touching) { } 39 40 public virtual void OnTouchedBy(BoundingRectangleOwner toucher) { } 41 42 public virtual void OnCollidedWith(BoundingRectangleOwner colliding) { } 43 44 public virtual void OnCollidedBy(BoundingRectangleOwner collidable) { } 21 45 } 22 46 } -
2013/30/DenisZ/CastleMaster/CastleMaster/CastleMasterContent/CastleMasterContent.contentproj
r4535 r4550 71 71 <Processor>TextureProcessor</Processor> 72 72 </Compile> 73 <Compile Include="mobs\ units.png">74 <Name> units</Name>73 <Compile Include="mobs\woodcutter.png"> 74 <Name>woodcutter</Name> 75 75 <Importer>TextureImporter</Importer> 76 76 <Processor>TextureProcessor</Processor>
Note: See TracChangeset
for help on using the changeset viewer.