Browse Source

Added "Tutorial022"

Niall Lewin 7 years ago
parent
commit
a04fac1ea6

+ 6 - 0
MonoGame_Tutorials/MonoGame_Tutorials.sln

@@ -33,6 +33,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tutorial018", "Tutorial018\
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tutorial019", "Tutorial019\Tutorial019.csproj", "{A2C150C7-8116-4F2E-AB0E-1E0FF3CDB34E}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tutorial022", "Tutorial022\Tutorial022.csproj", "{E3428A87-E72B-42CC-AC12-6C97BA5A957D}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|x86 = Debug|x86
@@ -99,6 +101,10 @@ Global
 		{A2C150C7-8116-4F2E-AB0E-1E0FF3CDB34E}.Debug|x86.Build.0 = Debug|x86
 		{A2C150C7-8116-4F2E-AB0E-1E0FF3CDB34E}.Release|x86.ActiveCfg = Release|x86
 		{A2C150C7-8116-4F2E-AB0E-1E0FF3CDB34E}.Release|x86.Build.0 = Release|x86
+		{E3428A87-E72B-42CC-AC12-6C97BA5A957D}.Debug|x86.ActiveCfg = Debug|x86
+		{E3428A87-E72B-42CC-AC12-6C97BA5A957D}.Debug|x86.Build.0 = Debug|x86
+		{E3428A87-E72B-42CC-AC12-6C97BA5A957D}.Release|x86.ActiveCfg = Release|x86
+		{E3428A87-E72B-42CC-AC12-6C97BA5A957D}.Release|x86.Build.0 = Release|x86
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 17 - 0
MonoGame_Tutorials/Tutorial022/Component.cs

@@ -0,0 +1,17 @@
+using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Graphics;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Tutorial022
+{
+  public abstract class Component
+  {
+    public abstract void Draw(GameTime gameTime, SpriteBatch spriteBatch);
+
+    public abstract void Update(GameTime gameTime);
+  }
+}

+ 27 - 0
MonoGame_Tutorials/Tutorial022/Content/Content.mgcb

@@ -0,0 +1,27 @@
+
+#----------------------------- Global Properties ----------------------------#
+
+/outputDir:bin/$(Platform)
+/intermediateDir:obj/$(Platform)
+/platform:Windows
+/config:
+/profile:Reach
+/compress:False
+
+#-------------------------------- References --------------------------------#
+
+
+#---------------------------------- Content ---------------------------------#
+
+#begin Square.png
+/importer:TextureImporter
+/processor:TextureProcessor
+/processorParam:ColorKeyColor=255,0,255,255
+/processorParam:ColorKeyEnabled=True
+/processorParam:GenerateMipmaps=False
+/processorParam:PremultiplyAlpha=True
+/processorParam:ResizeToPowerOfTwo=False
+/processorParam:MakeSquare=False
+/processorParam:TextureFormat=Color
+/build:Square.png
+

BIN
MonoGame_Tutorials/Tutorial022/Content/Square.png


BIN
MonoGame_Tutorials/Tutorial022/Content/Thumbs.db


BIN
MonoGame_Tutorials/Tutorial022/Content/bin/Windows/Square.xnb


+ 42 - 0
MonoGame_Tutorials/Tutorial022/Content/obj/Windows/Square.mgcontent

@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="utf-8"?>
+<PipelineBuildEvent xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+  <SourceFile>C:/Users/nlewin/source/repos/MonoGame_Tutorials/Tutorial022/Content/Square.png</SourceFile>
+  <SourceTime>2018-06-08T13:38:14.2437008+01:00</SourceTime>
+  <DestFile>C:/Users/nlewin/source/repos/MonoGame_Tutorials/Tutorial022/Content/bin/Windows/Square.xnb</DestFile>
+  <DestTime>2018-06-08T13:38:18.5751339+01:00</DestTime>
+  <Importer>TextureImporter</Importer>
+  <ImporterTime>2017-03-01T15:05:36+00:00</ImporterTime>
+  <Processor>TextureProcessor</Processor>
+  <ProcessorTime>2017-03-01T15:05:36+00:00</ProcessorTime>
+  <Parameters>
+    <Key>ColorKeyColor</Key>
+    <Value>255,0,255,255</Value>
+  </Parameters>
+  <Parameters>
+    <Key>ColorKeyEnabled</Key>
+    <Value>True</Value>
+  </Parameters>
+  <Parameters>
+    <Key>GenerateMipmaps</Key>
+    <Value>False</Value>
+  </Parameters>
+  <Parameters>
+    <Key>PremultiplyAlpha</Key>
+    <Value>True</Value>
+  </Parameters>
+  <Parameters>
+    <Key>ResizeToPowerOfTwo</Key>
+    <Value>False</Value>
+  </Parameters>
+  <Parameters>
+    <Key>MakeSquare</Key>
+    <Value>False</Value>
+  </Parameters>
+  <Parameters>
+    <Key>TextureFormat</Key>
+    <Value>Color</Value>
+  </Parameters>
+  <Dependencies />
+  <BuildAsset />
+  <BuildOutput />
+</PipelineBuildEvent>

