<commit_before>﻿/*
*  This Source Code Form is subject to the terms of the Mozilla Public
*  License, v. 2.0. If a copy of the MPL was not distributed with this
*  file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
*  EmpireSharp (c) <NAME> 2013 (www.simonmoles.com)
*
*/

using System;
using EmpireSharp.Game.Framework.Services;
using EmpireSharp.Simulation;
using EmpireSharp.Simulation.Commands;
using EmpireSharp.Simulation.Entities;
using FixMath.NET;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Ninject;
using Papyrus;

namespace EmpireSharp.Game.Modules.MonoGame.GameStates
{

	public class GameStateMain
	{


		private Simulation.Root _simulation;

		private TerrainRenderer _terrainRenderer;

		private Camera _camera;

		[Inject]
		IContentService Content { get; set; }

		[Inject]
		IShell Shell { get; set; }

		[Inject]
		IInputService Input { get; set; }

		private SpriteFont _debugFont;

		private Vector2 _mouseSimPos;

		[Inject]
		public GameStateMain(IContentService content, IKernel ioc)
		{

			_simulation = new Root();
			_simulation.Init(content.Database);

			_terrainRenderer = ioc.Get<TerrainRenderer>();
			_terrainRenderer.Init(_simulation.Terrain);

			_camera = new Camera();
			_camera.Zoom = 1;
			_camera.Rebuild();

			_debugFont = content.GetFont("Assets/Fonts/Orbitron");

		}

		private bool _prevPressed;

		public void Update(float dt)
		{

			_camera.Screen = new Rectangle(0, 0, Shell.Width, Shell.Height);

			var mouseState = Mouse.GetState();
			var keyState = Keyboard.GetState();

			_mouseSimPos = Translate.WorldPointToSimulation(_camera.TransformScreenToWorld(new Vector2(mouseState.X, mouseState.Y)));

			if (mouseState.LeftButton == ButtonState.Pressed) {

				if (!_prevPressed) {


					var simPoint = _mouseSimPos;

					_simulation.QueueCommand(new MoveCommand(1, 0, new FixedVector2((Fix16)simPoint.X, (Fix16)simPoint.Y)));
					_prevPressed = true;


				}

			} else {

				_prevPressed = false;

			}

			Vector2 cameraMoveDirection = Vector2.Zero;

			const float scrollSpeed = 9;

			if (Input.IsKeyDown(Keys.Left)) {
				cameraMoveDirection.X += 1;
			}
			if (Input.IsKeyDown(Keys.Right)) {
				cameraMoveDirection.X -= 1;
			}	
			if (Input.IsKeyDown(Keys.Up)) {
				cameraMoveDirection.Y += 1;
			}
			if (Input.IsKeyDown(Keys.Down)) {
				cameraMoveDirection.Y -= 1;
			}

			if (cameraMoveDirection.LengthSquared() > 0) {

				cameraMoveDirection.Normalize();
				cameraMoveDirection.Y *= 2;

				var simDirection = Translate.WorldDirectionToSimulation(cameraMoveDirection);

				_camera.SimulationPosition += simDirection * dt * scrollSpeed;

			}

			_camera.Zoom -= Input.MouseWheelDelta * dt * 0.01f;

			_simulation.Tick();

		}

		public void Draw()
		{

			var game = Shell as Shell;

			game.GraphicsDevice.Clear(Color.Black);

			_terrainRenderer.Draw(_camera);

			var entities = _simulation.EntityContainer.Entities;

			game.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null, null, _camera.Transform);

			foreach (var baseEntity in entities) {

				if (baseEntity is Unit) {

					var unit = baseEntity as Unit;

					var pos = new Vector2((float)unit.Transform.Position.X, (float)unit.Transform.Position.Y);

					pos = Translate.SimulationPointToWorld(pos);

					game.SpriteBatch.Draw(game.WhitePixelTex,
					                      new Rectangle((int) pos.X - 30,
					                                    (int) pos.Y - 30, 60, 60), new Rectangle(0, 0, 1, 1),
					                      Color.Red);

				}
			}

			game.SpriteBatch.End();

			var entity0 = entities[0] as Unit;

			game.SpriteBatch.Begin();

			var debugString = String.Format("{0,-25} {1,-25}", 
				string.Format("Camera Pos: {0}", _camera.SimulationPosition.ShortString()),
				string.Format("Mouse Pos: {0}", _mouseSimPos.ShortString()),
				entity0.Transform.Position.ShortString(), _camera.Zoom.ToString("0.00"));

			game.SpriteBatch.DrawString(_debugFont, string.Format("Camera Pos: {0}", _camera.SimulationPosition.ShortString()), new Vector2(10, 10), Color.White);
			game.SpriteBatch.DrawString(_debugFont, string.Format("Mouse Pos: {0}", _mouseSimPos.ShortString()), new Vector2(260, 10), Color.White);
			game.SpriteBatch.DrawString(_debugFont, string.Format("Zoom: {0}", _camera.Zoom.ToString("0.00")), new Vector2(500, 10), Color.White);
			game.SpriteBatch.DrawString(_debugFont, string.Format("Entity0: {0}", entity0.Transform.Position.ShortString()), new Vector2(10, 30), Color.White);

			game.SpriteBatch.End();

		}

	}

}
<commit_msg>Smooth zooming<commit_after>﻿/*
*  This Source Code Form is subject to the terms of the Mozilla Public
*  License, v. 2.0. If a copy of the MPL was not distributed with this
*  file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
*  EmpireSharp (c) <NAME> 2013 (www.simonmoles.com)
*
*/

using System;
using EmpireSharp.Game.Framework.Services;
using EmpireSharp.Simulation;
using EmpireSharp.Simulation.Commands;
using EmpireSharp.Simulation.Entities;
using FixMath.NET;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Ninject;
using Papyrus;

namespace EmpireSharp.Game.Modules.MonoGame.GameStates
{

	public class GameStateMain
	{


		private Simulation.Root _simulation;

		private TerrainRenderer _terrainRenderer;

		private Camera _camera;

		[Inject]
		IContentService Content { get; set; }

		[Inject]
		IShell Shell { get; set; }

		[Inject]
		IInputService Input { get; set; }

		private SpriteFont _debugFont;

		private Vector2 _mouseSimPos;

		private float _targetZoom;

		[Inject]
		public GameStateMain(IContentService content, IKernel ioc)
		{

			_simulation = new Root();
			_simulation.Init(content.Database);

			_terrainRenderer = ioc.Get<TerrainRenderer>();
			_terrainRenderer.Init(_simulation.Terrain);

			_camera = new Camera();
			_camera.Zoom = 1;
			_targetZoom = 1;
			_camera.Rebuild();

			_debugFont = content.GetFont("Assets/Fonts/Orbitron");

		}

		private bool _prevPressed;

		public void Update(float dt)
		{

			_camera.Screen = new Rectangle(0, 0, Shell.Width, Shell.Height);

			var mouseState = Mouse.GetState();
			var keyState = Keyboard.GetState();

			_mouseSimPos = Translate.WorldPointToSimulation(_camera.TransformScreenToWorld(new Vector2(mouseState.X, mouseState.Y)));

			if (mouseState.LeftButton == ButtonState.Pressed) {

				if (!_prevPressed) {


					var simPoint = _mouseSimPos;

					_simulation.QueueCommand(new MoveCommand(1, 0, new FixedVector2((Fix16)simPoint.X, (Fix16)simPoint.Y)));
					_prevPressed = true;


				}

			} else {

				_prevPressed = false;

			}

			Vector2 cameraMoveDirection = Vector2.Zero;

			const float scrollSpeed = 9;

			if (Input.IsKeyDown(Keys.Left)) {
				cameraMoveDirection.X += 1;
			}
			if (Input.IsKeyDown(Keys.Right)) {
				cameraMoveDirection.X -= 1;
			}	
			if (Input.IsKeyDown(Keys.Up)) {
				cameraMoveDirection.Y += 1;
			}
			if (Input.IsKeyDown(Keys.Down)) {
				cameraMoveDirection.Y -= 1;
			}

			if (cameraMoveDirection.LengthSquared() > 0) {

				cameraMoveDirection.Normalize();
				cameraMoveDirection.Y *= 2;

				var simDirection = Translate.WorldDirectionToSimulation(cameraMoveDirection);

				_camera.SimulationPosition += simDirection * dt * scrollSpeed;

			}

			_targetZoom -= Input.MouseWheelDelta * dt * 0.01f;

			_targetZoom = MathHelper.Clamp(_targetZoom, 0.1f, 1.2f);

			_camera.Zoom -= (_camera.Zoom - _targetZoom)*dt * 6;

			//_camera.Zoom -= 

			_simulation.Tick();

		}

		public void Draw()
		{

			var game = Shell as Shell;

			game.GraphicsDevice.Clear(Color.Black);

			_terrainRenderer.Draw(_camera);

			var entities = _simulation.EntityContainer.Entities;

			game.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null, null, _camera.Transform);

			foreach (var baseEntity in entities) {

				if (baseEntity is Unit) {

					var unit = baseEntity as Unit;

					var pos = new Vector2((float)unit.Transform.Position.X, (float)unit.Transform.Position.Y);

					pos = Translate.SimulationPointToWorld(pos);

					game.SpriteBatch.Draw(game.WhitePixelTex,
					                      new Rectangle((int) pos.X - 30,
					                                    (int) pos.Y - 30, 60, 60), new Rectangle(0, 0, 1, 1),
					                      Color.Red);

				}
			}

			game.SpriteBatch.End();

			var entity0 = entities[0] as Unit;

			game.SpriteBatch.Begin();

			var debugString = String.Format("{0,-25} {1,-25}", 
				string.Format("Camera Pos: {0}", _camera.SimulationPosition.ShortString()),
				string.Format("Mouse Pos: {0}", _mouseSimPos.ShortString()),
				entity0.Transform.Position.ShortString(), _camera.Zoom.ToString("0.00"));

			game.SpriteBatch.DrawString(_debugFont, string.Format("Camera Pos: {0}", _camera.SimulationPosition.ShortString()), new Vector2(10, 10), Color.White);
			game.SpriteBatch.DrawString(_debugFont, string.Format("Mouse Pos: {0}", _mouseSimPos.ShortString()), new Vector2(300, 10), Color.White);
			game.SpriteBatch.DrawString(_debugFont, string.Format("Zoom: {0}", _camera.Zoom.ToString("0.00")), new Vector2(620, 10), Color.White);
			game.SpriteBatch.DrawString(_debugFont, string.Format("Entity0: {0}", entity0.Transform.Position.ShortString()), new Vector2(10, 30), Color.White);

			game.SpriteBatch.End();

		}

	}

}

{'fraction_non_alphanumeric': 0.12620638455827765, 'fraction_numerical': 0.019726376073814826, 'mean_word_length': 3.6022449975597852, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5627414', 'n_tokens_mistral': 3657, 'n_tokens_neox': 3016, 'n_words': 643}