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

Revision 3563, 5.0 KB checked in by dezhidki, 11 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 System.Linq;
4using System.Text;
5using Jypeli;
6using Jypeli.Controls;
7using MathHelper;
8using Rooms;
9using Jypeli.Assets;
10using Items;
11
12namespace Entity
13{
14    public class Player : PhysicsObject
15    {
16        public const double playerWidth = 120/3;
17        public const double playerHeight = 180/3;
18       // private PhysicsObject head;
19        public Vector movementVector = Vector.Zero;
20        public bool isMoving = false;
21        private Keyboard keyboard;
22        public double speed = 200;
23        private TheDungeonGame game;
24        public Room currentRoom;
25        public IntMeter playerHealth = new IntMeter(5, 0, 30);
26        private Item itemInInventory;
27
28        public Player(TheDungeonGame game, Vector pos, Image texture)
29            : base(playerWidth, playerHeight)
30        {
31            this.game = game;
32            keyboard = game.Keyboard;
33            CanRotate = false;
34            setupKeys();
35            Image = texture;
36            itemInInventory = new TestItem(TheDungeonGame.LoadImage("items/testItem"), "Test Item", false);
37        }
38
39        private void setupKeys()
40        {
41            keyboard.Listen(Key.W, ButtonState.Down, move, "Move Up", new Vector(0, speed));
42            keyboard.Listen(Key.A, ButtonState.Down, move, "Move Left", new Vector(-speed, 0));
43            keyboard.Listen(Key.S, ButtonState.Down, move, "Move Down", new Vector(0, -speed));
44            keyboard.Listen(Key.D, ButtonState.Down, move, "Move Right", new Vector(speed, 0));
45
46            keyboard.Listen(Key.W, ButtonState.Released, removeMovement, null, new Vector(0, speed));
47            keyboard.Listen(Key.A, ButtonState.Released, removeMovement, null, new Vector(-speed, 0));
48            keyboard.Listen(Key.S, ButtonState.Released, removeMovement, null, new Vector(0, -speed));
49            keyboard.Listen(Key.D, ButtonState.Released, removeMovement, null, new Vector(speed, 0));
50            keyboard.Listen(Key.Space, ButtonState.Pressed, useItemInInventory, "Use Item");
51        }
52
53        private void useItemInInventory()
54        {
55            if (itemInInventory == null) return;
56            itemInInventory.useItem(game);
57            updateGuiImage();
58        }
59
60        public Item ItemInInventory
61        {
62            get
63            {
64                return itemInInventory;
65            }
66            set
67            {
68                itemInInventory = value;
69            }
70        }
71
72        public void updateGuiImage()
73        {
74            game.gui.setInventoryImage(itemInInventory);
75        }
76
77        public void performCollision(PhysicsObject player, PhysicsObject target)
78        {
79            Room room;
80            if (target.Tag.Equals(RoomDirection.North))
81            {
82                room = game.LevelGen.getRoomAt(currentRoom.PosOnGrid + RoomDirection.getOffsetFromDir(RoomDirection.North));
83                game.moveToRoom(room, RoomDirection.North);
84                currentRoom = room;
85            }else
86                if(target.Tag.Equals(RoomDirection.East))
87                {
88                    room = game.LevelGen.getRoomAt(currentRoom.PosOnGrid + RoomDirection.getOffsetFromDir(RoomDirection.East));
89                    game.moveToRoom(room, RoomDirection.East);
90                    currentRoom = room;
91                }
92                else
93                    if(target.Tag.Equals(RoomDirection.South))
94                    {
95                        room = game.LevelGen.getRoomAt(currentRoom.PosOnGrid + RoomDirection.getOffsetFromDir(RoomDirection.South));
96                        game.moveToRoom(room, RoomDirection.South);
97                        currentRoom = room;
98                    }
99                    else 
100                        if(target.Tag.Equals(RoomDirection.West))
101                        {
102                            room = game.LevelGen.getRoomAt(currentRoom.PosOnGrid + RoomDirection.getOffsetFromDir(RoomDirection.West));
103                            game.moveToRoom(room, RoomDirection.West);
104                            currentRoom = room;
105                        }
106        }
107
108        public void handleItemPickup(IPhysicsObject player, IPhysicsObject item)
109        {
110            ItemEntity itemEnt =  (ItemEntity)item;
111            itemEnt.bindedItem.onPickup(this);
112            item.Destroy();
113        }
114
115        public override void Update(Time time)
116        {
117            if (!isMoving)
118            {
119                movementVector = Vector.Zero;
120                Stop();
121            }
122
123            isMoving = false;
124
125            base.Update(time);
126        }
127
128        public void move(Vector vec)
129        {
130            if (vec.X != 0)
131                movementVector.X = vec.X;
132            if (vec.Y != 0)
133                movementVector.Y = vec.Y;
134
135            Move(movementVector);
136            isMoving = true;
137        }
138
139        public void removeMovement(Vector vec)
140        {
141            movementVector = VecMath.sub(movementVector, vec);
142        }
143    }
144}
Note: See TracBrowser for help on using the repository browser.