source: 2011/23/sijoseha/SpaceWorld/SpaceWorld/SpaceWorld/Game1.cs @ 10337

Revision 2118, 6.5 KB checked in by sijoseha, 9 years ago (diff)

Some modifications. Nothing is really happening yet.

Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using Microsoft.Xna.Framework;
5using Microsoft.Xna.Framework.Audio;
6using Microsoft.Xna.Framework.Content;
7using Microsoft.Xna.Framework.GamerServices;
8using Microsoft.Xna.Framework.Graphics;
9using Microsoft.Xna.Framework.Input;
10using Microsoft.Xna.Framework.Media;
11
12namespace SpaceWorld
13{
14    /// <summary>
15    /// This is the main type for your game
16    /// </summary>
17    public class SpaceWorld : Microsoft.Xna.Framework.Game
18    {
19        GraphicsDeviceManager graphics;
20        GraphicsDevice device;
21        SpriteBatch spriteBatch;
22
23        List<WorldPoint> points;
24        Shield shield;
25        Texture2D shieldKuva;
26        //WorldPoint point, point2, point3, point4;
27        BasicEffect effect;
28
29        Matrix viewMatrix, projectionMatrix;
30
31        public SpaceWorld()
32        {
33            graphics = new GraphicsDeviceManager(this);
34            Content.RootDirectory = "Content";
35        }
36
37        /// <summary>
38        /// Allows the game to perform any initialization it needs to before starting to run.
39        /// This is where it can query for any required services and load any non-graphic
40        /// related content.  Calling base.Initialize will enumerate through any components
41        /// and initialize them as well.
42        /// </summary>
43        protected override void Initialize()
44        {
45            IsMouseVisible = true;
46            base.Initialize();
47        }
48
49        /// <summary>
50        /// LoadContent will be called once per game and is the place to load
51        /// all of your content.
52        /// </summary>
53        protected override void LoadContent()
54        {
55            // Create a new SpriteBatch, which can be used to draw textures.
56            device = graphics.GraphicsDevice;
57            spriteBatch = new SpriteBatch(GraphicsDevice);
58
59            effect = new BasicEffect(device);
60
61            points = new List<WorldPoint>();
62            shieldKuva = Content.Load<Texture2D>("shieldImage");
63            shield = new Shield();
64            shield.shieldTexture = shieldKuva;
65           
66
67            Random r = new Random();
68            /*for (int i = 0; i < 10; i++)
69            {
70                WorldPoint point = new WorldPoint(new Vector2((float)r.NextDouble() * 400 - 200, (float)r.NextDouble() * 200 - 100));
71                points.Add(point);
72            }*/
73
74            WorldPoint point = new WorldPoint(new Vector2(-10, 0));
75            points.Add(point);
76            point = new WorldPoint(new Vector2(10, 0));
77            points.Add(point);
78
79            projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 1.0f, 300.0f);
80            //projectionMatrix = Matrix.CreateOrthographic(device.Viewport.Width, device.Viewport.Height, 0.1f, 100);
81            viewMatrix = Matrix.CreateLookAt(new Vector3(0, 0, 20), Vector3.Zero, Vector3.Up);
82            /*viewMatrix = Matrix.CreateTranslation(new Vector3(0, 0, 0)) *
83                Matrix.CreateScale(1) *
84                Matrix.CreateTranslation(new Vector3(
85                    device.Viewport.Width * 0.5f,
86                    device.Viewport.Height * 0.5f,
87                    0));*/
88            // TODO: use this.Content to load your game content here
89        }
90
91        /// <summary>
92        /// UnloadContent will be called once per game and is the place to unload
93        /// all content.
94        /// </summary>
95        protected override void UnloadContent()
96        {
97            // TODO: Unload any non ContentManager content here
98        }
99
100        /// <summary>
101        /// Allows the game to run logic such as updating the world,
102        /// checking for collisions, gathering input, and playing audio.
103        /// </summary>
104        /// <param name="gameTime">Provides a snapshot of timing values.</param>
105        protected override void Update(GameTime gameTime)
106        {
107            // Allows the game to exit
108            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
109                this.Exit();
110
111            MouseState ms = Mouse.GetState();
112           
113            Vector2 mPos = new Vector2(
114                (float)(ms.X - device.Viewport.Width/2)/(20*device.Viewport.AspectRatio),
115                -(float)(ms.Y - device.Viewport.Height / 2) / (20 * device.Viewport.AspectRatio));
116           
117            /*if (Vector2.Distance(mPos, shield.Position) < 3.0f)
118                for (int i = 0; i < shield.vertices.Length; i++)
119                {
120                    if (Vector2.Distance(Vector3to2(shield.vertices[i].Position), mPos) < 0.1f)
121                       
122                }*/
123            // TODO: Add your update logic here
124
125            base.Update(gameTime);
126        }
127
128        /// <summary>
129        /// This is called when the game should draw itself.
130        /// </summary>
131        /// <param name="gameTime">Provides a snapshot of timing values.</param>
132        protected override void Draw(GameTime gameTime)
133        {
134            GraphicsDevice.Clear(Color.Black);
135            RasterizerState rs = new RasterizerState();
136            rs.FillMode = FillMode.WireFrame;
137            SamplerState ss = new SamplerState();
138            ss.AddressW = TextureAddressMode.Clamp;
139            ss.AddressU = TextureAddressMode.Clamp;
140            ss.AddressV = TextureAddressMode.Clamp;
141            device.SamplerStates[0] = ss;
142            //device.RasterizerState = rs;
143            effect.LightingEnabled = false;
144           
145            effect.TextureEnabled = false;
146            //effect.Texture = shieldKuva;
147            effect.Projection = projectionMatrix;
148            effect.VertexColorEnabled = true;
149            effect.View = viewMatrix;
150            effect.World = Matrix.Identity;
151            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
152            {
153                pass.Apply();
154                foreach (WorldPoint point in points)
155                {
156                    point.Draw(device);
157                }
158               
159            }
160            effect.TextureEnabled = true;
161            effect.VertexColorEnabled = false;
162            effect.EnableDefaultLighting();
163            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
164            {
165                pass.Apply();
166                shield.Draw(device);
167            }
168            base.Draw(gameTime);
169        }
170
171        private Vector2 Vector3to2(Vector3 from)
172        {
173            return new Vector2(from.X, from.Y);
174        }
175    }
176}
Note: See TracBrowser for help on using the repository browser.