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

Revision 3446, 2.2 KB checked in by dezhidki, 9 years ago (diff)

Talletus.

Line 
1using System;
2using System.Collections.Generic;
3using Jypeli;
4using Jypeli.Assets;
5using Jypeli.Controls;
6using Jypeli.Effects;
7using Jypeli.Widgets;
8using MathHelper;
9
10public class TheDungeonGame : PhysicsGame
11{
12    private PhysicsObject player;
13    private bool isMoving = false;
14    private Vector movementVector = Vector.Zero;
15    private Vector moveUp = new Vector(0, 200);
16    private Vector moveDown = new Vector(0, -200);
17    private Vector moveRight = new Vector(200, 0);
18    private Vector moveLeft = new Vector(-200, 0);
19
20    public override void Begin()
21    {
22        IsMouseVisible = true;
23        SetWindowSize(800, 600);
24        Level.CreateBorders();
25        Camera.ZoomToLevel();
26
27        player = new PhysicsObject(75, 75);
28        Add(player);
29
30
31
32
33        Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Lopeta peli");
34        Keyboard.Listen(Key.W, ButtonState.Down, moveObject, "Move Up", player, moveUp);
35        Keyboard.Listen(Key.A, ButtonState.Down, moveObject, "Move Right", player, moveRight);
36        Keyboard.Listen(Key.S, ButtonState.Down, moveObject, "Move Down", player, moveDown);
37        Keyboard.Listen(Key.D, ButtonState.Down, moveObject, "Move Left", player, moveLeft);
38
39        Keyboard.Listen(Key.W, ButtonState.Released, removeMovement, null, moveUp);
40        Keyboard.Listen(Key.A, ButtonState.Released, removeMovement, null, moveRight);
41        Keyboard.Listen(Key.S, ButtonState.Released, removeMovement, null, moveDown);
42        Keyboard.Listen(Key.D, ButtonState.Released, removeMovement, null, moveLeft);
43    }
44
45    protected override void Update(Time time)
46    {
47        if (!isMoving)
48        {
49            movementVector = Vector.Zero;
50            player.Stop();
51        }
52        isMoving = false;
53        base.Update(time);
54    }
55
56    private void moveObject(IPhysicsObject obj, Vector vec)
57    {
58        if (vec.X > 0 || vec.X < 0)
59            movementVector.X = vec.X;
60        if (vec.Y > 0 || vec.Y < 0)
61            movementVector.Y = vec.Y;
62        obj.Move(movementVector);
63        isMoving = true;
64    }
65
66    private void removeMovement(Vector vec)
67    {
68        movementVector = VecMath.sub(movementVector, vec);
69    }
70}
Note: See TracBrowser for help on using the repository browser.