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

Revision 3628, 10.6 KB checked in by dezhidki, 11 years ago (diff)

More sounds added

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;
11using Jypeli.Widgets;
12
13namespace Entity
14{
15    public class Player : PhysicsObject
16    {
17        public const double playerWidth = 120 / 3;
18        public const double playerHeight = 180 / 3;
19        // private PhysicsObject head;
20        public Vector movementVector = Vector.Zero;
21        public bool isMoving = false;
22        private Keyboard keyboard;
23        public double speed = 250;
24        private TheDungeonGame game;
25        public Room currentRoom;
26        public IntMeter playerHealth;
27        private Item itemInInventory;
28        private AssaultRifle weapon;
29
30        private Angle UpAngle = Angle.FromDegrees(90);
31        private Angle RightAngle = Angle.Zero;
32        private Angle DownAngle = Angle.FromDegrees(270);
33        private Angle LeftAngle = Angle.FromDegrees(180);
34
35        public bool isDead = false;
36
37        private Image[] walkingImages = new Image[2];
38        private Image idleTexture;
39        private Animation walkingAnimation;
40        private bool isAnimationRunning = false;
41
42        private SoundEffect hitSound = TheDungeonGame.LoadSoundEffect("sound/robot_hit");
43
44        public Player(TheDungeonGame game, Vector pos)
45            : base(playerWidth, playerHeight)
46        {
47            playerHealth = new IntMeter(10, 0, 50);
48            this.game = game;
49            keyboard = game.Keyboard;
50            CanRotate = false;
51            setupKeys();
52            idleTexture = TheDungeonGame.LoadImage("animations/player/idle");
53            Image = idleTexture;
54            for (int i = 0; i < walkingImages.Length; i++)
55            {
56                walkingImages[i] = TheDungeonGame.LoadImage("animations/player/walk" + (i + 1));
57            }
58            walkingAnimation = new Animation(walkingImages);
59            walkingAnimation.FPS = 10;
60            Tag = "Player";
61        }
62
63        public void setupWeapon()
64        {
65            weapon = new AssaultRifle(playerWidth / 3, playerHeight / 3);
66            weapon.Top = Top;
67            weapon.Image = null;
68            weapon.Color = Color.Transparent;
69            weapon.CanHitOwner = false;
70            weapon.InfiniteAmmo = true;
71            weapon.MaxAmmoLifetime = TimeSpan.FromSeconds(0.5);
72            weapon.TimeBetweenUse = TimeSpan.FromSeconds(0.4);
73            weapon.ProjectileCollision = bulletCollided;
74            WeaponDamage = 1;
75            Add(weapon);
76        }
77
78        public void resetValues()
79        {
80            playerHealth = new IntMeter(10, 0, 50);
81            WeaponDamage = 1;
82            WeaponSpeed = TimeSpan.FromSeconds(0.4);
83            BulletLife = TimeSpan.FromSeconds(0.5);
84            isDead = false;
85            Animation = null;
86            isAnimationRunning = false;
87            Image = idleTexture;
88        }
89
90        private void setupKeys()
91        {
92            keyboard.Listen(Key.W, ButtonState.Down, move, "Move Up", new Vector(0, speed));
93            keyboard.Listen(Key.A, ButtonState.Down, move, "Move Left", new Vector(-speed, 0));
94            keyboard.Listen(Key.S, ButtonState.Down, move, "Move Down", new Vector(0, -speed));
95            keyboard.Listen(Key.D, ButtonState.Down, move, "Move Right", new Vector(speed, 0));
96
97            keyboard.Listen(Key.W, ButtonState.Released, removeMovement, null, new Vector(0, speed));
98            keyboard.Listen(Key.A, ButtonState.Released, removeMovement, null, new Vector(-speed, 0));
99            keyboard.Listen(Key.S, ButtonState.Released, removeMovement, null, new Vector(0, -speed));
100            keyboard.Listen(Key.D, ButtonState.Released, removeMovement, null, new Vector(speed, 0));
101
102            keyboard.Listen(Key.Up, ButtonState.Down, shootAmmo, "Shoot up", UpAngle);
103            keyboard.Listen(Key.Down, ButtonState.Down, shootAmmo, "Shoot up", DownAngle);
104            keyboard.Listen(Key.Right, ButtonState.Down, shootAmmo, "Shoot up", RightAngle);
105            keyboard.Listen(Key.Left, ButtonState.Down, shootAmmo, "Shoot up", LeftAngle);
106
107          //  keyboard.Listen(Key.Space, ButtonState.Pressed, useItemInInventory, "Use Item");
108        }
109
110        private void bulletCollided(PhysicsObject collidingObject, PhysicsObject targetObject)
111        {
112            collidingObject.IsVisible = true;
113            if (targetObject.Tag.Equals("Enemy"))
114            {
115                EntityBase entity = (EntityBase)targetObject;
116                if (entity.hit(WeaponDamage))
117                    currentRoom.EntityAmount--;
118                collidingObject.Destroy();
119            }
120            else if (targetObject.Tag.Equals("Wall") || targetObject.Tag.Equals("ObjectRock") || targetObject.Tag.Equals("Item")) 
121                collidingObject.Destroy();
122        }
123
124        public void hit(int damage)
125        {
126            playerHealth.Value -= damage;
127            hitSound.Play();
128            if (playerHealth.Value <= 0)
129            {
130                showLostScreen();
131            }         
132        }
133
134        public void showLostScreen()
135        {
136            if (isDead) return;
137            isDead = true;
138            string[] buttonNames = {"Retry", "Exit Game"};
139            keyboard.DisableAll();
140            MultiSelectWindow lostWindow = new MultiSelectWindow("Oh noes! You appear to be dead! \nBut, you can always start a new journey...", buttonNames);
141            lostWindow.AddItemHandler(0, game.restartGame);
142            lostWindow.AddItemHandler(1, game.Exit);
143            game.Add(lostWindow);
144        }
145
146        public TimeSpan WeaponSpeed
147        {
148            get
149            {
150                return weapon.TimeBetweenUse;
151            }
152            set
153            {
154                weapon.TimeBetweenUse = value;
155            }
156        }
157
158        public TimeSpan BulletLife
159        {
160            get
161            {
162                return weapon.MaxAmmoLifetime;
163            }
164            set
165            {
166                weapon.MaxAmmoLifetime = value;
167            }
168        }
169
170        public int WeaponDamage
171        {
172            get;
173            set;
174        }
175
176        private void shootAmmo(Angle angle)
177        {
178            weapon.Angle = angle;
179            PhysicsObject bullet = weapon.Shoot();
180            if (bullet != null)
181                bullet.IsVisible = true;
182        }
183
184        private void useItemInInventory()
185        {
186            if (itemInInventory == null) return;
187            itemInInventory.useItem(game);
188            updateGuiImage();
189        }
190
191        public Item ItemInInventory
192        {
193            get
194            {
195                return itemInInventory;
196            }
197            set
198            {
199                itemInInventory = value;
200            }
201        }
202
203        public void updateGuiImage()
204        {
205            game.gui.setInventoryImage(itemInInventory);
206        }
207
208        public void performCollision(PhysicsObject player, PhysicsObject target)
209        {
210            Room room;
211            if (target.Tag.Equals(RoomDirection.North) || target.Tag.Equals(RoomDirection.East) || target.Tag.Equals(RoomDirection.South) || target.Tag.Equals(RoomDirection.West))
212            {
213                ObjectDoor door = (ObjectDoor)target;
214                if (door != null && door.IsLocked) return;
215            }
216
217            if (target.Tag.Equals(RoomDirection.North))
218            {
219                room = game.LevelGen.getRoomAt(currentRoom.PosOnGrid + RoomDirection.getOffsetFromDir(RoomDirection.North));
220                game.moveToRoom(room, RoomDirection.North);
221                currentRoom = room;
222            }
223            else if (target.Tag.Equals(RoomDirection.East))
224            {
225                room = game.LevelGen.getRoomAt(currentRoom.PosOnGrid + RoomDirection.getOffsetFromDir(RoomDirection.East));
226                game.moveToRoom(room, RoomDirection.East);
227                currentRoom = room;
228            }
229            else if (target.Tag.Equals(RoomDirection.South))
230            {
231                room = game.LevelGen.getRoomAt(currentRoom.PosOnGrid + RoomDirection.getOffsetFromDir(RoomDirection.South));
232                game.moveToRoom(room, RoomDirection.South);
233                currentRoom = room;
234            }
235            else if (target.Tag.Equals(RoomDirection.West))
236            {
237                room = game.LevelGen.getRoomAt(currentRoom.PosOnGrid + RoomDirection.getOffsetFromDir(RoomDirection.West));
238                game.moveToRoom(room, RoomDirection.West);
239                currentRoom = room;
240            }
241        }
242
243        public void handleItemPickup(IPhysicsObject player, IPhysicsObject item)
244        {
245            ItemEntity itemEnt = (ItemEntity)item;
246            itemEnt.bindedItem.onPickup(this);
247            item.Destroy();
248        }
249
250        public override void Update(Time time)
251        {
252            if (!isMoving)
253            {
254                movementVector = Vector.Zero;
255                Stop();
256               
257            }
258
259            isMoving = false;
260
261            if (currentRoom.EntityAmount == 0)
262            {
263                for(int i = 0; i < 4; i++)
264                {
265                    if (currentRoom.doors[i] == null) continue;
266                    currentRoom.doors[i].IsLocked = false;
267                    currentRoom.setDoorTexture(game.objectTextures[1], i);
268                }
269            }
270            else
271            {
272                    for (int i = 0; i < 4; i++)
273                    {
274                        if (currentRoom.doors[i] == null) continue;
275                        currentRoom.doors[i].IsLocked = true;
276                        currentRoom.setDoorTexture(game.objectTextures[0], i);
277                    }
278            }
279
280
281            base.Update(time);
282        }
283
284        public void move(Vector vec)
285        {
286            if (vec.X != 0)
287                movementVector.X = vec.X;
288            if (vec.Y != 0)
289                movementVector.Y = vec.Y;
290            if (!isAnimationRunning)
291            {
292                Animation = walkingAnimation;
293                Animation.Start();
294                isAnimationRunning = true;
295            }         
296            Move(movementVector);
297            isMoving = true;
298        }
299
300        public void removeMovement(Vector vec)
301        {
302            movementVector = VecMath.sub(movementVector, vec);
303            Animation = null;
304            isAnimationRunning = false;
305            Image = idleTexture;
306        }
307    }
308}
Note: See TracBrowser for help on using the repository browser.