Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using System.Linq;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using OrientationSystems;
- namespace MazeEscape.CustomClasses
- {
- public class Camera : GameComponent
- {
- private Vector3 cameraPosition;
- public Vector3 cameraRotation;
- public float cameraSpeed { get; set; }
- public Vector3 cameraLookAt;
- private Vector3 mouseRotationBuffer;
- private MouseState currentMouseState;
- private MouseState prevMouseState;
- 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 => Matrix.CreateLookAt(cameraPosition, cameraLookAt, Vector3.Up);
- public Game1 game { get; set; }
- public Camera(Game game, Vector3 position, Vector3 rotation, float speed) : base(game)
- {
- this.game = game as Game1;
- 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 PreviewMove3D(Vector3 amount)
- {
- var rotate = Matrix.CreateRotationX(cameraRotation.X) * Matrix.CreateRotationY(cameraRotation.Y) * Matrix.CreateRotationZ(cameraRotation.Z);
- var movment = new Vector3(amount.X, amount.Y, amount.Z);
- movment = Vector3.Transform(movment, rotate);
- return cameraPosition + movment;
- }
- 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);
- }
- private KeyboardState prevState;
- public int walkTimer = 0;
- 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.W))
- {
- moveVector.Z = 1;
- }
- if (keyboardState.IsKeyDown(Keys.S))
- {
- moveVector.Z = -1;
- }
- if (keyboardState.IsKeyDown(Keys.A))
- {
- moveVector.X = 1;
- }
- if (keyboardState.IsKeyDown(Keys.D))
- {
- moveVector.X = -1;
- }
- if (keyboardState.IsKeyDown(Keys.Space))
- {
- moveVector.Y = 1;
- }
- if (keyboardState.IsKeyDown(Keys.C))
- {
- 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;
- 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);
- }
- }
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// END CAMERA
- using MazeEscape.CustomClasses;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- namespace OrientationSystems
- {
- public class LineFloor
- {
- private Game1 game1;
- private List<Line> Lines;
- private Vector3 Position;
- private float Width, Height, Spacing;
- private GraphicsDevice GraphicsDevice;
- public LineFloor(Game1 game, GraphicsDevice graphicsDevice, Vector3 position, float width, float height, float spacing)
- {
- game1 = game;
- Width = width;
- Height = height;
- Spacing = spacing;
- Position = position;
- GraphicsDevice = graphicsDevice;
- CreateFloor();
- }
- public void CreateFloor()
- {
- Lines = new List<Line>();
- for (int x = 0; x < Width; x++)
- {
- Lines.Add(new Line(GraphicsDevice, new Vector3(Position.X + (x * Spacing), Position.Y, Position.Z), new Vector3(Position.X + (x * Spacing), Position.Y, Position.Z + Width)));
- }
- Lines.Add(new Line(GraphicsDevice, new Vector3(Position.X + (Width * Spacing), Position.Y, Position.Z), new Vector3(Position.X + (Width * Spacing), Position.Y, Position.Z + Width)));
- for (int z = 0; z <= Height; z++)
- {
- Lines.Add(new Line(GraphicsDevice, new Vector3(Position.X, Position.Y, Position.Z + (z * Spacing)), new Vector3(Position.X + Width, Position.Y, Position.Z + (z * Spacing))));
- }
- Lines.Add(new Line(GraphicsDevice, new Vector3(Position.X, Position.Y, Position.Z + (Height * Spacing)), new Vector3(Position.X + Width, Position.Y, Position.Z + (Height * Spacing))));
- }
- public void Draw(BasicEffect basicEffect,Camera camera)
- {
- basicEffect.VertexColorEnabled = true;
- basicEffect.View = camera.View;
- basicEffect.Projection = camera.Projection;
- basicEffect.World = Matrix.CreateTranslation(Position);
- foreach (var line in Lines)
- {
- line.DrawLine(game1.camera, basicEffect, Color.Silver);
- }
- }
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// END LINE FLOR END
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using MazeEscape.CustomClasses;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- namespace OrientationSystems
- {
- public class CordSystem
- {
- public CordSystem ParentSystem { get; set; }
- public Vector3 Position { get; set; }
- public Vector3 Rotation { get; set; }
- public Vector3 Scale { get; set; }
- public bool IgnoreParentRotation { get; set; }
- private List<Line> CordLines { get; set; }
- public Vector3 GetRelativePosition
- {
- get
- {
- if (ParentSystem != null)
- {
- return Position + ParentSystem.GetRelativePosition;
- }
- else
- {
- return Position;
- }
- }
- }
- public Vector3 GetRelativeRotation
- {
- get
- {
- if (ParentSystem != null && !IgnoreParentRotation)
- {
- return Rotation + ParentSystem.GetRelativeRotation;
- }
- else
- {
- return Rotation;
- }
- }
- }
- private GraphicsDevice GraphicsDevice;
- public CordSystem(GraphicsDevice graphicsDevice, CordSystem parent, Vector3 position)
- {
- Scale = Vector3.One;
- IgnoreParentRotation = false;
- GraphicsDevice = graphicsDevice;
- ParentSystem = parent;
- Position = position;
- Rotation = Vector3.Zero;
- CordLines = new List<Line>(){
- new Line(GraphicsDevice, Vector3.Zero, new Vector3(20, 0, 0)) { color = Color.Red, Position = GetRelativePosition },
- new Line(GraphicsDevice, Vector3.Zero, new Vector3(0, 20, 0)) { color = Color.Green, Position = GetRelativePosition },
- new Line(GraphicsDevice, Vector3.Zero, new Vector3(0, 0, 20)) { color = Color.Blue, Position = GetRelativePosition }
- };
- }
- public void DrawSystemAxies(Camera camera)
- {
- var basicEffectBuff = CreateCordSystemBasicEffect(camera);
- foreach (var cordLine in CordLines)
- {
- cordLine.Position = GetRelativePosition;
- cordLine.DrawLine(camera, basicEffectBuff, cordLine.color);
- }
- }
- public BasicEffect CreateCordSystemBasicEffect(Camera camera)
- {
- var basicEffect = new BasicEffect(GraphicsDevice);
- basicEffect.VertexColorEnabled = true;
- basicEffect.View = camera.View;
- basicEffect.Projection = camera.Projection;
- basicEffect.World = Matrix.CreateScale(Scale) *
- Matrix.CreateTranslation(GetRelativePosition) *
- Matrix.CreateRotationX(GetRelativeRotation.X) *
- Matrix.CreateRotationY(GetRelativeRotation.Y) *
- Matrix.CreateRotationZ(GetRelativeRotation.Z);
- return basicEffect;
- }
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// END CORD SYSTEM END
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using OrientationSystems;
- namespace MazeEscape.CustomClasses
- {
- public class Line
- {
- public Vector3 Position;
- public Vector3 startPoint;
- public Vector3 endPoint;
- private VertexPositionColor[] vertices;
- private GraphicsDevice GraphicsDevice;
- public Color color;
- public Line(GraphicsDevice graphicsDevice)
- {
- Position = Vector3.Zero;
- GraphicsDevice = graphicsDevice;
- }
- public Line(GraphicsDevice graphicsDevice, Vector3 start, Vector3 end)
- {
- Position = Vector3.Zero;
- GraphicsDevice = graphicsDevice;
- startPoint = start;
- endPoint = end;
- }
- public void DrawLine(Camera camera, BasicEffect basicEffect, 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 DrawLine(Camera camera, BasicEffect basicEffect, Color color)
- {
- basicEffect.CurrentTechnique.Passes[0].Apply();
- vertices = new[] { new VertexPositionColor(startPoint, color), new VertexPositionColor(endPoint, color) };
- GraphicsDevice.DrawUserPrimitives(PrimitiveType.LineList, vertices, 0, 1);
- }
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// END LINE END
- using System.Collections.Generic;
- using MazeEscape.CustomClasses;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- namespace OrientationSystems
- {
- public class Game1 : Game
- {
- GraphicsDeviceManager graphics;
- public Camera camera;
- SpriteBatch spriteBatch;
- private BasicEffect basicEffect;
- SpriteFont Font;
- private CordSystem system1, system2, system3;
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- Window.AllowUserResizing = false;
- graphics.PreferredBackBufferHeight = 720;
- graphics.PreferredBackBufferWidth = 1280;
- IsMouseVisible = true;
- }
- private LineFloor floor;
- protected override void Initialize()
- {
- spriteBatch = new SpriteBatch(GraphicsDevice);
- basicEffect = new BasicEffect(GraphicsDevice)
- {
- Alpha = 1,
- VertexColorEnabled = true,
- LightingEnabled = false,
- };
- Font = Content.Load<SpriteFont>("SpriteFontPL");
- // Tworzenie i dodawanie kamery
- camera = new Camera(this, new Vector3(0, 15, 0), Vector3.Zero, 15);
- Components.Add(camera);
- system1 = new CordSystem(GraphicsDevice, null, Vector3.Zero);
- system2 = new CordSystem(GraphicsDevice, system1, new Vector3(5));
- system3 = new CordSystem(GraphicsDevice, system2, new Vector3(5));
- floor = new LineFloor(this, GraphicsDevice, Vector3.Zero, 50, 50, 1);
- base.Initialize();
- }
- private KeyboardState statePrev;
- protected override void Update(GameTime gameTime)
- {
- var state = Keyboard.GetState();
- if (state.IsKeyDown(Keys.Escape))
- Exit();
- var moveSpeed = 0.5f;
- var rotateSpeed = 0.05f;
- #region SterowanieSystem2
- if (state.IsKeyUp(Keys.RightControl))
- {
- if (state.IsKeyDown(Keys.Up))
- system2.Position += new Vector3(0, 0, moveSpeed);
- if (state.IsKeyDown(Keys.Down))
- system2.Position -= new Vector3(0, 0, moveSpeed);
- if (state.IsKeyDown(Keys.Left))
- system2.Position += new Vector3(moveSpeed, 0, 0);
- if (state.IsKeyDown(Keys.Right))
- system2.Position -= new Vector3(moveSpeed, 0, 0);
- if (state.IsKeyDown(Keys.PageUp))
- system2.Position += new Vector3(0, moveSpeed, 0);
- if (state.IsKeyDown(Keys.PageDown))
- system2.Position -= new Vector3(0, moveSpeed, 0);
- if (state.IsKeyDown(Keys.NumPad7))
- system2.Rotation += new Vector3(0, 0, rotateSpeed);
- if (state.IsKeyDown(Keys.NumPad9))
- system2.Rotation -= new Vector3(0, 0, rotateSpeed);
- if (state.IsKeyDown(Keys.NumPad8))
- system2.Rotation += new Vector3(rotateSpeed, 0, 0);
- if (state.IsKeyDown(Keys.NumPad2))
- system2.Rotation -= new Vector3(rotateSpeed, 0, 0);
- if (state.IsKeyDown(Keys.NumPad4))
- system2.Rotation += new Vector3(0, rotateSpeed, 0);
- if (state.IsKeyDown(Keys.NumPad6))
- system2.Rotation -= new Vector3(0, rotateSpeed, 0);
- if (state.IsKeyDown(Keys.M) && statePrev.IsKeyDown(Keys.M))
- system2.IgnoreParentRotation = !system2.IgnoreParentRotation;
- }
- #endregion
- #region SterowanieSystem2
- if (state.IsKeyDown(Keys.RightControl))
- {
- if (state.IsKeyDown(Keys.Up))
- system3.Position += new Vector3(0, 0, moveSpeed);
- if (state.IsKeyDown(Keys.Down))
- system3.Position -= new Vector3(0, 0, moveSpeed);
- if (state.IsKeyDown(Keys.Left))
- system3.Position += new Vector3(moveSpeed, 0, 0);
- if (state.IsKeyDown(Keys.Right))
- system3.Position -= new Vector3(moveSpeed, 0, 0);
- if (state.IsKeyDown(Keys.PageUp))
- system3.Position += new Vector3(0, moveSpeed, 0);
- if (state.IsKeyDown(Keys.PageDown))
- system3.Position -= new Vector3(0, moveSpeed, 0);
- if (state.IsKeyDown(Keys.NumPad7))
- system3.Rotation += new Vector3(0, 0, rotateSpeed);
- if (state.IsKeyDown(Keys.NumPad9))
- system3.Rotation -= new Vector3(0, 0, rotateSpeed);
- if (state.IsKeyDown(Keys.NumPad8))
- system3.Rotation += new Vector3(rotateSpeed, 0, 0);
- if (state.IsKeyDown(Keys.NumPad2))
- system3.Rotation -= new Vector3(rotateSpeed, 0, 0);
- if (state.IsKeyDown(Keys.NumPad4))
- system3.Rotation += new Vector3(0, rotateSpeed, 0);
- if (state.IsKeyDown(Keys.NumPad6))
- system3.Rotation -= new Vector3(0, rotateSpeed, 0);
- if (state.IsKeyDown(Keys.M) && statePrev.IsKeyDown(Keys.M))
- system3.IgnoreParentRotation = !system3.IgnoreParentRotation;
- }
- #endregion
- statePrev = state;
- base.Update(gameTime);
- }
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.CornflowerBlue);
- spriteBatch.Begin();
- {
- spriteBatch.DrawString(Font, $"System1 {system1.Position} === {system1.GetRelativePosition}", new Vector2(20, 20), Color.White, 0, Vector2.Zero, new Vector2(0.3f), SpriteEffects.None, 0);
- spriteBatch.DrawString(Font, $"System2 {system2.Position} === {system2.GetRelativePosition}", new Vector2(20, 45), Color.White, 0, Vector2.Zero, new Vector2(0.3f), SpriteEffects.None, 0);
- spriteBatch.DrawString(Font, $"System3 {system3.Position} === {system3.GetRelativePosition}", new Vector2(20, 70), Color.White, 0, Vector2.Zero, new Vector2(0.3f), SpriteEffects.None, 0);
- }
- spriteBatch.End();
- var depthStencilState = new DepthStencilState
- {
- DepthBufferEnable = true
- };
- GraphicsDevice.DepthStencilState = depthStencilState;
- floor.Draw(basicEffect, camera);
- system1.DrawSystemAxies(camera);
- system2.DrawSystemAxies(camera);
- system3.DrawSystemAxies(camera);
- // TODO: Add your drawing code here
- base.Draw(gameTime);
- }
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// END GAME 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement