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

Revision 3563, 5.6 KB checked in by dezhidki, 9 years ago (diff)

Added some textures
Added basic ingame Gui
Added ability to hold and use items
Camera movement is now totaly fixed and working properly
Added ability to easily add and manage textures
Minor fixes on some stuff

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