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

Revision 3538, 4.3 KB checked in by dezhidki, 8 years ago (diff)

Updated camera motion

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