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

Revision 3624, 10.4 KB checked in by dezhidki, 11 years ago (diff)

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