Changeset 3489 for 2012


Ignore:
Timestamp:
2012-07-03 15:01:44 (8 years ago)
Author:
dezhidki
Message:

Talletus.

Location:
2012/27/DenisZ/TheDungeonGame/TheDungeonGame/TheDungeonGame
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • 2012/27/DenisZ/TheDungeonGame/TheDungeonGame/TheDungeonGame/LevelGenerator.cs

    r3477 r3489  
    44using System.Text; 
    55using Jypeli; 
     6using MathHelper; 
    67 
    78namespace Rooms 
     
    1213        private int roomAmountHor, roomAmountVert; 
    1314        private Game game; 
    14         private Room[,] rooms; 
     15        private TestRoom[,] rooms; 
     16        private List<TestRoom> roomList = new List<TestRoom>(); 
    1517 
    1618        public LevelGenerator(Game game, Vector size, Vector roomamount) 
     
    2224            roomAmountVert = (int)roomamount.Y; 
    2325 
    24             rooms = new Room[roomAmountHor, roomAmountVert]; 
     26            rooms = new TestRoom[roomAmountHor, roomAmountVert]; 
     27        } 
     28 
     29        public void buildLevel() 
     30        { 
     31            foreach (TestRoom room in roomList) 
     32            { 
     33                    room.buildLevel(); 
     34            } 
    2535        } 
    2636 
    2737        public void generateRandomLevel(int minRooms, int maxRooms) 
    2838        { 
     39            int roomAmount = RandomGen.NextInt(minRooms, maxRooms); 
     40            TestRoom lastRoom = null; 
     41 
     42            for (int room = 0; room < roomAmount; room++) 
     43            { 
     44                if (room == 0) 
     45                { 
     46                    lastRoom = new TestRoom(game, VecMath.mul(CenterRoom, new Vector(TheDungeonGame.ROOMWIDTH, -(TheDungeonGame.ROOMHEIGHT))), TheDungeonGame.roomSize, TheDungeonGame.ROOMTHICKNESS); 
     47                    placeRoomAt(lastRoom, (int)CenterRoom.X, (int)CenterRoom.Y); 
     48                    continue; 
     49                } 
     50 
     51                Room randomRoom = getRandomRoom(); 
     52                int checkedSides = 0; 
     53                bool[] sideChecked = new bool[4]; 
     54 
     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                // } 
     67 
     68            } 
     69        } 
     70 
     71        private TestRoom tryCreateRoom(int dir, Vector pos, Room lastRoom) 
     72        { 
     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; 
     78        } 
     79 
     80        public void placeRoomAt(TestRoom room, int x, int y) 
     81        { 
     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); 
     84            rooms[x, y] = room; 
     85            roomList.Add(room); 
     86        } 
     87 
     88        public Room getRoomAt(int x, int y) 
     89        { 
     90            if (x > roomAmountHor || x < 0 || y > roomAmountVert || y < 0) return null; 
     91            return rooms[x, y]; 
     92        } 
     93 
     94        public Room getRoomFromDirection(Room source, int dir) 
     95        { 
     96            int roomX = (int)source.RoomsPosOnGrid.X; 
     97            int roomY = (int)source.RoomsPosOnGrid.Y; 
     98 
     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; 
     109        } 
     110 
     111        public Room getRandomRoom() 
     112        { 
     113            if (roomList.Count == 0) throw new InvalidOperationException("The List is empty!"); 
     114            return roomList[RandomGen.NextInt(roomList.Count)]; 
     115        } 
     116 
     117        public Vector CenterRoom 
     118        { 
     119            get 
     120            { 
     121                return new Vector(roomAmountHor / 2, roomAmountVert / 2); 
     122            } 
    29123        } 
    30124    } 
  • 2012/27/DenisZ/TheDungeonGame/TheDungeonGame/TheDungeonGame/MathHelper.cs

    r3462 r3489  
    1919        } 
    2020 
     21        public static Vector mul(Vector vec1, Vector vec2) 
     22        { 
     23            return new Vector(vec1.X * vec2.X, vec1.Y * vec2.Y); 
     24        } 
     25 
    2126    } 
    2227} 
  • 2012/27/DenisZ/TheDungeonGame/TheDungeonGame/TheDungeonGame/Room.cs

    r3477 r3489  
    2020        protected int bWidth, bHeight; 
    2121        public Game Game { get; set; } 
     22        public Vector RoomsPosOnGrid { get; set; } 
    2223 
    2324        public Room(Game game, Vector pos, Vector size, double thickness) 
    2425        { 
    2526            Game = game; 
    26             Position = pos; 
     27            Position = VecMath.sub(pos, new Vector(Game.Level.Width / 2, -(Game.Level.Height / 2))); 
    2728            width = size.X; 
    2829            height = size.Y; 
     
    5354            foreach (IPhysicsObject obj in insideObjects) 
    5455            { 
    55                 if(obj != null) 
    56                 Game.Add(obj); 
     56                if (obj != null) 
     57                    Game.Add(obj); 
    5758            } 
    58              
     59 
    5960 
    6061            isBuilt = true; 
     
    7475        public void addBlock(IPhysicsObject obj, int bx, int by) 
    7576        { 
    76             if (bx > bWidth || bx < 0 || by > bHeight || by < 0 || insideObjects[bx,by] != null) throw new Exception("Can't put block to " + bx + " , " + by); 
     77            if (bx > bWidth || bx < 0 || by > bHeight || by < 0 || insideObjects[bx, by] != null) throw new Exception("Can't put block to " + bx + " , " + by); 
    7778            obj.Left = insidePos.X + (bx * blockWidth); 
    7879            obj.Top = insidePos.Y + (by * blockHeight); 
     
    8586            PhysicsObject wallLeft = createWall(VecMath.sub(Position, new Vector(0, thickness)), thickness, height); 
    8687            PhysicsObject wallDown = createWall(VecMath.sub(Position, new Vector(0, height)), width + thickness, thickness); 
    87             PhysicsObject wallRight = createWall(VecMath.add(Position, new Vector(width, -thickness)), thickness, height-thickness); 
     88            wallDown.Color = Color.Black; 
     89            PhysicsObject wallRight = createWall(VecMath.add(Position, new Vector(width, -thickness)), thickness, height - thickness); 
    8890            roomObjects.Add(wallTop); 
    8991            roomObjects.Add(wallLeft); 
     
    107109            bg.Top = Position.Y - thickness; 
    108110            bg.IgnoresCollisionResponse = true; 
    109             bg.Color = Color.Black; 
     111            bg.Color = Color.Green; 
    110112            bg.IgnoresExplosions = true; 
    111113            return bg; 
     
    114116    } 
    115117 
    116     class RoomTemplates 
     118    class RoomDirection 
    117119    { 
     120        public const int North = 0; 
     121        public const int East = 1; 
     122        public const int South = 2; 
     123        public const int West = 3; 
    118124 
     125        public static Vector getOffsetFromDir(int dir) 
     126        { 
     127            if (dir == North) 
     128                return new Vector(0, -1); 
     129            if (dir == East) 
     130                return new Vector(1, 0); 
     131            if (dir == South) 
     132                return new Vector(0, 1); 
     133            if (dir == West) 
     134                return new Vector(-1, 0); 
     135 
     136            return Vector.Zero; 
     137        } 
    119138    } 
    120139} 
  • 2012/27/DenisZ/TheDungeonGame/TheDungeonGame/TheDungeonGame/TestRoom.cs

    r3477 r3489  
    2121            createBorders(); 
    2222            roomObjects.Add(createBackground()); 
    23             addBlock(new PhysicsObject(blockWidth, blockHeight), 0, 0); 
     23          //  addBlock(new PhysicsObject(blockWidth, blockHeight), 0, 0); 
    2424        } 
    2525    } 
  • 2012/27/DenisZ/TheDungeonGame/TheDungeonGame/TheDungeonGame/TheDungeonGame.cs

    r3477 r3489  
    1414    public const double ROOMWIDTH = 850; 
    1515    public const double ROOMHEIGHT = 500; 
     16    public const double ROOMTHICKNESS = 70; 
     17    public static Vector roomSize = new Vector(ROOMWIDTH, ROOMHEIGHT); 
    1618    private Player player; 
    1719 
     
    2022        IsMouseVisible = true; 
    2123        SetWindowSize(800, 600); 
    22         Level.Width = 100000; 
    23         Level.Height = 100000; 
    24         Level.CreateBorders(); 
    25         Camera.Zoom(0.8); 
     24        Level.Width = ROOMWIDTH * 20; 
     25        Level.Height = ROOMHEIGHT * 20; 
     26     //   Level.CreateBorders(); 
     27        Camera.ZoomToLevel(); 
    2628 
    27         TestRoom testroom = new TestRoom(this,new Vector(-460, 300), new Vector(ROOMWIDTH, ROOMHEIGHT), 70); 
    28         testroom.buildLevel(); 
     29        LevelGenerator generator = new LevelGenerator(this, Level.Size, new Vector(20, 20)); 
     30        generator.generateRandomLevel(5, 10); 
     31        generator.buildLevel(); 
    2932 
    3033        player = new Player(Vector.Zero); 
Note: See TracChangeset for help on using the changeset viewer.