source: 2012/27/DenisZ/TheDungeonGame/TheDungeonGame/TheDungeonGame/TheDungeonGame.cs @ 3572

Revision 3572, 5.8 KB checked in by dezhidki, 9 years ago (diff)

Levels are now dissapearing, if not on the screen
Possible to spawn random levels

Line 
1using System;
2using System.Collections.Generic;
3using Jypeli;
4using Jypeli.Assets;
5using Jypeli.Controls;
6using Jypeli.Effects;
7using Jypeli.Widgets;
8using MathHelper;
9using Entity;
10using Rooms;
11using Gui;
12
13public class TheDungeonGame : PhysicsGame
14{
15    public const double ROOMTHICKNESS = 60;
16    public const double ROOMWIDTH = 800 - ROOMTHICKNESS;
17    public const double ROOMHEIGHT = 450;
18
19    public static Vector roomSize = new Vector(ROOMWIDTH, ROOMHEIGHT);
20    private Player player;
21    public Player Player { get { return player; } }
22    private LevelGenerator generator;
23    public LevelGenerator LevelGen { get { return generator; } }
24
25    private Vector cameraPosMission, cameraVelocity, newPos, oldPos;
26    private Room oldRoom;
27    private bool moveCamera = false;
28
29    public Image playerPic = LoadImage("player");
30    public List<Image> levelTopBottoms = new List<Image>();
31    public List<Image> levelLeftRights = new List<Image>();
32    public List<Image> levelBackGrounds = new List<Image>();
33    public List<Image> objectTextures = new List<Image>();
34    public InGameGui gui;
35
36    public override void Begin()
37    {
38        IsMouseVisible = true;
39        SetWindowSize(800, 600);
40        Level.Width = ROOMWIDTH * 20;
41        Level.Height = ROOMHEIGHT * 20;
42        setupLevelTextures(1);
43        setupObjectTextures();     
44
45        generator = new LevelGenerator(this, Level.Size, new Vector(20, 20));
46        generator.generateRandomLevel(5, 10);
47        generator.initDungeon();
48        Room centerRoom = generator.getRoomAt((int)(generator.CenterRoom.X), (int)(generator.CenterRoom.Y));
49        Camera.Position = centerRoom.Position + new Vector(ROOMWIDTH / 2 + ROOMTHICKNESS / 2, -ROOMHEIGHT / 2 + ROOMTHICKNESS / 4);
50
51        player = new Player(this, Vector.Zero, playerPic);
52        setupPlayer();
53        AddCollisionHandler(player, player.performCollision);
54        AddCollisionHandler(player, "Item", player.handleItemPickup);
55
56
57        gui = new InGameGui(this);
58        Add(gui);
59
60        Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Lopeta peli");
61        Keyboard.Listen(Key.R, ButtonState.Pressed, regenerateLevel, null);
62    }
63
64    private void setupLevelTextures(int levels)
65    {
66        for (int i = 1; i <= levels; i++)
67        {
68            levelTopBottoms.Add(LoadImage("level"+i+"/topBottom"));
69            levelLeftRights.Add(LoadImage("level"+i+"/leftRight"));
70            levelBackGrounds.Add(LoadImage("level"+i+"/levelBackground"));
71        }
72    }
73
74    private void setupObjectTextures()
75    {
76        objectTextures.Add(LoadImage("objects/door_closed"));
77        //objectTextures.Add(LoadImage("objects/door_open"));
78    }
79
80    public Image getLeftRightWall(int level)
81    {
82        return levelLeftRights[level - 1];
83    }
84
85    public Image getTopBottomWall(int level)
86    {
87        return levelTopBottoms[level - 1];
88    }
89
90    public Image getLevelBackground(int level)
91    {
92        return levelBackGrounds[level - 1];
93    }
94
95    public void moveToRoom(Room room, int dir)
96    {
97        oldPos = player.currentRoom.Position + new Vector(ROOMWIDTH / 2 + ROOMTHICKNESS / 2, -ROOMHEIGHT / 2 + ROOMTHICKNESS / 4);
98        newPos = room.Position + new Vector(ROOMWIDTH / 2 + ROOMTHICKNESS / 2, -ROOMHEIGHT / 2 + ROOMTHICKNESS / 4);
99        oldRoom = player.currentRoom;
100        if (room.isBuilt) room.restoreLevel();
101        else room.buildLevel();
102        moveCameraTo(newPos - oldPos);
103        player.Position = player.currentRoom.getDoor(dir).Position + RoomDirection.getOffsetFromWorldDir(dir) * 130;
104    }
105
106    public void moveCameraTo(Vector pos)
107    {
108        cameraPosMission = pos;
109        cameraVelocity = pos * 1.5;
110        moveCamera = true;
111    }
112
113    private void moveCameraPart()
114    {
115        Camera.Velocity = cameraVelocity;
116        Vector remainingLenght = Camera.Position - newPos;
117        if (Math.Abs((int)remainingLenght.X) < 20 && Math.Abs((int)remainingLenght.Y) < 20)
118        {
119            cameraPosMission = Vector.Zero;
120            cameraVelocity = Vector.Zero;
121            Camera.Velocity = cameraVelocity;
122            Camera.Position = newPos;
123            moveCamera = false;
124            oldRoom.hideLevel();
125        }
126    }
127
128    private void setupPlayer()
129    {
130        player.Position = generator.getRoomAt(generator.CenterRoom).Position + new Vector(ROOMWIDTH / 2, -ROOMHEIGHT / 2);
131        player.currentRoom = generator.getRoomAt(generator.CenterRoom);
132        player.currentRoom.buildLevel();
133        Add(player, 2);
134    }
135
136    private void regenerateLevel()
137    {
138        generator.destroyDungeon();
139        generator = new LevelGenerator(this, Level.Size, new Vector(20, 20));
140        generator.generateRandomLevel(10, 15);
141        generator.initDungeon();
142      //  generator.buildDungeon();
143        player.currentRoom = generator.getRoomAt(generator.CenterRoom);
144        player.currentRoom.buildLevel();
145    }
146
147    int errorAmount = 0;
148    protected override void Update(Time time)
149    {
150        try  // Tapa pakottaa Updatea toimimaan LevelGeneratorin kanssa
151        {
152            base.Update(time);
153        }
154        catch (FormatException) // Kaatuu tähän melkein kokoajan kun yrittää luoda tasoa. Mun koodi liian "likainen", tai hidas?
155        {
156            if (errorAmount > 50) // Virheitä voi tulla aika monta samaan aikaan, joten tehdään raja, jolloin regeneroidaan leveli
157            {
158                regenerateLevel();
159                errorAmount = 0;
160            }
161            errorAmount++;
162            return; // Skipataan "virheelisiä" tickea ja huijataan peliä niin kuin pahat merimiehet (dirty pirates, Arr)
163        }
164        if (moveCamera)
165        {
166            moveCameraPart();
167        }
168
169        player.Update(time);
170    }
171}
Note: See TracBrowser for help on using the repository browser.