+ 113 - 0
MonoGame_Tutorials/Tutorial022/Game1.cs

@@ -0,0 +1,113 @@
+using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Graphics;
+using Microsoft.Xna.Framework.Input;
+using System.Collections.Generic;
+using Tutorial022.Sprites;
+
+namespace Tutorial022
+{
+  /// <summary>
+  /// This is the main type for your game.
+  /// </summary>
+  public class Game1 : Game
+  {
+    GraphicsDeviceManager graphics;
+    SpriteBatch spriteBatch;
+
+    private List<Sprite> _sprites;
+
+    public Game1()
+    {
+      graphics = new GraphicsDeviceManager(this);
+      Content.RootDirectory = "Content";
+    }
+
+    /// <summary>
+    /// Allows the game to perform any initialization it needs to before starting to run.
+    /// This is where it can query for any required services and load any non-graphic
+    /// related content.  Calling base.Initialize will enumerate through any components
+    /// and initialize them as well.
+    /// </summary>
+    protected override void Initialize()
+    {
+      // TODO: Add your initialization logic here
+
+      base.Initialize();
+    }
+
+    /// <summary>
+    /// LoadContent will be called once per game and is the place to load
+    /// all of your content.
+    /// </summary>
+    protected override void LoadContent()
+    {
+      // Create a new SpriteBatch, which can be used to draw textures.
+      spriteBatch = new SpriteBatch(GraphicsDevice);
+
+      var texture = Content.Load<Texture2D>("Square");
+
+      var player = new Player(texture)
+      {
+        Colour = Color.Green,
+        Position = new Vector2(100, 100),
+      };
+
+      _sprites = new List<Sprite>()
+      {
+        player,
+        new Sprite(texture)
+        {
+          Colour = Color.Blue,
+          Position = new Vector2(200, 200),
+        }.SetFollowTarget(player, 75f),
+        new Sprite(texture)
+        {
+          Colour = Color.Orange,
+          Position = new Vector2(400, 200),
+          FollowTarget = player,
+          FollowDistance = 150f,
+        }
+      };
+    }
+
+    /// <summary>
+    /// UnloadContent will be called once per game and is the place to unload
+    /// game-specific content.
+    /// </summary>
+    protected override void UnloadContent()
+    {
+      // TODO: Unload any non ContentManager content here
+    }
+
+    /// <summary>
+    /// Allows the game to run logic such as updating the world,
+    /// checking for collisions, gathering input, and playing audio.
+    /// </summary>
+    /// <param name="gameTime">Provides a snapshot of timing values.</param>
+    protected override void Update(GameTime gameTime)
+    {
+      foreach (var sprite in _sprites)
+        sprite.Update(gameTime);
+
+      base.Update(gameTime);
+    }
+
+    /// <summary>
+    /// This is called when the game should draw itself.
+    /// </summary>
+    /// <param name="gameTime">Provides a snapshot of timing values.</param>
+    protected override void Draw(GameTime gameTime)
+    {
+      GraphicsDevice.Clear(Color.CornflowerBlue);
+
+      spriteBatch.Begin();
+
+      foreach (var sprite in _sprites)
+        sprite.Draw(gameTime, spriteBatch);
+
+      spriteBatch.End();
+
+      base.Draw(gameTime);
+    }
+  }
+}

BIN
MonoGame_Tutorials/Tutorial022/Icon.ico


+ 22 - 0
MonoGame_Tutorials/Tutorial022/Program.cs

@@ -0,0 +1,22 @@
+using System;
+
+namespace Tutorial022
+{
+#if WINDOWS || LINUX
+    /// <summary>
+    /// The main class.
+    /// </summary>
+    public static class Program
+    {
+        /// <summary>
+        /// The main entry point for the application.
+        /// </summary>
+        [STAThread]
+        static void Main()
+        {
+            using (var game = new Game1())
+                game.Run();
+        }
+    }
+#endif
+}

+ 36 - 0
MonoGame_Tutorials/Tutorial022/Properties/AssemblyInfo.cs

@@ -0,0 +1,36 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following 
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("Tutorial022")]
+[assembly: AssemblyProduct("Tutorial022")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyCopyright("")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible 
+// to COM components.  If you need to access a type in this assembly from 
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("62785e6b-454b-48b6-bac4-6b62c8cfbd82")]
+
+// Version information for an assembly consists of the following four values:
+//
+//      Major Version
+//      Minor Version 
+//      Build Number
+//      Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers 
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]

+ 36 - 0
MonoGame_Tutorials/Tutorial022/Sprites/Player.cs

@@ -0,0 +1,36 @@
+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 Microsoft.Xna.Framework.Input;
+
+namespace Tutorial022.Sprites
+{
+  public class Player : Sprite
+  {
+    public Player(Texture2D texture)
+      : base(texture)
+    {
+    }
+
+    public override void Update(GameTime gameTime)
+    {
+      var velocity = Vector2.Zero;
+
+      if (Keyboard.GetState().IsKeyDown(Keys.W))
+        velocity.Y = -LinearVelocity;
+      else if (Keyboard.GetState().IsKeyDown(Keys.S))
+        velocity.Y = LinearVelocity;
+
+      if (Keyboard.GetState().IsKeyDown(Keys.A))
+        velocity.X = -LinearVelocity;
+      else if (Keyboard.GetState().IsKeyDown(Keys.D))
+        velocity.X = LinearVelocity;
+
+      Position += velocity;
+    }
+  }
+}

+ 141 - 0
MonoGame_Tutorials/Tutorial022/Sprites/Sprite.cs

@@ -0,0 +1,141 @@
+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 Tutorial022.Sprites
+{
+  public class Sprite : Component
+  {
+    protected float _layer { get; set; }
+
+    protected Vector2 _origin { get; set; }
+
+    protected Vector2 _position { get; set; }
+
+    protected float _rotation { get; set; }
+
+    protected Texture2D _texture;
+
+    public Color Colour { get; set; }
+
+    /// <summary>
+    /// The sprite that we want to follow
+    /// </summary>
+    public Sprite FollowTarget { get; set; }
+
+    /// <summary>
+    /// How close we want to be to our target
+    /// </summary>
+    public float FollowDistance { get; set; }
+
+    public bool IsRemoved { get; set; }
+
+    public Vector2 Direction;
+
+    public float RotationVelocity = 3f;
+
+    public float LinearVelocity = 4f;
+
+    public float Layer
+    {
+      get { return _layer; }
+      set
+      {
+        _layer = value;
+      }
+    }
+
+    public Vector2 Origin
+    {
+      get { return _origin; }
+      set
+      {
+        _origin = value;
+      }
+    }
+
+    public Vector2 Position
+    {
+      get { return _position; }
+      set
+      {
+        _position = value;
+      }
+    }
+
+    public Rectangle Rectangle
+    {
+      get
+      {
+        return new Rectangle((int)Position.X - (int)Origin.X, (int)Position.Y - (int)Origin.Y, _texture.Width, _texture.Height);
+      }
+    }
+
+    public float Rotation
+    {
+      get { return _rotation; }
+      set
+      {
+        _rotation = value;
+      }
+    }
+
+    public Sprite(Texture2D texture)
+    {
+      _texture = texture;
+
+      Origin = new Vector2(_texture.Width / 2, _texture.Height / 2);
+
+      Colour = Color.White;
+    }
+
+    public override void Update(GameTime gameTime)
+    {
+      Follow();
+    }
+
+    protected void Follow()
+    {
+      if (FollowTarget == null)
+        return;
+
+      var distance = FollowTarget.Position - this.Position;
+      _rotation = (float)Math.Atan2(distance.Y, distance.X);
+
+      Direction = new Vector2((float)Math.Cos(_rotation), (float)Math.Sin(_rotation));
+
+      var currentDistance = Vector2.Distance(this.Position, FollowTarget.Position);
+      if (currentDistance > FollowDistance)
+      {
+        var t = MathHelper.Min((float)Math.Abs(currentDistance - FollowDistance), LinearVelocity);
+        var velocity = Direction * t;
+
+        Position += velocity;
+      }
+    }
+
+    public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
+    {
+      spriteBatch.Draw(_texture, Position, null, Colour, _rotation, Origin, 1f, SpriteEffects.None, Layer);
+    }
+
+    /// <summary>
+    /// Once the follow target has been set, the sprite will follow the target
+    /// </summary>
+    /// <param name="followTarget">The sprite we want to follow</param>
+    /// <param name="followDistance">how close we'll get to our target</param>
+    /// <returns></returns>
+    public Sprite SetFollowTarget(Sprite followTarget, float followDistance)
+    {
+      FollowTarget = followTarget;
+
+      FollowDistance = followDistance;
+
+      return this;
+    }
+  }
+}

+ 77 - 0
MonoGame_Tutorials/Tutorial022/Tutorial022.csproj

@@ -0,0 +1,77 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="$(MSBuildExtensionsPath)\MonoGame\v3.0\MonoGame.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\MonoGame\v3.0\MonoGame.Common.props')" />
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
+    <ProductVersion>8.0.30703</ProductVersion>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{E3428A87-E72B-42CC-AC12-6C97BA5A957D}</ProjectGuid>
+    <OutputType>WinExe</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <RootNamespace>Tutorial022</RootNamespace>
+    <AssemblyName>Tutorial022</AssemblyName>
+    <FileAlignment>512</FileAlignment>
+    <MonoGamePlatform>Windows</MonoGamePlatform>
+    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
+    <PlatformTarget>x86</PlatformTarget>
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\$(MonoGamePlatform)\$(Platform)\$(Configuration)\</OutputPath>
+    <DefineConstants>DEBUG;TRACE;WINDOWS</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
+    <PlatformTarget>x86</PlatformTarget>
+    <DebugType>pdbonly</DebugType>
+    <Optimize>true</Optimize>
+    <OutputPath>bin\$(MonoGamePlatform)\$(Platform)\$(Configuration)\</OutputPath>
+    <DefineConstants>TRACE;WINDOWS</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <PropertyGroup>
+    <ApplicationIcon>Icon.ico</ApplicationIcon>
+  </PropertyGroup>
+  <PropertyGroup>
+    <ApplicationManifest>app.manifest</ApplicationManifest>
+  </PropertyGroup>
+  <PropertyGroup>
+    <StartupObject />
+  </PropertyGroup>
+  <ItemGroup>
+    <Compile Include="Component.cs" />
+    <Compile Include="Game1.cs" />
+    <Compile Include="Program.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="Sprites\Player.cs" />
+    <Compile Include="Sprites\Sprite.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <Reference Include="MonoGame.Framework">
+      <HintPath>$(MonoGameInstallDirectory)\MonoGame\v3.0\Assemblies\Windows\MonoGame.Framework.dll</HintPath>
+    </Reference>
+    <Reference Include="System" />
+    <Reference Include="System.Xml" />
+  </ItemGroup>
+  <ItemGroup>
+    <Content Include="Icon.ico" />
+  </ItemGroup>
+  <ItemGroup>
+    <MonoGameContentReference Include="Content\Content.mgcb" />
+    <None Include="app.manifest" />
+  </ItemGroup>
+  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <Import Project="$(MSBuildExtensionsPath)\MonoGame\v3.0\MonoGame.Content.Builder.targets" />
+  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
+       Other similar extension points exist, see Microsoft.Common.targets.
+  <Target Name="BeforeBuild">
+  </Target>
+  <Target Name="AfterBuild">
+  </Target>
+  -->
+</Project>

+ 42 - 0
MonoGame_Tutorials/Tutorial022/app.manifest

@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="utf-8"?>
+<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
+  <assemblyIdentity version="1.0.0.0" name="Tutorial022"/>
+  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
+    <security>
+      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
+        <requestedExecutionLevel  level="asInvoker" uiAccess="false" />
+      </requestedPrivileges>
+    </security>
+  </trustInfo>
+
+  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
+    <application>
+      <!-- A list of the Windows versions that this application has been tested on and is
+           is designed to work with. Uncomment the appropriate elements and Windows will 
+           automatically selected the most compatible environment. -->
+
+      <!-- Windows Vista -->
+      <supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />
+
+      <!-- Windows 7 -->
+      <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />
+
+      <!-- Windows 8 -->
+      <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />
+
+      <!-- Windows 8.1 -->
+      <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />
+
+      <!-- Windows 10 -->
+      <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
+
+    </application>
+  </compatibility>
+
+  <application xmlns="urn:schemas-microsoft-com:asm.v3">
+    <windowsSettings>
+      <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/pm</dpiAware>
+    </windowsSettings>
+  </application>
+
+</assembly>