-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLandscape.cs
195 lines (172 loc) · 8.07 KB
/
Landscape.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpDX;
using SharpDX.Toolkit;
namespace Project1
{
using SharpDX.Toolkit.Graphics;
using SharpDX.Toolkit.Input;
using System.Diagnostics;
class Landscape : ColoredGameObject
{
//Set the speed of rotaion
private float speed = 0.006f;
//The default rotation of the Landscape
private float rotationX = -0.5f;
private float rotationY = 0.0f;
private float rotationZ = 0.0f;
private float baseline;
private float pix = 0.0625f; // 0.125f or 0.0625
float zooming = 4.0f;
public int level = 1;
public float landscapeWidth = 4f;
public float landscapeHeight = 4f;
public Vector3 corner1;
public Vector3 corner2;
public Vector3 corner3;
public Vector3 corner4;
//Here to set the Level, TODO create a new screen for level selecting
//public void setLevel(int level, float landscapeHeight, float landscapeWidth)
//{
// this.landscapeWidth = landscapeWidth;
// this.landscapeHeight = landscapeHeight;
// this.level = level;
//}
// an array of Vectors without color setting
private Vector3[] buffer;
// an array of Vectors with color setting
private VertexPositionColor[] colorBuffer;
BufferGenerator bufferGenerator = new BufferGenerator();
public Landscape(Project1Game game, int level)
{
this.level = level;
this.SetBuffer();
vertices = Buffer.Vertex.New(game.GraphicsDevice, colorBuffer);
setBoxBoundary();
basicEffect = new BasicEffect(game.GraphicsDevice)
{
View = game.camera.View,
Projection = game.camera.Projection,
World = Matrix.Identity,
VertexColorEnabled = true
};
inputLayout = VertexInputLayout.FromBuffer(0, vertices);
this.game = game;
}
public override void Update(GameTime gameTime)
{
// Listen to the keyboard to rotate the landscape
//if (game.keyboardState.IsKeyDown(Keys.Left))
//{
// rotationY -= speed;
// basicEffect.World = Matrix.RotationX(rotationX) * Matrix.RotationY(rotationY) * Matrix.RotationZ(rotationZ);
//}
//if (game.keyboardState.IsKeyDown(Keys.Right))
//{
// rotationY += speed;
// basicEffect.World = Matrix.RotationX(rotationX) * Matrix.RotationY(rotationY) * Matrix.RotationZ(rotationZ);
//}
//if (game.keyboardState.IsKeyDown(Keys.Up))
//{
// rotationX -= speed;
// basicEffect.World = Matrix.RotationX(rotationX) * Matrix.RotationY(rotationY) * Matrix.RotationZ(rotationZ);
//}
//if (game.keyboardState.IsKeyDown(Keys.Down))
//{
// rotationX += speed;
// basicEffect.World = Matrix.RotationX(rotationX) * Matrix.RotationY(rotationY) * Matrix.RotationZ(rotationZ);
//}
//if (game.keyboardState.IsKeyDown(Keys.W))
//{
// zooming = zooming + 0.1f;
//}
//if (game.keyboardState.IsKeyDown(Keys.S))
//{
// zooming = zooming - 0.1f;
// if (zooming < 2.0f)
// {
// zooming = 2.0f;
// }
//}
//basicEffect.World = Matrix.RotationX(0.1f) * Matrix.RotationY(0.2f) * Matrix.RotationZ(0);
// basicEffect.World = Matrix.RotationX(0.1f) * Matrix.RotationY(0.2f) * Matrix.RotationZ(0);
// basicEffect.Projection = Matrix.PerspectiveFovLH((float)Math.PI / zooming, (float)game.GraphicsDevice.BackBuffer.Width / game.GraphicsDevice.BackBuffer.Height, 0.1f, 100.0f);
}
public Boolean checkBoundary(Vector3 position)
{
if (position.Y > corner1.Y || position.Y > corner2.Y || position.Y > corner3.Y || position.Y > corner4.Y)
if(position.X >= corner1.X && position.X <= corner2.X && position.Z<=corner1.Z && position.Z>= corner4.Z)
return true;
return false;
}
private void setBoxBoundary()
{
//The position of 4 connors of the landscape
corner1 = new Vector3(0.0f - landscapeWidth / 2, 0.0f, 0.0f + landscapeWidth / 2);//look from the top, left up conor
corner2 = new Vector3(0.0f + landscapeWidth / 2, 0.0f, 0.0f + landscapeHeight / 2);//look from the top, right up conor
corner3 = new Vector3(0.0f + landscapeWidth / 2, 0.0f, 0.0f - landscapeHeight / 2);//look from the top, right down conor
corner4 = new Vector3(0.0f - landscapeWidth / 2, 0.0f, 0.0f - landscapeHeight / 2);//look from the top, left down conor
}
public override void Draw(GameTime gameTime)
{
// Setup the vertices
game.GraphicsDevice.SetVertexBuffer(vertices);
game.GraphicsDevice.SetVertexInputLayout(inputLayout);
// Apply the basic effect technique and draw the rotating cube
basicEffect.CurrentTechnique.Passes[0].Apply();
game.GraphicsDevice.Draw(PrimitiveType.TriangleList, vertices.ElementCount);
}
private void SetBuffer()
{
bufferGenerator.SetPix(pix);
//To generate a random float number
Random rd = new Random();
float c1, c2, c3, c4;
c1 = (float)rd.NextDouble(0, 1.0);
c2 = (float)rd.NextDouble(0, 1.0);
c3 = (float)rd.NextDouble(0, 1.0);
c4 = (float)rd.NextDouble(0, 1.0);
// to record the baseline of landscape's height
baseline = (c1 + c2 + c3 + c4) / 4;
bufferGenerator.SetBaseLine(baseline);
bufferGenerator.SetLandscapeHeight(this.landscapeHeight);
bufferGenerator.SetLandscapeWidth(this.landscapeWidth);
//The position of 4 connors of the landscape
Vector3 dpos1 = new Vector3(0.0f - landscapeWidth/2, c1, 0.0f + landscapeHeight/2);//look from the top, left up conor
Vector3 dpos2 = new Vector3(dpos1.X + landscapeWidth, c2, dpos1.Z);//look from the top, right up conor
Vector3 dpos3 = new Vector3(dpos1.X + landscapeWidth, c3, dpos1.Z - landscapeHeight);//look from the top, right down conor
Vector3 dpos4 = new Vector3(dpos1.X, c4, dpos1.Z - landscapeHeight);//look from the top, left down conor
float width = Math.Abs(dpos1.X - dpos2.X);
float height = Math.Abs(dpos1.Z - dpos3.Z);
this.colorBuffer = new VertexPositionColor[] { };
this.buffer = new Vector3[] { };
bufferGenerator.setLevel(level, landscapeHeight,landscapeWidth,dpos1);
this.buffer = bufferGenerator.DivideGrid(dpos1.X, dpos1.Z, width, height, c1, c2, c3, c4);
this.bufferGenerator.setColorRanges();
this.colorBuffer = bufferGenerator.setColor();
this.colorBuffer = bufferGenerator.setCannonPos();
this.colorBuffer = bufferGenerator.AddBase(dpos1, dpos2, dpos3, dpos4);
}
public float getHeight(float x, float z)
{
for (int i = 0; i < colorBuffer.Length-1; i++ )
{
if (colorBuffer[i].Position.X == x && colorBuffer[i].Position.Z == z)
{
return colorBuffer[i].Position.Y;
}
}
return 0f;
}
public VertexPositionColor getCannonPos()
{
return bufferGenerator.getCannonPos();
}
public VertexPositionColor getTargetPos()
{
return bufferGenerator.getTargetPos();
}
}
}