1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using System.Linq; |
---|
4 | using System.Text; |
---|
5 | using Jypeli; |
---|
6 | using Jypeli.Controls; |
---|
7 | using MathHelper; |
---|
8 | using Rooms; |
---|
9 | using Jypeli.Assets; |
---|
10 | using Items; |
---|
11 | |
---|
12 | namespace 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 = 250; |
---|
23 | private TheDungeonGame game; |
---|
24 | public Room currentRoom; |
---|
25 | public IntMeter playerHealth = new IntMeter(5, 0, 30); |
---|
26 | private Item itemInInventory; |
---|
27 | private AssaultRifle weapon; |
---|
28 | |
---|
29 | private Angle UpAngle = Angle.FromDegrees(90); |
---|
30 | private Angle RightAngle = Angle.Zero; |
---|
31 | private Angle DownAngle = Angle.FromDegrees(270); |
---|
32 | private Angle LeftAngle = Angle.FromDegrees(180); |
---|
33 | |
---|
34 | public Player(TheDungeonGame game, Vector pos, Image texture) |
---|
35 | : base(playerWidth, playerHeight) |
---|
36 | { |
---|
37 | this.game = game; |
---|
38 | keyboard = game.Keyboard; |
---|
39 | CanRotate = false; |
---|
40 | setupKeys(); |
---|
41 | Image = texture; |
---|
42 | itemInInventory = new TestItem(TheDungeonGame.LoadImage("items/testItem"), "Test Item", false); |
---|
43 | Tag = "Player"; |
---|
44 | |
---|
45 | weapon = new AssaultRifle(playerWidth / 3, playerHeight / 3); |
---|
46 | weapon.Top = Top; |
---|
47 | weapon.Image = null; |
---|
48 | weapon.Color = Color.Transparent; |
---|
49 | weapon.CanHitOwner = false; |
---|
50 | weapon.InfiniteAmmo = true; |
---|
51 | weapon.MaxAmmoLifetime = TimeSpan.FromSeconds(0.5); |
---|
52 | weapon.TimeBetweenUse = TimeSpan.FromSeconds(0.4); |
---|
53 | weapon.ProjectileCollision = bulletCollided; |
---|
54 | Add(weapon); |
---|
55 | } |
---|
56 | |
---|
57 | private void setupKeys() |
---|
58 | { |
---|
59 | keyboard.Listen(Key.W, ButtonState.Down, move, "Move Up", new Vector(0, speed)); |
---|
60 | keyboard.Listen(Key.A, ButtonState.Down, move, "Move Left", new Vector(-speed, 0)); |
---|
61 | keyboard.Listen(Key.S, ButtonState.Down, move, "Move Down", new Vector(0, -speed)); |
---|
62 | keyboard.Listen(Key.D, ButtonState.Down, move, "Move Right", new Vector(speed, 0)); |
---|
63 | |
---|
64 | keyboard.Listen(Key.W, ButtonState.Released, removeMovement, null, new Vector(0, speed)); |
---|
65 | keyboard.Listen(Key.A, ButtonState.Released, removeMovement, null, new Vector(-speed, 0)); |
---|
66 | keyboard.Listen(Key.S, ButtonState.Released, removeMovement, null, new Vector(0, -speed)); |
---|
67 | keyboard.Listen(Key.D, ButtonState.Released, removeMovement, null, new Vector(speed, 0)); |
---|
68 | |
---|
69 | keyboard.Listen(Key.Up, ButtonState.Down, shootAmmo, "Shoot up", UpAngle); |
---|
70 | keyboard.Listen(Key.Down, ButtonState.Down, shootAmmo, "Shoot up", DownAngle); |
---|
71 | keyboard.Listen(Key.Right, ButtonState.Down, shootAmmo, "Shoot up", RightAngle); |
---|
72 | keyboard.Listen(Key.Left, ButtonState.Down, shootAmmo, "Shoot up", LeftAngle); |
---|
73 | |
---|
74 | keyboard.Listen(Key.Space, ButtonState.Pressed, useItemInInventory, "Use Item"); |
---|
75 | } |
---|
76 | |
---|
77 | private void bulletCollided(PhysicsObject collidingObject, PhysicsObject targetObject) |
---|
78 | { |
---|
79 | |
---|
80 | if (targetObject.Tag.Equals("Enemy")) |
---|
81 | { |
---|
82 | EntityBase entity = (EntityBase)targetObject; |
---|
83 | entity.hit(WeaponDamage); |
---|
84 | collidingObject.Destroy(); |
---|
85 | currentRoom.EntityAmount--; |
---|
86 | } |
---|
87 | else if(targetObject.Tag.Equals("Wall") || targetObject.Tag.Equals("ObjectRock")) |
---|
88 | collidingObject.Destroy(); |
---|
89 | } |
---|
90 | |
---|
91 | public TimeSpan WeaponSpeed |
---|
92 | { |
---|
93 | get |
---|
94 | { |
---|
95 | return weapon.TimeBetweenUse; |
---|
96 | } |
---|
97 | set |
---|
98 | { |
---|
99 | weapon.TimeBetweenUse = value; |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | public TimeSpan BulletLife |
---|
104 | { |
---|
105 | get |
---|
106 | { |
---|
107 | return weapon.MaxAmmoLifetime; |
---|
108 | } |
---|
109 | set |
---|
110 | { |
---|
111 | weapon.MaxAmmoLifetime = value; |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | public int WeaponDamage |
---|
116 | { |
---|
117 | get; |
---|
118 | set; |
---|
119 | } |
---|
120 | |
---|
121 | private void shootAmmo(Angle angle) |
---|
122 | { |
---|
123 | weapon.Angle = angle; |
---|
124 | weapon.Shoot(); |
---|
125 | } |
---|
126 | |
---|
127 | private void useItemInInventory() |
---|
128 | { |
---|
129 | if (itemInInventory == null) return; |
---|
130 | itemInInventory.useItem(game); |
---|
131 | updateGuiImage(); |
---|
132 | } |
---|
133 | |
---|
134 | public Item ItemInInventory |
---|
135 | { |
---|
136 | get |
---|
137 | { |
---|
138 | return itemInInventory; |
---|
139 | } |
---|
140 | set |
---|
141 | { |
---|
142 | itemInInventory = value; |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | public void updateGuiImage() |
---|
147 | { |
---|
148 | game.gui.setInventoryImage(itemInInventory); |
---|
149 | } |
---|
150 | |
---|
151 | public void performCollision(PhysicsObject player, PhysicsObject target) |
---|
152 | { |
---|
153 | Room room; |
---|
154 | if (target.Tag.Equals(RoomDirection.North) || target.Tag.Equals(RoomDirection.East) || target.Tag.Equals(RoomDirection.South) || target.Tag.Equals(RoomDirection.West)) |
---|
155 | { |
---|
156 | ObjectDoor door = (ObjectDoor)target; |
---|
157 | if (door != null && door.IsLocked) return; |
---|
158 | } |
---|
159 | |
---|
160 | if (target.Tag.Equals(RoomDirection.North)) |
---|
161 | { |
---|
162 | room = game.LevelGen.getRoomAt(currentRoom.PosOnGrid + RoomDirection.getOffsetFromDir(RoomDirection.North)); |
---|
163 | game.moveToRoom(room, RoomDirection.North); |
---|
164 | currentRoom = room; |
---|
165 | } |
---|
166 | else |
---|
167 | if (target.Tag.Equals(RoomDirection.East)) |
---|
168 | { |
---|
169 | room = game.LevelGen.getRoomAt(currentRoom.PosOnGrid + RoomDirection.getOffsetFromDir(RoomDirection.East)); |
---|
170 | game.moveToRoom(room, RoomDirection.East); |
---|
171 | currentRoom = room; |
---|
172 | } |
---|
173 | else |
---|
174 | if (target.Tag.Equals(RoomDirection.South)) |
---|
175 | { |
---|
176 | room = game.LevelGen.getRoomAt(currentRoom.PosOnGrid + RoomDirection.getOffsetFromDir(RoomDirection.South)); |
---|
177 | game.moveToRoom(room, RoomDirection.South); |
---|
178 | currentRoom = room; |
---|
179 | } |
---|
180 | else |
---|
181 | if (target.Tag.Equals(RoomDirection.West)) |
---|
182 | { |
---|
183 | room = game.LevelGen.getRoomAt(currentRoom.PosOnGrid + RoomDirection.getOffsetFromDir(RoomDirection.West)); |
---|
184 | game.moveToRoom(room, RoomDirection.West); |
---|
185 | currentRoom = room; |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | public void handleItemPickup(IPhysicsObject player, IPhysicsObject item) |
---|
190 | { |
---|
191 | ItemEntity itemEnt = (ItemEntity)item; |
---|
192 | itemEnt.bindedItem.onPickup(this); |
---|
193 | item.Destroy(); |
---|
194 | } |
---|
195 | |
---|
196 | public override void Update(Time time) |
---|
197 | { |
---|
198 | if (!isMoving) |
---|
199 | { |
---|
200 | movementVector = Vector.Zero; |
---|
201 | Stop(); |
---|
202 | } |
---|
203 | |
---|
204 | isMoving = false; |
---|
205 | |
---|
206 | Console.WriteLine(currentRoom.EntityAmount); |
---|
207 | |
---|
208 | if (currentRoom.EntityAmount == 0) |
---|
209 | { |
---|
210 | foreach (ObjectDoor door in currentRoom.doors) |
---|
211 | { |
---|
212 | if (door != null) |
---|
213 | door.IsLocked = false; |
---|
214 | } |
---|
215 | } |
---|
216 | else |
---|
217 | { |
---|
218 | foreach (ObjectDoor door in currentRoom.doors) |
---|
219 | { |
---|
220 | if (door != null) |
---|
221 | door.IsLocked = true; |
---|
222 | } |
---|
223 | } |
---|
224 | |
---|
225 | |
---|
226 | base.Update(time); |
---|
227 | } |
---|
228 | |
---|
229 | public void move(Vector vec) |
---|
230 | { |
---|
231 | if (vec.X != 0) |
---|
232 | movementVector.X = vec.X; |
---|
233 | if (vec.Y != 0) |
---|
234 | movementVector.Y = vec.Y; |
---|
235 | |
---|
236 | Move(movementVector); |
---|
237 | isMoving = true; |
---|
238 | } |
---|
239 | |
---|
240 | public void removeMovement(Vector vec) |
---|
241 | { |
---|
242 | movementVector = VecMath.sub(movementVector, vec); |
---|
243 | } |
---|
244 | } |
---|
245 | } |
---|