Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Audio;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.GamerServices;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Media;
- namespace Układ_Słoneczny
- {
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- Texture2D background;
- Cube sun, mercury, venus, earth, mars, moon;
- public Matrix view, world, projection;
- BasicEffect basicEffect;
- KeyboardState keyboard, keyboardPrevious;
- Vector3 cameraPosition;
- float cameraDistance;
- float angleX, angleY;
- bool backgroundActive;
- bool netActive;
- NetGenerator net;
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- graphics.PreferredBackBufferWidth = 800;
- graphics.PreferredBackBufferHeight = 600;
- Content.RootDirectory = "Content";
- }
- protected override void Initialize()
- {
- this.Window.AllowUserResizing = true;
- sun = new Cube(Color.Orange, Color.Yellow, new Vector3(0, 0, 0), new Vector3(0, 0, 0), 3, 2, GraphicsDevice);
- mercury = new Cube(Color.DarkRed, Color.IndianRed, new Vector3(15, 0, 0), new Vector3(0, 0, 0), 1, 2.5f, GraphicsDevice);
- venus = new Cube(Color.Blue, Color.White, new Vector3(30, 0, 0), new Vector3(0, 0, 0), 1, 1, GraphicsDevice);
- earth = new Cube(Color.LightBlue, Color.LimeGreen, new Vector3(40, 0, 0), new Vector3(0, 0, 0), 2, 1.5f, GraphicsDevice);
- mars = new Cube(Color.Red, Color.Gray, new Vector3(55, 0, 0), new Vector3(0, 0, 0), 1, 1.25f, GraphicsDevice);
- moon = new Cube(Color.Gray, Color.White, new Vector3(5, 0, 0), new Vector3(0, 0, 0), 1, 2, GraphicsDevice);
- angleX = 0.0f;
- angleY = 0.0f;
- cameraDistance = 150.0f;
- backgroundActive = true;
- netActive = false;
- cameraPosition = new Vector3(0, 0, cameraDistance);
- net = new NetGenerator(new Vector2(-80, -80), new Vector2(80, 80));
- base.Initialize();
- }
- protected override void LoadContent()
- {
- // Create a new SpriteBatch, which can be used to draw textures.
- spriteBatch = new SpriteBatch(GraphicsDevice);
- background = this.Content.Load<Texture2D>("stars");
- world = Matrix.Identity;
- view = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up);
- projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), GraphicsDevice.Viewport.AspectRatio, 1.0f, 1000.0f);
- basicEffect = new BasicEffect(GraphicsDevice);
- basicEffect.VertexColorEnabled = true;
- basicEffect.World = world;
- basicEffect.View = view;
- basicEffect.Projection = projection;
- }
- protected override void Update(GameTime gameTime)
- {
- if (Keyboard.GetState().IsKeyDown(Keys.Escape))
- this.Exit();
- keyboard = Keyboard.GetState();
- if (keyboard.IsKeyDown(Keys.Right))
- angleY++;
- if (keyboard.IsKeyDown(Keys.Left))
- angleY--;
- if (keyboard.IsKeyDown(Keys.Up))
- angleX--;
- if (keyboard.IsKeyDown(Keys.Down))
- angleX++;
- if (keyboard.IsKeyDown(Keys.Q))
- cameraDistance += 0.25f;
- if (keyboard.IsKeyDown(Keys.A))
- cameraDistance -= 0.25f;
- if (keyboard.IsKeyDown(Keys.B) && keyboardPrevious.IsKeyUp(Keys.B))
- backgroundActive = !backgroundActive;
- if (keyboard.IsKeyDown(Keys.X) && keyboardPrevious.IsKeyUp(Keys.X))
- netActive = !netActive;
- view = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up);
- cameraPosition = new Vector3(0.0f, 0.0f, cameraDistance);
- view = Matrix.CreateRotationX(MathHelper.ToRadians(angleX)) * Matrix.CreateRotationY(MathHelper.ToRadians(angleY)) * view;
- basicEffect.View = view;
- keyboardPrevious = keyboard;
- sun.Update(gameTime, view, projection);
- venus.Update(gameTime, view, projection);
- mercury.Update(gameTime, view, projection);
- earth.Update(gameTime, view, projection);
- mars.Update(gameTime, view, projection);
- moon.UpdateMoon(gameTime, view, projection, earth.GetActualPosition());
- base.Update(gameTime);
- }
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.CornflowerBlue);
- if (backgroundActive)
- {
- spriteBatch.Begin();
- spriteBatch.Draw(background, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), Color.White);
- spriteBatch.End();
- }
- basicEffect.CurrentTechnique.Passes[0].Apply();
- if (netActive) GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineList, net.GetVertices(), 0, net.GetVerticesLength() / 2);
- earth.Draw();
- mercury.Draw();
- venus.Draw();
- mars.Draw();
- sun.Draw();
- moon.Draw();
- base.Draw(gameTime);
- }
- }
- }
- /*using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework;
- namespace Układ_Słoneczny
- {
- class NetGenerator
- {
- VertexPositionColor[] lineVertices;
- Vector2 startPosition;
- Vector2 endPosition;
- public NetGenerator(Vector2 startPosition,Vector2 endPosition)
- {
- this.startPosition = startPosition;
- this.endPosition = endPosition;
- lineVertices = GenerateLines();
- }
- public VertexPositionColor[] GetVertices()
- {
- return lineVertices;
- }
- public int GetVerticesLength()
- {
- return lineVertices.Length;
- }
- private VertexPositionColor[] GenerateLines()
- {
- List<VertexPositionColor> lines = new List<VertexPositionColor>();
- int count = (int)(endPosition.X - startPosition.X) / 40;
- for (int i = 0; i <=40; i++)
- {
- lines.Add(new VertexPositionColor(new Vector3(startPosition.X ,startPosition.Y+count*i,-4),Color.White));
- lines.Add(new VertexPositionColor(new Vector3(endPosition.X,startPosition.Y+count*i,-4),Color.White));
- lines.Add(new VertexPositionColor(new Vector3(startPosition.X + count * i, startPosition.Y, -4), Color.White));
- lines.Add(new VertexPositionColor(new Vector3(startPosition.X + count * i, endPosition.Y, -4), Color.White));
- }
- return lines.ToArray();
- }
- }
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement