1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using System.Linq; |
---|
4 | using System.Text; |
---|
5 | using Jypeli; |
---|
6 | using MathHelper; |
---|
7 | using Entity; |
---|
8 | |
---|
9 | namespace Rooms |
---|
10 | { |
---|
11 | public abstract class Room |
---|
12 | { |
---|
13 | |
---|
14 | public Vector Position; |
---|
15 | public double width, height, thickness; |
---|
16 | protected List<IPhysicsObject> roomDecorations = new List<IPhysicsObject>(); |
---|
17 | protected List<IPhysicsObject> roomObjects = new List<IPhysicsObject>(); |
---|
18 | private bool isBuilt = false; |
---|
19 | protected IPhysicsObject[,] insideObjects; |
---|
20 | protected PhysicsObject[] doors = new PhysicsObject[4]; |
---|
21 | protected double blockWidth, blockHeight; |
---|
22 | protected double insideWidth, insideHeight; |
---|
23 | protected Vector insidePos; |
---|
24 | protected int bWidth, bHeight; |
---|
25 | public TheDungeonGame Game { get; set; } |
---|
26 | public Vector PosOnGrid { get; set; } |
---|
27 | public PhysicsObject wall; |
---|
28 | public int EntityAmount { get; set; } |
---|
29 | |
---|
30 | public Room(TheDungeonGame game, Vector pos, Vector size, double thickness) |
---|
31 | { |
---|
32 | Game = game; |
---|
33 | Position = VecMath.sub(pos, new Vector(Game.Level.Width / 2, -(Game.Level.Height / 2))); |
---|
34 | width = size.X; |
---|
35 | height = size.Y; |
---|
36 | this.thickness = thickness; |
---|
37 | |
---|
38 | insidePos = VecMath.add(Position, new Vector(thickness, -thickness)); |
---|
39 | insideWidth = width - thickness; |
---|
40 | insideHeight = height - thickness; |
---|
41 | blockWidth = insideWidth / 10; |
---|
42 | blockHeight = blockWidth; |
---|
43 | bWidth = (int)(insideWidth / blockWidth); |
---|
44 | bHeight = (int)(insideHeight / blockHeight); |
---|
45 | |
---|
46 | insideObjects = new PhysicsObject[bWidth, bHeight]; |
---|
47 | } |
---|
48 | |
---|
49 | public abstract void initRoom(); |
---|
50 | |
---|
51 | public void buildLevel() |
---|
52 | { |
---|
53 | if (roomDecorations.Count == 0 && roomObjects.Count == 0) throw new Exception("Cannot build empty room!"); |
---|
54 | |
---|
55 | foreach (IPhysicsObject obj in roomDecorations) |
---|
56 | { |
---|
57 | Game.Add(obj); |
---|
58 | } |
---|
59 | |
---|
60 | foreach (IPhysicsObject obj in roomObjects) |
---|
61 | { |
---|
62 | Game.Add(obj, 1); |
---|
63 | } |
---|
64 | |
---|
65 | foreach (IPhysicsObject obj in insideObjects) |
---|
66 | { |
---|
67 | if (obj != null) |
---|
68 | Game.Add(obj, 1); |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | isBuilt = true; |
---|
73 | } |
---|
74 | |
---|
75 | public void destroyLevel() |
---|
76 | { |
---|
77 | if (roomDecorations.Count == 0) throw new Exception("Cannot destory empty room!"); |
---|
78 | if (!isBuilt) throw new Exception("Cannot destroy unbuilt room!"); |
---|
79 | |
---|
80 | foreach (IPhysicsObject obj in roomDecorations) |
---|
81 | { |
---|
82 | obj.Destroy(); |
---|
83 | } |
---|
84 | foreach (IPhysicsObject obj in roomObjects) |
---|
85 | { |
---|
86 | obj.Destroy(); |
---|
87 | } |
---|
88 | foreach (IPhysicsObject obj in insideObjects) |
---|
89 | { |
---|
90 | if (obj != null) |
---|
91 | obj.Destroy(); |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | public void addBlock(IPhysicsObject obj, int bx, int by) |
---|
96 | { |
---|
97 | if (bx >= bWidth || bx < 0 || by >= bHeight || by < 0 || insideObjects[bx, by] != null) throw new Exception("Can't put block to " + bx + " , " + by); |
---|
98 | obj.Left = insidePos.X + (bx * blockWidth); |
---|
99 | obj.Top = insidePos.Y - (by * blockHeight); |
---|
100 | insideObjects[bx, by] = obj; |
---|
101 | } |
---|
102 | |
---|
103 | public void addBlockAt(IPhysicsObject obj, double x, double y) |
---|
104 | { |
---|
105 | obj.Left = Position.X + x; |
---|
106 | obj.Top = Position.Y + y; |
---|
107 | roomObjects.Add(obj); |
---|
108 | } |
---|
109 | |
---|
110 | public void addEntityAt(EntityBase ent, int bx, int by) |
---|
111 | { |
---|
112 | if (bx >= bWidth || bx < 0 || by >= bHeight || by < 0 || insideObjects[bx, by] != null) throw new Exception("Can't put block to " + bx + " , " + by); |
---|
113 | EntityAmount++; |
---|
114 | ent.Left = insidePos.X + (bx * blockWidth); |
---|
115 | ent.Top = insidePos.Y + (by * blockHeight); |
---|
116 | insideObjects[bx, by] = ent; |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | // En vitsinyt käyttää enempää aikaa tämän parantamiseksi. Helppo ja toimiva |
---|
121 | public void addDoors(Image doorTexture) |
---|
122 | { |
---|
123 | for (int i = 0; i < 4; i++) |
---|
124 | { |
---|
125 | Room room = Game.LevelGen.getRoomAt(PosOnGrid + RoomDirection.getOffsetFromDir(i)); |
---|
126 | |
---|
127 | if (room == null) continue; |
---|
128 | |
---|
129 | PhysicsObject door = null; |
---|
130 | |
---|
131 | if (i == RoomDirection.North) |
---|
132 | { |
---|
133 | door = PhysicsObject.CreateStaticObject(thickness, thickness); |
---|
134 | door.Position = Position + new Vector((width + thickness) / 2, -thickness / 2); |
---|
135 | door.Image = doorTexture; |
---|
136 | door.Tag = RoomDirection.North; |
---|
137 | } |
---|
138 | else |
---|
139 | if (i == RoomDirection.East) |
---|
140 | { |
---|
141 | door = PhysicsObject.CreateStaticObject(thickness, thickness); |
---|
142 | door.Position = Position + new Vector(width + thickness / 2, -(height + thickness) / 2); |
---|
143 | door.Image = doorTexture; |
---|
144 | door.Angle = Angle.FromDegrees(270); |
---|
145 | door.Tag = RoomDirection.East; |
---|
146 | } |
---|
147 | else |
---|
148 | if (i == RoomDirection.South) |
---|
149 | { |
---|
150 | door = PhysicsObject.CreateStaticObject(thickness, thickness); |
---|
151 | door.Position = Position + new Vector((thickness + width) / 2, -height - thickness / 2); |
---|
152 | door.Image = Image.Flip(doorTexture); |
---|
153 | door.Tag = RoomDirection.South; |
---|
154 | } |
---|
155 | else |
---|
156 | if (i == RoomDirection.West) |
---|
157 | { |
---|
158 | door = PhysicsObject.CreateStaticObject(thickness, thickness); |
---|
159 | door.Position = Position + new Vector(thickness / 2, -height / 2 - thickness / 2); |
---|
160 | door.Image = doorTexture; |
---|
161 | door.Angle = Angle.FromDegrees(90); |
---|
162 | door.Tag = RoomDirection.West; |
---|
163 | } |
---|
164 | if (door != null) |
---|
165 | { |
---|
166 | roomObjects.Add(door); |
---|
167 | doors[i] = door; |
---|
168 | } |
---|
169 | |
---|
170 | } |
---|
171 | } |
---|
172 | |
---|
173 | public PhysicsObject getDoor(int dir) |
---|
174 | { |
---|
175 | PhysicsObject result = null; |
---|
176 | for (int i = 0; i < doors.Length; i++ ) |
---|
177 | { |
---|
178 | PhysicsObject door = doors[i]; |
---|
179 | if (door != null && (int)door.Tag == dir) |
---|
180 | { |
---|
181 | result = door; |
---|
182 | return result; |
---|
183 | } |
---|
184 | |
---|
185 | } |
---|
186 | return result; |
---|
187 | } |
---|
188 | |
---|
189 | public void createBorders(Image topBottom, Image leftRight) |
---|
190 | { |
---|
191 | PhysicsObject wallTop = createWall(Position, width + thickness, thickness); |
---|
192 | wallTop.Color = Color.Blue; |
---|
193 | wallTop.Image = topBottom; |
---|
194 | PhysicsObject wallLeft = createWall(VecMath.sub(Position, new Vector(0, thickness)), thickness, height); |
---|
195 | wallLeft.Color = Color.Red; |
---|
196 | wallLeft.Image = leftRight; |
---|
197 | PhysicsObject wallDown = createWall(VecMath.sub(Position, new Vector(0, height)), width + thickness, thickness); |
---|
198 | wallDown.Color = Color.Black; |
---|
199 | if(topBottom != null) wallDown.Image = Image.Flip(topBottom); |
---|
200 | PhysicsObject wallRight = createWall(VecMath.add(Position, new Vector(width, -thickness)), thickness, height - thickness); |
---|
201 | wallRight.Color = Color.Orange; |
---|
202 | if(leftRight != null) wallRight.Image = Image.Mirror(leftRight); |
---|
203 | roomDecorations.Add(wallTop); |
---|
204 | roomDecorations.Add(wallLeft); |
---|
205 | roomDecorations.Add(wallDown); |
---|
206 | roomDecorations.Add(wallRight); |
---|
207 | } |
---|
208 | |
---|
209 | public PhysicsObject createWall(Vector pos, double width, double height) |
---|
210 | { |
---|
211 | wall = PhysicsObject.CreateStaticObject(width, height); |
---|
212 | wall.Left = pos.X; |
---|
213 | wall.Top = pos.Y; |
---|
214 | |
---|
215 | return wall; |
---|
216 | } |
---|
217 | |
---|
218 | public PhysicsObject createBackground(Image background) |
---|
219 | { |
---|
220 | PhysicsObject bg = PhysicsObject.CreateStaticObject(insideWidth, insideHeight); |
---|
221 | bg.Left = Position.X + thickness; |
---|
222 | bg.Top = Position.Y - thickness; |
---|
223 | bg.IgnoresCollisionResponse = true; |
---|
224 | bg.Image = background; |
---|
225 | bg.Color = Color.Green; |
---|
226 | bg.IgnoresExplosions = true; |
---|
227 | return bg; |
---|
228 | } |
---|
229 | } |
---|
230 | |
---|
231 | public class RoomDirection |
---|
232 | { |
---|
233 | public const int North = 0; |
---|
234 | public const int East = 1; |
---|
235 | public const int South = 2; |
---|
236 | public const int West = 3; |
---|
237 | |
---|
238 | public static Vector getOffsetFromDir(int dir) |
---|
239 | { |
---|
240 | if (dir == North) |
---|
241 | return new Vector(0, -1); |
---|
242 | if (dir == East) |
---|
243 | return new Vector(1, 0); |
---|
244 | if (dir == South) |
---|
245 | return new Vector(0, 1); |
---|
246 | if (dir == West) |
---|
247 | return new Vector(-1, 0); |
---|
248 | |
---|
249 | return Vector.Zero; |
---|
250 | } |
---|
251 | |
---|
252 | public static Vector getOffsetFromWorldDir(int dir) |
---|
253 | { |
---|
254 | if (dir == North) |
---|
255 | return new Vector(0, 1); |
---|
256 | if (dir == East) |
---|
257 | return new Vector(1, 0); |
---|
258 | if (dir == South) |
---|
259 | return new Vector(0, -1); |
---|
260 | if (dir == West) |
---|
261 | return new Vector(-1, 0); |
---|
262 | |
---|
263 | return Vector.Zero; |
---|
264 | } |
---|
265 | } |
---|
266 | } |
---|