Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11.  
  12. namespace Układ_Słoneczny
  13. {
  14.     public class Game1 : Microsoft.Xna.Framework.Game
  15.     {
  16.         GraphicsDeviceManager graphics;
  17.         SpriteBatch spriteBatch;
  18.         Texture2D background;
  19.         Cube sun, mercury, venus, earth, mars, moon;
  20.         public Matrix view, world, projection;
  21.         BasicEffect basicEffect;
  22.         KeyboardState keyboard, keyboardPrevious;
  23.         Vector3 cameraPosition;
  24.         float cameraDistance;
  25.         float angleX, angleY;
  26.         bool backgroundActive;
  27.         bool netActive;
  28.         NetGenerator net;
  29.  
  30.         public Game1()
  31.         {
  32.             graphics = new GraphicsDeviceManager(this);
  33.             graphics.PreferredBackBufferWidth = 800;
  34.             graphics.PreferredBackBufferHeight = 600;
  35.             Content.RootDirectory = "Content";
  36.         }
  37.  
  38.         protected override void Initialize()
  39.         {
  40.             this.Window.AllowUserResizing = true;
  41.             sun = new Cube(Color.Orange, Color.Yellow, new Vector3(0, 0, 0), new Vector3(0, 0, 0), 3, 2, GraphicsDevice);
  42.             mercury = new Cube(Color.DarkRed, Color.IndianRed, new Vector3(15, 0, 0), new Vector3(0, 0, 0), 1, 2.5f, GraphicsDevice);
  43.             venus = new Cube(Color.Blue, Color.White, new Vector3(30, 0, 0), new Vector3(0, 0, 0), 1, 1, GraphicsDevice);
  44.             earth = new Cube(Color.LightBlue, Color.LimeGreen, new Vector3(40, 0, 0), new Vector3(0, 0, 0), 2, 1.5f, GraphicsDevice);
  45.             mars = new Cube(Color.Red, Color.Gray, new Vector3(55, 0, 0), new Vector3(0, 0, 0), 1, 1.25f, GraphicsDevice);
  46.             moon = new Cube(Color.Gray, Color.White, new Vector3(5, 0, 0), new Vector3(0, 0, 0), 1, 2, GraphicsDevice);
  47.             angleX = 0.0f;
  48.             angleY = 0.0f;
  49.             cameraDistance = 150.0f;
  50.             backgroundActive = true;
  51.             netActive = false;
  52.             cameraPosition = new Vector3(0, 0, cameraDistance);
  53.             net = new NetGenerator(new Vector2(-80, -80), new Vector2(80, 80));
  54.             base.Initialize();
  55.         }
  56.  
  57.         protected override void LoadContent()
  58.         {
  59.             // Create a new SpriteBatch, which can be used to draw textures.
  60.             spriteBatch = new SpriteBatch(GraphicsDevice);
  61.  
  62.             background = this.Content.Load<Texture2D>("stars");
  63.             world = Matrix.Identity;
  64.  
  65.             view = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up);
  66.             projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), GraphicsDevice.Viewport.AspectRatio, 1.0f, 1000.0f);
  67.             basicEffect = new BasicEffect(GraphicsDevice);
  68.             basicEffect.VertexColorEnabled = true;
  69.             basicEffect.World = world;
  70.             basicEffect.View = view;
  71.             basicEffect.Projection = projection;
  72.         }
  73.  
  74.  
  75.         protected override void Update(GameTime gameTime)
  76.         {
  77.             if (Keyboard.GetState().IsKeyDown(Keys.Escape))
  78.                 this.Exit();
  79.  
  80.             keyboard = Keyboard.GetState();
  81.  
  82.             if (keyboard.IsKeyDown(Keys.Right))
  83.                 angleY++;
  84.             if (keyboard.IsKeyDown(Keys.Left))
  85.                 angleY--;
  86.             if (keyboard.IsKeyDown(Keys.Up))
  87.                 angleX--;
  88.             if (keyboard.IsKeyDown(Keys.Down))
  89.                 angleX++;
  90.             if (keyboard.IsKeyDown(Keys.Q))
  91.                 cameraDistance += 0.25f;
  92.             if (keyboard.IsKeyDown(Keys.A))
  93.                 cameraDistance -= 0.25f;
  94.             if (keyboard.IsKeyDown(Keys.B) && keyboardPrevious.IsKeyUp(Keys.B))
  95.                 backgroundActive = !backgroundActive;
  96.             if (keyboard.IsKeyDown(Keys.X) && keyboardPrevious.IsKeyUp(Keys.X))
  97.                 netActive = !netActive;
  98.  
  99.             view = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up);
  100.             cameraPosition = new Vector3(0.0f, 0.0f, cameraDistance);
  101.             view = Matrix.CreateRotationX(MathHelper.ToRadians(angleX)) * Matrix.CreateRotationY(MathHelper.ToRadians(angleY)) * view;
  102.             basicEffect.View = view;
  103.             keyboardPrevious = keyboard;
  104.             sun.Update(gameTime, view, projection);
  105.             venus.Update(gameTime, view, projection);
  106.             mercury.Update(gameTime, view, projection);
  107.             earth.Update(gameTime, view, projection);
  108.             mars.Update(gameTime, view, projection);
  109.             moon.UpdateMoon(gameTime, view, projection, earth.GetActualPosition());
  110.             base.Update(gameTime);
  111.         }
  112.  
  113.         protected override void Draw(GameTime gameTime)
  114.         {
  115.             GraphicsDevice.Clear(Color.CornflowerBlue);
  116.  
  117.             if (backgroundActive)
  118.             {
  119.                 spriteBatch.Begin();
  120.                 spriteBatch.Draw(background, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), Color.White);
  121.                 spriteBatch.End();
  122.             }
  123.             basicEffect.CurrentTechnique.Passes[0].Apply();
  124.             if (netActive) GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineList, net.GetVertices(), 0, net.GetVerticesLength() / 2);
  125.             earth.Draw();
  126.             mercury.Draw();
  127.             venus.Draw();
  128.             mars.Draw();
  129.             sun.Draw();
  130.             moon.Draw();
  131.             base.Draw(gameTime);
  132.         }
  133.     }
  134. }
  135.  
  136.  
  137. /*using System;
  138. using System.Collections.Generic;
  139. using System.Linq;
  140. using System.Text;
  141. using Microsoft.Xna.Framework.Graphics;
  142. using Microsoft.Xna.Framework;
  143.  
  144. namespace Układ_Słoneczny
  145. {
  146.     class NetGenerator
  147.     {
  148.  
  149.         VertexPositionColor[] lineVertices;
  150.         Vector2 startPosition;
  151.         Vector2 endPosition;
  152.  
  153.         public NetGenerator(Vector2 startPosition,Vector2 endPosition)
  154.         {
  155.             this.startPosition = startPosition;
  156.             this.endPosition = endPosition;
  157.             lineVertices = GenerateLines();
  158.         }
  159.  
  160.         public VertexPositionColor[] GetVertices()
  161.         {
  162.             return lineVertices;
  163.         }
  164.  
  165.         public int GetVerticesLength()
  166.         {
  167.             return lineVertices.Length;
  168.         }
  169.  
  170.  
  171.         private VertexPositionColor[] GenerateLines()
  172.         {
  173.             List<VertexPositionColor> lines = new List<VertexPositionColor>();
  174.             int count = (int)(endPosition.X - startPosition.X) / 40;
  175.             for (int i = 0; i <=40; i++)
  176.             {
  177.                 lines.Add(new VertexPositionColor(new Vector3(startPosition.X ,startPosition.Y+count*i,-4),Color.White));
  178.                 lines.Add(new VertexPositionColor(new Vector3(endPosition.X,startPosition.Y+count*i,-4),Color.White));
  179.                 lines.Add(new VertexPositionColor(new Vector3(startPosition.X + count * i, startPosition.Y, -4), Color.White));
  180.                 lines.Add(new VertexPositionColor(new Vector3(startPosition.X + count * i, endPosition.Y, -4), Color.White));
  181.             }
  182.             return lines.ToArray();
  183.         }
  184.     }
  185. }
  186. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement