Basic component system

This commit is contained in:
2012-09-19 12:07:22 +02:00
parent 6dffd04627
commit 23a6e7a951
12 changed files with 195 additions and 2 deletions
+17
View File
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DepthsBelow
{
public class Component
{
public Entity Parent;
public Component(Entity parent)
{
Parent = parent;
}
}
}
+23 -2
View File
@@ -18,6 +18,7 @@
<XnaOutputType>Game</XnaOutputType>
<ApplicationIcon>Game.ico</ApplicationIcon>
<Thumbnail>GameThumbnail.png</Thumbnail>
<IsWebBootstrapper>false</IsWebBootstrapper>
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
@@ -28,10 +29,10 @@
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationRevision>1</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<PublishWizardCompleted>true</PublishWizardCompleted>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
@@ -59,6 +60,18 @@
<PlatformTarget>x86</PlatformTarget>
<XnaCompressContent>true</XnaCompressContent>
</PropertyGroup>
<PropertyGroup>
<ManifestCertificateThumbprint>ECE66DEF071576E8D2F7D57232C0569623E3E2DF</ManifestCertificateThumbprint>
</PropertyGroup>
<PropertyGroup>
<ManifestKeyFile>DepthsBelow_TemporaryKey.pfx</ManifestKeyFile>
</PropertyGroup>
<PropertyGroup>
<GenerateManifests>true</GenerateManifests>
</PropertyGroup>
<PropertyGroup>
<SignManifests>true</SignManifests>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Xna.Framework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553, processorArchitecture=x86">
<Private>False</Private>
@@ -107,9 +120,14 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Component.cs" />
<Compile Include="Entity.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Program.cs" />
<Compile Include="Game1.cs" />
<Compile Include="RenderComponent.cs" />
<Compile Include="Soldier.cs" />
<Compile Include="TransformComponent.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Game.ico" />
@@ -148,6 +166,9 @@
<Install>true</Install>
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<None Include="DepthsBelow_TemporaryKey.pfx" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\XNA Game Studio\Microsoft.Xna.GameStudio.targets" />
<!--
@@ -0,0 +1 @@
Content\images\soldier.xnb
@@ -10,4 +10,7 @@
<FallbackCulture>en-US</FallbackCulture>
<VerifyUploadedFiles>false</VerifyUploadedFiles>
</PropertyGroup>
<PropertyGroup>
<EnableSecurityDebugging>false</EnableSecurityDebugging>
</PropertyGroup>
</Project>
Binary file not shown.
+33
View File
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DepthsBelow
{
public class Entity
{
List<Component> Components;
public Entity()
{
Components = new List<Component>();
}
public T GetComponent<T>() where T : Component
{
foreach (Component c in Components)
{
if (c is T)
return (T)c;
}
return null;
}
public void AddComponent(Component c)
{
Components.Add(c);
}
}
}
+18
View File
@@ -19,10 +19,19 @@ namespace DepthsBelow
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Soldier soldier;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferWidth = 1280;
graphics.PreferredBackBufferHeight = 720;
graphics.IsFullScreen = true;
this.IsMouseVisible = true;
}
/// <summary>
@@ -48,6 +57,9 @@ namespace DepthsBelow
spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
Soldier.LoadContent(this);
soldier = new Soldier(this);
}
/// <summary>
@@ -71,6 +83,7 @@ namespace DepthsBelow
this.Exit();
// TODO: Add your update logic here
soldier.Update(gameTime);
base.Update(gameTime);
}
@@ -84,6 +97,11 @@ namespace DepthsBelow
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
spriteBatch.Begin();
SpriteRenderComponent rc = soldier.GetComponent<SpriteRenderComponent>();
if (rc != null)
rc.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace DepthsBelow
{
public class SpriteRenderComponent : Component
{
public Texture2D Texture;
public SpriteRenderComponent(Entity parent) : base(parent)
{
}
public void Draw(SpriteBatch spriteBatch)
{
TransformComponent tc = this.Parent.GetComponent<TransformComponent>();
spriteBatch.Draw(Texture, tc.Position, Color.White);
}
}
}
+46
View File
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace DepthsBelow
{
class Soldier : Entity
{
Game1 game;
static Texture2D Texture;
// Components
TransformComponent transform;
static public void LoadContent(Game1 game)
{
Texture = game.Content.Load<Texture2D>("images/soldier");
}
public Soldier(Game1 game)
{
if (Texture == null)
LoadContent(game);
SpriteRenderComponent rc = new SpriteRenderComponent(this);
rc.Texture = Texture;
this.AddComponent(rc);
transform = new TransformComponent(this);
this.AddComponent(transform);
}
public void Update(GameTime gameTime)
{
KeyboardState ks = Keyboard.GetState();
if (ks.IsKeyDown(Keys.Left))
transform.Position.X--;
}
}
}
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
namespace DepthsBelow
{
class TransformComponent : Component
{
public Vector2 Position;
public float Rotation;
public Point Origin;
public TransformComponent(Entity parent) : base(parent)
{
Position = Vector2.Zero;
Rotation = 0.0f;
Origin = Point.Zero;
}
}
}
@@ -41,6 +41,13 @@
<Private>False</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="images\soldier.bmp">
<Name>soldier</Name>
<Importer>TextureImporter</Importer>
<Processor>TextureProcessor</Processor>
</Compile>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\XNA Game Studio\$(XnaFrameworkVersion)\Microsoft.Xna.GameStudio.ContentPipeline.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.
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB