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

Revision 3562, 4.1 KB checked in by dezhidki, 10 years ago (diff)

Made player really go through the door, nor just spawn in the middle of the room when going through door

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;
11
12public class TheDungeonGame : PhysicsGame
13{
14    public const double ROOMTHICKNESS = 60;
15    public const double ROOMWIDTH = 800 - ROOMTHICKNESS;
16    public const double ROOMHEIGHT = 450;
17
18    public static Vector roomSize = new Vector(ROOMWIDTH, ROOMHEIGHT);
19    private Player player;
20    private LevelGenerator generator;
21    public LevelGenerator LevelGen { get { return generator; } }
22
23    private Vector cameraPosMission, cameraVelocity, newPos, currentPos;
24    private bool moveCamera = false;
25
26    public override void Begin()
27    {
28        IsMouseVisible = true;
29        SetWindowSize(800, 600);
30        Level.Width = ROOMWIDTH * 20;
31        Level.Height = ROOMHEIGHT * 20;
32
33        generator = new LevelGenerator(this, Level.Size, new Vector(20, 20));
34        generator.generateRandomLevel(5, 10);
35        generator.buildDungeon();
36        Room centerRoom = generator.getRoomAt((int)(generator.CenterRoom.X), (int)(generator.CenterRoom.Y));
37        Camera.Position = centerRoom.Position + new Vector(ROOMWIDTH / 2 + ROOMTHICKNESS / 2, -ROOMHEIGHT / 2 + ROOMTHICKNESS / 4);
38
39        player = new Player(this, Vector.Zero);
40        setupPlayer();
41        AddCollisionHandler(player, player.performCollision);
42
43        Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Lopeta peli");
44        Keyboard.Listen(Key.R, ButtonState.Pressed, regenerateLevel, null);
45    }
46
47    public void moveToRoom(Room room, int dir)
48    {
49        currentPos = player.currentRoom.Position + new Vector(ROOMWIDTH / 2 + ROOMTHICKNESS / 2, -ROOMHEIGHT / 2 + ROOMTHICKNESS / 4);
50        newPos = room.Position + new Vector(ROOMWIDTH / 2 + ROOMTHICKNESS / 2, -ROOMHEIGHT / 2 + ROOMTHICKNESS / 4);
51        moveCameraTo(newPos - currentPos);
52        player.Position = player.currentRoom.getDoor(dir).Position + RoomDirection.getOffsetFromWorldDir(dir) * 130;
53    }
54
55    public void moveCameraTo(Vector pos)
56    {
57        cameraPosMission = pos;
58        cameraVelocity = pos * 1.5;
59        moveCamera = true;
60    }
61
62    private void moveCameraPart()
63    {
64        Camera.Velocity = cameraVelocity;
65        Vector remainingLenght = Camera.Position - newPos;
66        if (Math.Abs((int)remainingLenght.X) < 20 && Math.Abs((int)remainingLenght.Y) < 20)
67        {
68            cameraPosMission = Vector.Zero;
69            cameraVelocity = Vector.Zero;
70            Camera.Velocity = cameraVelocity;
71            Camera.Position = newPos;
72            moveCamera = false;
73        }
74    }
75
76    private void setupPlayer()
77    {
78        player.Position = generator.getRoomAt(generator.CenterRoom).Position + new Vector(ROOMWIDTH / 2, -ROOMHEIGHT / 2);
79        player.currentRoom = generator.getRoomAt(generator.CenterRoom);
80        Add(player, 1);
81    }
82
83    private void regenerateLevel()
84    {
85        generator.destroyDungeon();
86        generator = new LevelGenerator(this, Level.Size, new Vector(20, 20));
87        generator.generateRandomLevel(10, 15);
88        generator.buildDungeon();
89        player.currentRoom = generator.getRoomAt(generator.CenterRoom);
90    }
91
92    int errorAmount = 0;
93    protected override void Update(Time time)
94    {
95        try  // Tapa pakottaa Updatea toimimaan LevelGeneratorin kanssa
96        {
97            base.Update(time);
98        }
99        catch (FormatException) // Kaatuu tähän melkein kokoajan kun yrittää luoda tasoa. Mun koodi liian "likainen", tai hidas?
100        {
101            if (errorAmount > 50) // Virheitä voi tulla aika monta samaan aikaan, joten tehdään raja, jolloin regeneroidaan leveli
102            {
103                regenerateLevel();
104                errorAmount = 0;
105            }
106            errorAmount++;
107            return; // Skipataan "virheelisiä" tickea ja huijataan peliä niin kuin pahat merimiehet (dirty pirates, Arr)
108        }
109        if (moveCamera)
110        {
111            moveCameraPart();
112        }
113
114        player.Update(time);
115    }
116}
Note: See TracBrowser for help on using the repository browser.