Added 3D Variant, still nedds improvment
This commit is contained in:
178
GameOfLife3D/GameOfLife.cs
Normal file
178
GameOfLife3D/GameOfLife.cs
Normal file
@@ -0,0 +1,178 @@
|
||||
using GameOfLife3D.GameModel;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
using System;
|
||||
|
||||
namespace GameOfLife3D
|
||||
{
|
||||
public class GameOfLife : Game
|
||||
{
|
||||
private GraphicsDeviceManager _graphics;
|
||||
private SpriteBatch _spriteBatch;
|
||||
private GameOfLifeModel _gameOfLife;
|
||||
private bool paused= false;
|
||||
private Model model;
|
||||
|
||||
Vector3 CameraTarget, CameraPosition;
|
||||
Matrix ProjectionMatrix, ViewMatrix, WorldMatrix;
|
||||
private bool orbit = false;
|
||||
Matrix TranslationMatrix;
|
||||
int tickcount = 0;
|
||||
public GameOfLife()
|
||||
{
|
||||
_graphics = new GraphicsDeviceManager(this);
|
||||
|
||||
_graphics.PreferredBackBufferWidth = 1920;
|
||||
_graphics.PreferredBackBufferHeight = 1080;
|
||||
|
||||
Content.RootDirectory = "Content";
|
||||
IsMouseVisible = true;
|
||||
}
|
||||
|
||||
protected override void Initialize()
|
||||
{
|
||||
// TODO: Add your initialization logic here
|
||||
_gameOfLife = new(16);
|
||||
_gameOfLife.Map.SetRandom(2);
|
||||
model = Content.Load<Model>("Models/Cube");
|
||||
|
||||
CameraTarget = new(0f,0f,0f);
|
||||
CameraPosition = new(0f, 0f, -30f);
|
||||
|
||||
ProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(60f), _graphics.GraphicsDevice.Viewport.AspectRatio, 1f, 1000f);
|
||||
|
||||
ViewMatrix = Matrix.CreateLookAt(CameraPosition, CameraTarget,new Vector3(0f,1f,0f));
|
||||
|
||||
WorldMatrix = Matrix.CreateWorld(CameraTarget, Vector3.Forward, Vector3.Up);
|
||||
|
||||
TranslationMatrix = Matrix.CreateTranslation(new Vector3(0f, 0f, 0f));
|
||||
|
||||
|
||||
base.Initialize();
|
||||
}
|
||||
|
||||
protected override void LoadContent()
|
||||
{
|
||||
_spriteBatch = new SpriteBatch(GraphicsDevice);
|
||||
|
||||
// TODO: use this.Content to load your game content here
|
||||
}
|
||||
|
||||
protected override void Update(GameTime gameTime)
|
||||
{
|
||||
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
|
||||
Exit();
|
||||
|
||||
if(Keyboard.GetState().IsKeyDown(Keys.P))
|
||||
{
|
||||
paused = !paused;
|
||||
}
|
||||
|
||||
|
||||
if (Keyboard.GetState().IsKeyDown(Keys.Left))
|
||||
{
|
||||
CameraPosition.X -= 0.1f;
|
||||
CameraTarget.X -= 0.1f;
|
||||
}
|
||||
if (Keyboard.GetState().IsKeyDown(Keys.Right))
|
||||
{
|
||||
CameraPosition.X += 0.1f;
|
||||
CameraTarget.X += 0.1f;
|
||||
}
|
||||
if (Keyboard.GetState().IsKeyDown(Keys.Up))
|
||||
{
|
||||
CameraPosition.Y -= 0.1f;
|
||||
CameraTarget.Y -= 0.1f;
|
||||
}
|
||||
if (Keyboard.GetState().IsKeyDown(Keys.Down))
|
||||
{
|
||||
CameraPosition.Y += 0.1f;
|
||||
CameraTarget.Y += 0.1f;
|
||||
}
|
||||
if (Keyboard.GetState().IsKeyDown(Keys.OemPlus))
|
||||
{
|
||||
CameraPosition.Z += 0.1f;
|
||||
}
|
||||
if (Keyboard.GetState().IsKeyDown(Keys.OemMinus))
|
||||
{
|
||||
CameraPosition.Z -= 0.1f;
|
||||
}
|
||||
if (Keyboard.GetState().IsKeyDown(Keys.Space))
|
||||
{
|
||||
orbit = !orbit;
|
||||
}
|
||||
|
||||
|
||||
if (orbit)
|
||||
{
|
||||
Matrix rotationMatrix = Matrix.CreateRotationY(
|
||||
MathHelper.ToRadians(1f));
|
||||
CameraPosition = Vector3.Transform(CameraPosition,
|
||||
rotationMatrix);
|
||||
}
|
||||
ViewMatrix = Matrix.CreateLookAt(CameraPosition, CameraTarget,
|
||||
Vector3.Up);
|
||||
|
||||
|
||||
TranslationMatrix = Matrix.CreateTranslation(
|
||||
new Vector3(
|
||||
0f,
|
||||
0f,
|
||||
(float)Math.Sin(gameTime.ElapsedGameTime.TotalMilliseconds)*10f));
|
||||
//WorldMatrix.Translation = TranslationMatrix.Translation;
|
||||
|
||||
tickcount++;
|
||||
|
||||
if (!paused)
|
||||
{
|
||||
if (tickcount % 20 == 0)
|
||||
{
|
||||
_gameOfLife.Update();
|
||||
}
|
||||
}
|
||||
// TODO: Add your update logic here
|
||||
|
||||
base.Update(gameTime);
|
||||
|
||||
}
|
||||
|
||||
protected override void Draw(GameTime gameTime)
|
||||
{
|
||||
GraphicsDevice.Clear(Color.Black);
|
||||
foreach (var cell in _gameOfLife.Map.Cells)
|
||||
{
|
||||
|
||||
foreach (ModelMesh mesh in model.Meshes)
|
||||
{
|
||||
foreach (BasicEffect effect in mesh.Effects)
|
||||
{
|
||||
|
||||
effect.EnableDefaultLighting();
|
||||
effect.AmbientLightColor = cell.isAlive ? Color.AliceBlue.ToVector3() : Color.Green.ToVector3();
|
||||
effect.View = ViewMatrix;
|
||||
effect.World = Matrix.CreateTranslation(cell.X*2.1f, cell.Y*2.1f, cell.Z * 2.1f);
|
||||
effect.Projection = ProjectionMatrix;
|
||||
|
||||
}
|
||||
|
||||
if (cell.isAlive)
|
||||
{
|
||||
mesh.Draw();
|
||||
}
|
||||
}
|
||||
|
||||
//model.Draw(WorldMatrix * Matrix.CreateTranslation(new Vector3(cell.X, cell.Y, cell.Z)), ViewMatrix, ProjectionMatrix);
|
||||
}
|
||||
|
||||
// TODO: Add your drawing code here
|
||||
|
||||
base.Draw(gameTime);
|
||||
}
|
||||
|
||||
protected void GeneratorCubeModel()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user