Files
test/GameOfLife3D/GameOfLife.cs
2024-10-24 17:15:51 +02:00

196 lines
5.9 KiB
C#

using GameOfLife3D.GameModel;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Diagnostics;
namespace GameOfLife3D
{
public class GameOfLife : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private GameOfLifeModel _gameOfLife;
private bool paused= false;
private Model model;
Stopwatch watch = new();
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(64);
_gameOfLife.Map.SetRandom(8);
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)
{
watch.Restart();
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 (Keyboard.GetState().IsKeyDown(Keys.R))
{
_gameOfLife.Map.SetRandom(8);
}
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
Debug.WriteLine($"Logic took {watch.ElapsedMilliseconds}ms");
watch.Stop();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
watch.Restart();
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
Debug.WriteLine($"Drawing took {watch.ElapsedMilliseconds}ms");
base.Draw(gameTime);
}
protected void GeneratorCubeModel()
{
}
}
}