Changeset 3515
- Timestamp:
- 2012-07-03 20:04:30 (11 years ago)
- Location:
- 2012/27/DenisZ/TheDungeonGame/TheDungeonGame/TheDungeonGame
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
2012/27/DenisZ/TheDungeonGame/TheDungeonGame/TheDungeonGame/LevelGenerator.cs
r3489 r3515 13 13 private int roomAmountHor, roomAmountVert; 14 14 private Game game; 15 private TestRoom[,] rooms;16 private List< TestRoom> roomList = new List<TestRoom>();15 private Room[,] rooms; 16 private List<Room> roomList = new List<Room>(); 17 17 18 18 public LevelGenerator(Game game, Vector size, Vector roomamount) … … 24 24 roomAmountVert = (int)roomamount.Y; 25 25 26 rooms = new TestRoom[roomAmountHor, roomAmountVert];26 rooms = new Room[roomAmountHor, roomAmountVert]; 27 27 } 28 28 29 public void buildLevel()29 public Vector CenterRoom 30 30 { 31 foreach (TestRoom room in roomList)31 get 32 32 { 33 room.buildLevel(); 33 return new Vector(roomAmountHor / 2, roomAmountVert / 2); 34 } 35 } 36 37 public void buildDungeon() 38 { 39 foreach (Room room in roomList) 40 { 41 room.buildLevel(); 42 } 43 } 44 45 public void destroyDungeon() 46 { 47 foreach (Room room in roomList) 48 { 49 room.destroyLevel(); 34 50 } 35 51 } … … 38 54 { 39 55 int roomAmount = RandomGen.NextInt(minRooms, maxRooms); 40 TestRoom lastRoom = null;56 Room lastRoom = null; 41 57 42 58 for (int room = 0; room < roomAmount; room++) … … 53 69 bool[] sideChecked = new bool[4]; 54 70 55 // while (checkedSides < 4) 56 // { 57 int dir = 2; 58 /* if (sideChecked[dir]) 59 { 60 checkedSides++; 61 continue; 62 } */ 63 // if (lastRoom == null) return; 64 checkedSides++; 65 lastRoom = tryCreateRoom(dir, VecMath.add(lastRoom.RoomsPosOnGrid, RoomDirection.getOffsetFromDir(dir)), lastRoom); 66 // } 71 while (checkedSides < 4) 72 { 73 int dir = RandomGen.NextInt(4); 74 if (sideChecked[dir]) 75 { 76 checkedSides++; 77 continue; 78 } 79 else 80 { 81 checkedSides++; 82 sideChecked[dir] = true; 83 } 84 85 if (tryCreateRoom(dir, VecMath.add(lastRoom.PosOnGrid, RoomDirection.getOffsetFromDir(dir)), lastRoom)) 86 { 87 lastRoom = currentRoom; 88 break; 89 } 90 91 } 67 92 68 93 } 69 94 } 70 95 71 private TestRoom tryCreateRoom(int dir, Vector pos, Room lastRoom) 96 97 98 private Room currentRoom; 99 private bool tryCreateRoom(int dir, Vector pos, Room lastRoom) 72 100 { 73 if (getRoomFromDirection(lastRoom, dir) != null) return null; 74 75 TestRoom currentRoom = new TestRoom(game, VecMath.mul(pos, new Vector(TheDungeonGame.ROOMWIDTH, -TheDungeonGame.ROOMHEIGHT)), TheDungeonGame.roomSize, TheDungeonGame.ROOMTHICKNESS); 76 placeRoomAt(currentRoom, (int)pos.X, (int)pos.Y); 77 return currentRoom; 101 if (getRoomFromDirection(lastRoom, dir) != null) return false; 102 currentRoom = new TestRoom(game, VecMath.mul(pos, new Vector(TheDungeonGame.ROOMWIDTH, -TheDungeonGame.ROOMHEIGHT)), TheDungeonGame.roomSize, TheDungeonGame.ROOMTHICKNESS); 103 return placeRoomAt(currentRoom, (int)pos.X, (int)pos.Y); 78 104 } 79 105 80 public void placeRoomAt(TestRoom room, int x, int y)106 public bool placeRoomAt(Room room, int x, int y) 81 107 { 82 if ( x > roomAmountHor || x < 0 || y > roomAmountVert || y < 0 || getRoomAt(x, y) != null) throw new Exception("Cant place Room there!");83 room. RoomsPosOnGrid = new Vector(x, y);108 if (!isValidPos(x, y) || getRoomAt(x, y) != null) return false; 109 room.PosOnGrid = new Vector(x, y); 84 110 rooms[x, y] = room; 85 111 roomList.Add(room); 112 return true; 86 113 } 87 114 88 115 public Room getRoomAt(int x, int y) 89 116 { 90 if ( x > roomAmountHor || x < 0 || y > roomAmountVert || y < 0) return null;117 if (!isValidPos(x, y)) return null; 91 118 return rooms[x, y]; 92 119 } … … 94 121 public Room getRoomFromDirection(Room source, int dir) 95 122 { 96 int roomX = (int)source. RoomsPosOnGrid.X;97 int roomY = (int)source. RoomsPosOnGrid.Y;123 int roomX = (int)source.PosOnGrid.X; 124 int roomY = (int)source.PosOnGrid.Y; 98 125 99 if (dir == RoomDirection.North) 100 return getRoomAt(roomX, roomY - 1); 101 if (dir == RoomDirection.East) 102 return getRoomAt(roomX + 1, roomY); 103 if (dir == RoomDirection.North) 104 return getRoomAt(roomX, roomY + 1); 105 if (dir == RoomDirection.West) 106 return getRoomAt(roomX - 1, roomY); 107 108 return null; 126 Vector roomPos = VecMath.add(source.PosOnGrid, RoomDirection.getOffsetFromDir(dir)); 127 return getRoomAt((int)roomPos.X, (int)roomPos.Y); 109 128 } 110 129 … … 115 134 } 116 135 117 p ublic Vector CenterRoom136 private bool isValidPos(int x, int y) 118 137 { 119 get 120 { 121 return new Vector(roomAmountHor / 2, roomAmountVert / 2); 122 } 138 return !(x >= roomAmountHor || x < 0 || y >= roomAmountVert || y < 0); 123 139 } 140 141 124 142 } 125 143 } -
2012/27/DenisZ/TheDungeonGame/TheDungeonGame/TheDungeonGame/Player.cs
r3477 r3515 14 14 public const double playerHeight = 75; 15 15 // private PhysicsObject head; 16 p rivateVector movementVector = Vector.Zero;17 p rivatebool isMoving = false;16 public Vector movementVector = Vector.Zero; 17 public bool isMoving = false; 18 18 private Keyboard keyboard; 19 19 private double speed = 200; 20 20 21 public Player( Vector pos)21 public Player(Game game, Vector pos) 22 22 : base(playerWidth, playerHeight) 23 23 { 24 keyboard = Game.Keyboard;24 keyboard = game.Keyboard; 25 25 CanRotate = false; 26 26 setupKeys(); … … 40 40 } 41 41 42 public override void Update(Time time)42 public void tick() 43 43 { 44 if ( !isMoving)44 if (isMoving) 45 45 { 46 46 movementVector = Vector.Zero; … … 48 48 } 49 49 50 isMoving = false; 51 base.Update(time); 50 isMoving = false; 52 51 } 53 52 -
2012/27/DenisZ/TheDungeonGame/TheDungeonGame/TheDungeonGame/Room.cs
r3489 r3515 10 10 abstract class Room 11 11 { 12 public Vector Position { get; set; } 12 13 public Vector Position; 13 14 public double width, height, thickness; 14 15 protected List<IPhysicsObject> roomObjects = new List<IPhysicsObject>(); … … 20 21 protected int bWidth, bHeight; 21 22 public Game Game { get; set; } 22 public Vector RoomsPosOnGrid { get; set; }23 public Vector PosOnGrid { get; set; } 23 24 24 25 public Room(Game game, Vector pos, Vector size, double thickness) … … 70 71 { 71 72 obj.Destroy(); 73 } 74 foreach (IPhysicsObject obj in insideObjects) 75 { 76 if (obj != null) 77 obj.Destroy(); 72 78 } 73 79 } -
2012/27/DenisZ/TheDungeonGame/TheDungeonGame/TheDungeonGame/TheDungeonGame.cs
r3489 r3515 17 17 public static Vector roomSize = new Vector(ROOMWIDTH, ROOMHEIGHT); 18 18 private Player player; 19 private LevelGenerator generator; 19 20 20 21 public override void Begin() … … 24 25 Level.Width = ROOMWIDTH * 20; 25 26 Level.Height = ROOMHEIGHT * 20; 26 // Level.CreateBorders();27 // Level.CreateBorders(); 27 28 Camera.ZoomToLevel(); 28 29 29 LevelGeneratorgenerator = new LevelGenerator(this, Level.Size, new Vector(20, 20));30 generator.generateRandomLevel( 5, 10);31 generator.build Level();30 generator = new LevelGenerator(this, Level.Size, new Vector(20, 20)); 31 generator.generateRandomLevel(1, 10); 32 generator.buildDungeon(); 32 33 33 player = new Player( Vector.Zero);34 player = new Player(this, Vector.Zero); 34 35 Add(player, 1); 35 36 36 Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Lopeta peli"); 37 Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Lopeta peli"); 38 Keyboard.Listen(Key.R, ButtonState.Pressed, regenerateLevel, null); 39 } 40 41 private void regenerateLevel() 42 { 43 generator.destroyDungeon(); 44 generator = new LevelGenerator(this, Level.Size, new Vector(20, 20)); 45 generator.generateRandomLevel(1, 10); 46 generator.buildDungeon(); 37 47 } 38 48 39 49 protected override void Update(Time time) 40 50 { 41 player.Update(time); 42 base.Update(time); 51 try // Tapa pakottaa Updatea toimimaan LevelGeneratorin kanssa 52 { 53 base.Update(time); 54 } 55 catch (FormatException) // Kaatuu tähän melkein kokoajan kun yrittää luoda tasoa. Mun koodi liian "likainen", tai hidas? 56 { 57 return; // Skipataan "virheelisiä" tickea ja huijataan peliä niin kuin pahat merimiehet (dirty pirates, Arr) 58 } 59 60 player.tick(); 43 61 } 44 62 } -
2012/27/DenisZ/TheDungeonGame/TheDungeonGame/TheDungeonGame/TheDungeonGame.csproj
r3477 r3515 19 19 <ApplicationIcon>Game.ico</ApplicationIcon> 20 20 <Thumbnail>GameThumbnail.png</Thumbnail> 21 <IsWebBootstrapper>false</IsWebBootstrapper> 22 <ReferencePath>$(registry:HKEY_LOCAL_MACHINE\Software\Jypeli@Install_Dir)\lib\x86</ReferencePath> 21 23 <PublishUrl>publish\</PublishUrl> 22 24 <Install>true</Install> … … 31 33 <ApplicationRevision>0</ApplicationRevision> 32 34 <ApplicationVersion>1.0.0.%2a</ApplicationVersion> 33 <IsWebBootstrapper>false</IsWebBootstrapper>34 35 <UseApplicationTrust>false</UseApplicationTrust> 35 36 <BootstrapperEnabled>true</BootstrapperEnabled> 36 <ReferencePath>$(registry:HKEY_LOCAL_MACHINE\Software\Jypeli@Install_Dir)\lib\x86</ReferencePath>37 37 </PropertyGroup> 38 38 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' "> … … 62 62 </PropertyGroup> 63 63 <ItemGroup> 64 <Reference Include="Jypeli"> 64 <Reference Include="Jypeli4, Version=4.2.1.0, Culture=neutral, processorArchitecture=x86"> 65 <SpecificVersion>False</SpecificVersion> 66 <HintPath>F:\Program Files\Jypeli\lib\x86\Jypeli4.dll</HintPath> 65 67 </Reference> 66 68 <Reference Include="Microsoft.Xna.Framework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553, processorArchitecture=x86">
Note: See TracChangeset
for help on using the changeset viewer.