Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace UkladSloneczny
- {
- public class Game1 : Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- KeyboardState prevState;
- private Texture2D background;
- public BasicEffect basicEffect;
- public Camera camera;
- Floor floor;
- Cube earth, cubeMoon, mars, saturn, sun;
- private bool _grid = true;
- private bool _background = true;
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- Window.AllowUserResizing = false;
- graphics.PreferredBackBufferHeight = 720;
- graphics.PreferredBackBufferWidth = 1280;
- graphics.IsFullScreen = false;
- IsMouseVisible = false;
- }
- protected override void Initialize()
- {
- basicEffect = new BasicEffect(GraphicsDevice)
- {
- Alpha = 1,
- VertexColorEnabled = true,
- LightingEnabled = false,
- };
- camera = new Camera(this, new Vector3(0, 10, 0), Vector3.Zero, 5);
- Components.Add(camera);
- floor = new Floor(GraphicsDevice, 200, 200);
- sun = new Cube(this, new Vector3(0, 0, 0), new Vector3(0, 0, 0), new Vector3(5), new Vector3(0,0,3), GraphicsDevice, Color.Yellow, Color.OrangeRed);
- Components.Add(sun);
- earth = new Cube(this, new Vector3(0, 20, 0), new Vector3(0, 0, 0), new Vector3(3), new Vector3(0, 0, 2), GraphicsDevice,Color.Green,Color.Yellow);
- Components.Add(earth);
- cubeMoon = new Cube(this, new Vector3(0, 10, 0), new Vector3(0, 0, 0), new Vector3(1), new Vector3(0, 0, 15), GraphicsDevice);
- Components.Add(cubeMoon);
- mars = new Cube(this, new Vector3(0, 50, 0), new Vector3(0, 0, 0), new Vector3(2), new Vector3(0, 0, 3), GraphicsDevice, Color.Red, Color.DarkRed);
- Components.Add(mars);
- saturn = new Cube(this, new Vector3(0, 70, 0), new Vector3(0, 0, 0), new Vector3(3), new Vector3(0, 0, 2), GraphicsDevice, Color.Blue, Color.DarkRed);
- Components.Add(saturn);
- base.Initialize();
- }
- protected override void LoadContent()
- {
- spriteBatch = new SpriteBatch(GraphicsDevice);
- background = Content.Load<Texture2D>("stars");
- }
- protected override void UnloadContent() { }
- protected override void Update(GameTime gameTime)
- {
- var keyboardState = Keyboard.GetState();
- var mouseState = Mouse.GetState();
- var mousePostion = new Point(mouseState.X, mouseState.Y);
- if (keyboardState.IsKeyDown(Keys.Escape))
- {
- Exit();
- }
- if (keyboardState.IsKeyDown(Keys.LeftShift) )
- {
- camera.cameraSpeed = 25;
- }
- if (keyboardState.IsKeyDown(Keys.X) && prevState.IsKeyUp(Keys.X))
- {
- _grid = !_grid;
- }
- if (keyboardState.IsKeyDown(Keys.B) && prevState.IsKeyUp(Keys.B))
- {
- _background = !_background;
- }
- else if (prevState.IsKeyDown(Keys.LeftShift) && keyboardState.IsKeyUp(Keys.LeftShift))
- {
- camera.cameraSpeed = 5;
- }
- cubeMoon.rotationCenter = earth.GetActualPosition();
- base.Update(gameTime);
- prevState = keyboardState;
- }
- protected override void Draw(GameTime gameTime)
- {
- graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
- if (_background)
- {
- spriteBatch.Begin();
- spriteBatch.Draw(background,
- new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height),
- Color.White);
- spriteBatch.End();
- }
- RasterizerState rasterizerState = new RasterizerState()
- {
- CullMode = CullMode.None
- };
- GraphicsDevice.RasterizerState = rasterizerState;
- var depthStencilState = new DepthStencilState
- {
- DepthBufferEnable = true
- };
- GraphicsDevice.DepthStencilState = depthStencilState;
- if(_grid)
- floor.Draw(camera, basicEffect);
- sun.Draw();
- earth.Draw();
- cubeMoon.Draw();
- mars.Draw();
- saturn.Draw();
- base.Draw(gameTime);
- }
- }
- public class Camera : GameComponent
- {
- private KeyboardState prevState;
- private Vector3 cameraPosition;
- public Vector3 cameraRotation;
- public float cameraSpeed { get; set; }
- public Vector3 cameraLookAt;
- private Vector3 mouseRotationBuffer;
- private MouseState currentMouseState;
- private MouseState prevMouseState;
- public BoundingBox ColliderBox { get { return new BoundingBox(cameraPosition - new Vector3(0.3f), cameraPosition + new Vector3(0.3f)); } }
- public bool ShowCenterLine { get; set; }
- public Vector3 Position
- {
- get { return cameraPosition; }
- set
- {
- cameraPosition = value;
- UpdateLookAt();
- }
- }
- public Vector3 Rotation
- {
- get { return cameraRotation; }
- set
- {
- cameraRotation = value;
- UpdateLookAt();
- }
- }
- public Matrix Projection { get; protected set; }
- public Matrix View { get { return Matrix.CreateLookAt(cameraPosition, cameraLookAt, Vector3.Up); } }
- public Camera(Game game, Vector3 position, Vector3 rotation, float speed) : base(game)
- {
- cameraSpeed = speed;
- Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, Game.GraphicsDevice.Viewport.AspectRatio, 0.05f, 1000);
- MoveTo(position, rotation);
- prevMouseState = Mouse.GetState();
- }
- private void MoveTo(Vector3 pos, Vector3 rot)
- {
- Position = pos;
- Rotation = rot;
- }
- private void UpdateLookAt()
- {
- var rotationMartix = Matrix.CreateRotationX(cameraRotation.X) * Matrix.CreateRotationY(cameraRotation.Y);
- var lookAtOffset = Vector3.Transform(Vector3.UnitZ, rotationMartix);
- cameraLookAt = cameraPosition + lookAtOffset;
- }
- private Vector3 PreviewMove(Vector3 amount)
- {
- var rotate = Matrix.CreateRotationY(cameraRotation.Y);
- var movment = new Vector3(amount.X, amount.Y, amount.Z);
- movment = Vector3.Transform(movment, rotate);
- return cameraPosition + movment;
- }
- public Vector3 cameraAxiesPosition
- {
- get
- {
- var rotate = Matrix.CreateRotationX(cameraRotation.X) * Matrix.CreateRotationY(cameraRotation.Y) * Matrix.CreateRotationZ(cameraRotation.Z);
- var movment = new Vector3(-0.3f, -0.15f, 0.5f);
- movment = Vector3.Transform(movment, rotate);
- return cameraPosition + movment;
- }
- }
- private void Move(Vector3 scale)
- {
- MoveTo(PreviewMove(scale), Rotation);
- }
- public override void Update(GameTime gameTime)
- {
- if (Game.IsActive)
- {
- currentMouseState = Mouse.GetState();
- var keyboardState = Keyboard.GetState();
- var dt = (float)gameTime.ElapsedGameTime.TotalSeconds;
- var moveVector = Vector3.Zero;
- if (keyboardState.IsKeyDown(Keys.Q))
- moveVector.Z = 1;
- if (keyboardState.IsKeyDown(Keys.A))
- moveVector.Z = -1;
- if (keyboardState.IsKeyDown(Keys.Left))
- moveVector.X = 1;
- if (keyboardState.IsKeyDown(Keys.Right))
- moveVector.X = -1;
- if (keyboardState.IsKeyDown(Keys.Up)) //up
- moveVector.Y = 1;
- if (keyboardState.IsKeyDown(Keys.Down)) //down
- moveVector.Y = -1;
- if (moveVector != Vector3.Zero)
- {
- moveVector.Normalize();
- moveVector *= dt * cameraSpeed;
- Move(moveVector);
- }
- //#region Camera Mouse
- //if (currentMouseState != prevMouseState)
- //{
- // var deltaX = currentMouseState.X - (Game.GraphicsDevice.Viewport.Width / 2);
- // var deltaY = currentMouseState.Y - (Game.GraphicsDevice.Viewport.Height / 2);
- // mouseRotationBuffer.X -= 0.05F * deltaX * dt; //0,05 czułość <3
- // mouseRotationBuffer.Y -= 0.05F * deltaY * dt;
- // if (mouseRotationBuffer.Y < MathHelper.ToRadians(-75))
- // mouseRotationBuffer.Y -= mouseRotationBuffer.Y - MathHelper.ToRadians(-75);
- // if (mouseRotationBuffer.Y > MathHelper.ToRadians(75))
- // mouseRotationBuffer.Y -= mouseRotationBuffer.Y - MathHelper.ToRadians(75);
- // Rotation = new Vector3(
- // -MathHelper.Clamp(mouseRotationBuffer.Y, MathHelper.ToRadians(-75f), MathHelper.ToRadians(75f)),
- // MathHelper.WrapAngle(mouseRotationBuffer.X),
- // 0);
- //}
- //Mouse.SetPosition(Game.GraphicsDevice.Viewport.Width / 2, Game.GraphicsDevice.Viewport.Height / 2);
- prevMouseState = currentMouseState;
- //#endregion
- prevState = keyboardState;
- base.Update(gameTime);
- }
- }
- }
- public class Floor
- {
- private int floorWidth;
- private int floorHeight;
- private VertexBuffer floorBuffer;
- private GraphicsDevice device;
- Color[] floorColors = new Color[2] { Color.Transparent, Color.Gray };
- public Floor(GraphicsDevice device, int width, int height)
- {
- this.device = device;
- floorWidth = width;
- floorHeight = height;
- BuildFloorBuffer();
- }
- private void BuildFloorBuffer()
- {
- List<VertexPositionColor> vertexList = new List<VertexPositionColor>();
- int counter = 0;
- for (int x = 0; x < floorWidth; x++)
- {
- counter++;
- for (int z = 0; z < floorHeight; z++)
- {
- counter++;
- foreach (var vertex in FloorTile(x, z, floorColors[counter % 2]))
- {
- vertexList.Add(vertex);
- }
- }
- }
- floorBuffer = new VertexBuffer(device, VertexPositionColor.VertexDeclaration, vertexList.Count, BufferUsage.None);
- floorBuffer.SetData(vertexList.ToArray());
- }
- private List<VertexPositionColor> FloorTile(int xOffset, int zOffset, Color tileColor)
- {
- var vList = new List<VertexPositionColor>()
- {
- new VertexPositionColor(new Vector3(0 + xOffset - 100, 0 + zOffset - floorHeight/2, 0), tileColor),
- new VertexPositionColor(new Vector3(1 + xOffset - 100, 0 + zOffset - floorHeight/2, 0), tileColor),
- new VertexPositionColor(new Vector3(0 + xOffset - 100, 1 + zOffset - floorHeight/2, 0), tileColor),
- new VertexPositionColor(new Vector3(1 + xOffset - 100, 0 + zOffset - floorHeight/2, 0), tileColor),
- new VertexPositionColor(new Vector3(1 + xOffset - 100, 1 + zOffset - floorHeight/2, 0), tileColor),
- new VertexPositionColor(new Vector3(0 + xOffset - 100, 1 + zOffset - floorHeight/2, 0), tileColor),
- };
- return vList;
- }
- public void Draw(Camera camera, BasicEffect effect)
- {
- effect.VertexColorEnabled = true;
- effect.View = camera.View;
- effect.Projection = camera.Projection;
- effect.World = Matrix.Identity;
- foreach (var pass in effect.CurrentTechnique.Passes)
- {
- pass.Apply();
- device.SetVertexBuffer(floorBuffer);
- device.DrawPrimitives(PrimitiveType.TriangleList, 0, floorBuffer.VertexCount / 3);
- }
- }
- }
- public class Cube : GameComponent
- {
- public Vector3 middleLocation;
- VertexPositionColor[] planetVertices;
- GraphicsDevice graphicsDevice;
- Vector3 speed;
- Vector3 size;
- Vector3 angle;
- Game1 game;
- public Vector3 rotationCenter;
- Camera camera;
- private Matrix World;
- Vector3 vectorZero = Vector3.Zero;
- public List<Color> Colors;
- public Cube(Game game, Vector3 middleLocation, Vector3 rotationCenter, Vector3 size, Vector3 speed, GraphicsDevice graphicsDevice, params Color[] colors)
- : base(game)
- {
- Colors = colors.ToList();
- if (!Colors.Any())
- {
- Colors = new List<Color>() { Color.White, Color.Black, Color.White, Color.Black };
- }
- this.game = game as Game1;
- this.camera = this.game.camera;
- this.middleLocation = middleLocation;
- angle = Vector3.Zero;
- this.size = size;
- this.speed = speed;
- this.rotationCenter = rotationCenter;
- this.graphicsDevice = graphicsDevice;
- this.basicEffect = new BasicEffect(graphicsDevice)
- {
- Alpha = 1,
- VertexColorEnabled = true,
- LightingEnabled = false,
- };
- planetVertices = CubeVerticles();
- }
- public VertexPositionColor[] GetPlanetVertices()
- {
- return this.planetVertices;
- }
- public Vector3 GetActualPosition()
- {
- return Vector3.Transform(middleLocation, World);
- }
- public override void Update(GameTime gameTime)
- {
- angle += speed;
- World = Matrix.Identity;
- World *= Matrix.CreateRotationX(MathHelper.ToRadians(angle.X));
- World *= Matrix.CreateRotationY(MathHelper.ToRadians(angle.Y));
- World *= Matrix.CreateRotationZ(MathHelper.ToRadians(angle.Z));
- World *= Matrix.CreateTranslation(rotationCenter);
- basicEffect.World = World;
- basicEffect.View = camera.View;
- basicEffect.Projection = camera.Projection;
- }
- private VertexPositionColor[] CubeVerticles()
- {
- Vector3 topLeftFront = new Vector3(-size.X + middleLocation.X, size.Y + middleLocation.Y, size.Z + middleLocation.Z);
- Vector3 bottomLeftFront = new Vector3(-size.X + middleLocation.X, -size.Y + middleLocation.Y, size.Z + middleLocation.Z);
- Vector3 topRightFront = new Vector3(size.X + middleLocation.X, size.Y + middleLocation.Y, size.Z + middleLocation.Z);
- Vector3 bottomRightFront = new Vector3(size.X + middleLocation.X, -size.Y + middleLocation.Y, size.Z + middleLocation.Z);
- Vector3 topLeftBack = new Vector3(-size.X + middleLocation.X, size.Y + middleLocation.Y, -size.Z + middleLocation.Z);
- Vector3 topRightBack = new Vector3(size.X + middleLocation.X, size.Y + middleLocation.Y, -size.Z + middleLocation.Z);
- Vector3 bottomLeftBack = new Vector3(-size.X + middleLocation.X, -size.Y + middleLocation.Y, -size.Z + middleLocation.Z);
- Vector3 bottomRightBack = new Vector3(size.X + middleLocation.X, -size.Y + middleLocation.Y, -size.Z + middleLocation.Z);
- Random rnd = new Random();
- VertexPositionColor[] verticles = new VertexPositionColor[8]{
- new VertexPositionColor(topLeftFront, Colors[rnd.Next(0,Colors.Count())]),//1
- new VertexPositionColor(bottomLeftFront, Colors[rnd.Next(0,Colors.Count())]),//1
- new VertexPositionColor(topRightFront, Colors[rnd.Next(0,Colors.Count())]),//2
- new VertexPositionColor(bottomRightFront, Colors[rnd.Next(0,Colors.Count())]),//3
- new VertexPositionColor(topLeftBack, Colors[rnd.Next(0,Colors.Count())]),//4
- new VertexPositionColor(topRightBack, Colors[rnd.Next(0,Colors.Count())]),//5
- new VertexPositionColor(bottomLeftBack, Colors[rnd.Next(0,Colors.Count())]),//6
- new VertexPositionColor(bottomRightBack, Colors[rnd.Next(0,Colors.Count())])//7
- };
- List<VertexPositionColor> cubeVertices = new List<VertexPositionColor>();
- //Front
- cubeVertices.Add(verticles[2]); cubeVertices.Add(verticles[3]); cubeVertices.Add(verticles[0]);
- cubeVertices.Add(verticles[3]); cubeVertices.Add(verticles[1]); cubeVertices.Add(verticles[0]);
- //Left
- cubeVertices.Add(verticles[0]); cubeVertices.Add(verticles[1]); cubeVertices.Add(verticles[4]);
- cubeVertices.Add(verticles[1]); cubeVertices.Add(verticles[6]); cubeVertices.Add(verticles[4]);
- //Back
- cubeVertices.Add(verticles[4]); cubeVertices.Add(verticles[6]); cubeVertices.Add(verticles[7]);
- cubeVertices.Add(verticles[5]); cubeVertices.Add(verticles[4]); cubeVertices.Add(verticles[7]);
- //Right
- cubeVertices.Add(verticles[5]); cubeVertices.Add(verticles[7]); cubeVertices.Add(verticles[2]);
- cubeVertices.Add(verticles[7]); cubeVertices.Add(verticles[3]); cubeVertices.Add(verticles[2]);
- //Top
- cubeVertices.Add(verticles[2]); cubeVertices.Add(verticles[0]); cubeVertices.Add(verticles[5]);
- cubeVertices.Add(verticles[5]); cubeVertices.Add(verticles[0]); cubeVertices.Add(verticles[4]);
- //Bottom
- cubeVertices.Add(verticles[1]); cubeVertices.Add(verticles[3]); cubeVertices.Add(verticles[6]);
- cubeVertices.Add(verticles[6]); cubeVertices.Add(verticles[3]); cubeVertices.Add(verticles[7]);
- return cubeVertices.ToArray();
- }
- public void Draw()
- {
- basicEffect.CurrentTechnique.Passes[0].Apply();
- this.graphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleList, planetVertices, 0, planetVertices.Length / 3);
- }
- public BasicEffect basicEffect { get; set; }
- }
- public class Line : IDisposable
- {
- public Vector3 startPoint;
- public Vector3 endPoint;
- private VertexPositionColor[] vertices;
- public Line(Vector3 startPoint, Vector3 endPoint)
- {
- this.startPoint = startPoint;
- this.endPoint = endPoint;
- }
- public Line() { }
- public void DrawLine(BasicEffect basicEffect, GraphicsDevice graphicsDevice)
- {
- basicEffect.CurrentTechnique.Passes[0].Apply();
- vertices = new[] { new VertexPositionColor(startPoint, Color.White), new VertexPositionColor(endPoint, Color.White) };
- graphicsDevice.DrawUserPrimitives(PrimitiveType.LineList, vertices, 0, 1);
- }
- public void DrawLine(BasicEffect basicEffect, GraphicsDevice graphicsDevice, Vector3 start, Vector3 end)
- {
- basicEffect.CurrentTechnique.Passes[0].Apply();
- vertices = new[] { new VertexPositionColor(start, Color.White), new VertexPositionColor(end, Color.White) };
- graphicsDevice.DrawUserPrimitives(PrimitiveType.LineList, vertices, 0, 1);
- }
- public void DrawLine(BasicEffect basicEffect, GraphicsDevice graphicsDevice, Vector3 start, Vector3 end, Color color)
- {
- basicEffect.CurrentTechnique.Passes[0].Apply();
- vertices = new[] { new VertexPositionColor(start, color), new VertexPositionColor(end, color) };
- graphicsDevice.DrawUserPrimitives(PrimitiveType.LineList, vertices, 0, 1);
- }
- public void Dispose()
- {
- throw new NotImplementedException();
- }
- }
- }