1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using System.Linq; |
---|
4 | using System.Text; |
---|
5 | using Jypeli; |
---|
6 | using MathHelper; |
---|
7 | |
---|
8 | namespace Rooms |
---|
9 | { |
---|
10 | abstract class Room |
---|
11 | { |
---|
12 | |
---|
13 | public Vector Position; |
---|
14 | public double width, height, thickness; |
---|
15 | protected List<IPhysicsObject> roomObjects = new List<IPhysicsObject>(); |
---|
16 | private bool isBuilt = false; |
---|
17 | protected IPhysicsObject[,] insideObjects; |
---|
18 | protected double blockWidth, blockHeight; |
---|
19 | protected double insideWidth, insideHeight; |
---|
20 | protected Vector insidePos; |
---|
21 | protected int bWidth, bHeight; |
---|
22 | public Game Game { get; set; } |
---|
23 | public Vector PosOnGrid { get; set; } |
---|
24 | |
---|
25 | public Room(Game game, Vector pos, Vector size, double thickness) |
---|
26 | { |
---|
27 | Game = game; |
---|
28 | Position = VecMath.sub(pos, new Vector(Game.Level.Width / 2, -(Game.Level.Height / 2))); |
---|
29 | width = size.X; |
---|
30 | height = size.Y; |
---|
31 | this.thickness = thickness; |
---|
32 | |
---|
33 | insidePos = VecMath.add(pos, new Vector(thickness, -thickness)); |
---|
34 | insideWidth = width - thickness; |
---|
35 | insideHeight = height - thickness; |
---|
36 | blockWidth = insideWidth / 10; |
---|
37 | blockHeight = blockWidth; |
---|
38 | bWidth = (int)(insideWidth / blockWidth); |
---|
39 | bHeight = (int)(insideHeight / blockHeight); |
---|
40 | |
---|
41 | insideObjects = new PhysicsObject[bWidth, bHeight]; |
---|
42 | } |
---|
43 | |
---|
44 | public abstract void initRoom(); |
---|
45 | |
---|
46 | public void buildLevel() |
---|
47 | { |
---|
48 | if (roomObjects.Count == 0) throw new Exception("Cannot build empty room!"); |
---|
49 | |
---|
50 | foreach (IPhysicsObject obj in roomObjects) |
---|
51 | { |
---|
52 | Game.Add(obj); |
---|
53 | } |
---|
54 | |
---|
55 | foreach (IPhysicsObject obj in insideObjects) |
---|
56 | { |
---|
57 | if (obj != null) |
---|
58 | Game.Add(obj); |
---|
59 | } |
---|
60 | |
---|
61 | |
---|
62 | isBuilt = true; |
---|
63 | } |
---|
64 | |
---|
65 | public void destroyLevel() |
---|
66 | { |
---|
67 | if (roomObjects.Count == 0) throw new Exception("Cannot destory empty room!"); |
---|
68 | if (!isBuilt) throw new Exception("Cannot destroy unbuilt room!"); |
---|
69 | |
---|
70 | foreach (IPhysicsObject obj in roomObjects) |
---|
71 | { |
---|
72 | obj.Destroy(); |
---|
73 | } |
---|
74 | foreach (IPhysicsObject obj in insideObjects) |
---|
75 | { |
---|
76 | if (obj != null) |
---|
77 | obj.Destroy(); |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | public void addBlock(IPhysicsObject obj, int bx, int by) |
---|
82 | { |
---|
83 | if (bx > bWidth || bx < 0 || by > bHeight || by < 0 || insideObjects[bx, by] != null) throw new Exception("Can't put block to " + bx + " , " + by); |
---|
84 | obj.Left = insidePos.X + (bx * blockWidth); |
---|
85 | obj.Top = insidePos.Y + (by * blockHeight); |
---|
86 | insideObjects[bx, by] = obj; |
---|
87 | } |
---|
88 | |
---|
89 | public void createBorders() |
---|
90 | { |
---|
91 | PhysicsObject wallTop = createWall(Position, width + thickness, thickness); |
---|
92 | PhysicsObject wallLeft = createWall(VecMath.sub(Position, new Vector(0, thickness)), thickness, height); |
---|
93 | PhysicsObject wallDown = createWall(VecMath.sub(Position, new Vector(0, height)), width + thickness, thickness); |
---|
94 | wallDown.Color = Color.Black; |
---|
95 | PhysicsObject wallRight = createWall(VecMath.add(Position, new Vector(width, -thickness)), thickness, height - thickness); |
---|
96 | roomObjects.Add(wallTop); |
---|
97 | roomObjects.Add(wallLeft); |
---|
98 | roomObjects.Add(wallDown); |
---|
99 | roomObjects.Add(wallRight); |
---|
100 | } |
---|
101 | |
---|
102 | public PhysicsObject createWall(Vector pos, double width, double height) |
---|
103 | { |
---|
104 | PhysicsObject wall = PhysicsObject.CreateStaticObject(width, height); |
---|
105 | wall.Left = pos.X; |
---|
106 | wall.Top = pos.Y; |
---|
107 | |
---|
108 | return wall; |
---|
109 | } |
---|
110 | |
---|
111 | public PhysicsObject createBackground() |
---|
112 | { |
---|
113 | PhysicsObject bg = PhysicsObject.CreateStaticObject(insideWidth, insideHeight); |
---|
114 | bg.Left = Position.X + thickness; |
---|
115 | bg.Top = Position.Y - thickness; |
---|
116 | bg.IgnoresCollisionResponse = true; |
---|
117 | bg.Color = Color.Green; |
---|
118 | bg.IgnoresExplosions = true; |
---|
119 | return bg; |
---|
120 | } |
---|
121 | |
---|
122 | } |
---|
123 | |
---|
124 | class RoomDirection |
---|
125 | { |
---|
126 | public const int North = 0; |
---|
127 | public const int East = 1; |
---|
128 | public const int South = 2; |
---|
129 | public const int West = 3; |
---|
130 | |
---|
131 | public static Vector getOffsetFromDir(int dir) |
---|
132 | { |
---|
133 | if (dir == North) |
---|
134 | return new Vector(0, -1); |
---|
135 | if (dir == East) |
---|
136 | return new Vector(1, 0); |
---|
137 | if (dir == South) |
---|
138 | return new Vector(0, 1); |
---|
139 | if (dir == West) |
---|
140 | return new Vector(-1, 0); |
---|
141 | |
---|
142 | return Vector.Zero; |
---|
143 | } |
---|
144 | } |
---|
145 | } |
---|