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

Revision 3649, 12.0 KB checked in by dezhidki, 11 years ago (diff)

Talletus.

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