Esempio n. 1
0
		public void Render(Camera<OrthographicProjection> camera, PlayerEntity player, IChunkAccess chunk)
		{
			var topLeft = Vector3.Transform(new Vector3(camera.Projection.Left, camera.Projection.Top, 0), camera.ModelViewMatrix.Inverted());
			var bottomRight = Vector3.Transform(new Vector3(camera.Projection.Right, camera.Projection.Bottom, 0), camera.ModelViewMatrix.Inverted());

			var minX = (int)Math.Floor(topLeft.X);
			var maxX = (int)Math.Ceiling(bottomRight.X);
			var minY = (int)Math.Floor(topLeft.Y);
			var maxY = (int)Math.Ceiling(bottomRight.Y);

			_tessellator.BindColor(Color.White);

			_tessellator.LoadIdentity();
			_tessellator.Begin(PrimitiveType.Quads);

			RenderLayer(_tessellator, chunk, ChunkLayer.Background, minX, maxX, minY, maxY);
			RenderLayer(_tessellator, chunk, ChunkLayer.Floor, minX, maxX, minY, maxY);

			RenderEntities(_tessellator, chunk, minX, maxX, minY, maxY);

			RenderLayer(_tessellator, chunk, ChunkLayer.Blocking, minX, maxX, minY, maxY);
			RenderLayer(_tessellator, chunk, ChunkLayer.Ceiling, minX, maxX, minY, maxY);

			var fov = CalculateFieldOfView(_tessellator, camera, player, chunk, minX, maxX, minY, maxY);
			RenderFieldOfView(fov, _tessellator, camera, chunk, minX, maxX, minY, maxY);

			_tessellator.End();
		}
		private void Render(ITessellator tessellator, PlayerEntity entity)
		{
			tessellator.PushTransform();

			var origin = tessellator.Transform(Vector3.Zero);

			tessellator.LoadIdentity();
			tessellator.Scale(entity.Size, entity.Size);
			tessellator.Translate(origin);
			//tessellator.Translate(0.1f, 0.1f); // center on the current tile position

			tessellator.Translate(entity.Position); // move to the entity's position

			_playerTile.Render(tessellator);
			tessellator.PopTransform();
		}
Esempio n. 3
0
		private int[,] CalculateFieldOfView(ITessellator tessellator, Camera<OrthographicProjection> camera, PlayerEntity player, IChunkAccess chunk, int minX, int maxX, int minY, int maxY)
		{
			int[,] fovMap = new int[maxY - minY + 1, maxX - minX + 1];
			var originX = player.Position.X + player.Size / 2.0f;
			var originY = player.Position.Y + player.Size / 2.0f;

			var considered = new List<Vector2I>();
			//for (var angle = 0.0f; angle < 360.0f; angle += (9.0f - distance)) // hit more angles as you move further out
			for (var angle = 0.0f; angle < 360.0f; angle += 1.0f)
			{
				for (var distance = 0.0f; distance < chunk.AmbientLightLevel * 2.0f; distance++)
				{
					var x = (int)Math.Floor(originX + distance * Math.Cos(OpenTK.MathHelper.DegreesToRadians(angle)));
					var y = (int)Math.Floor(originY + distance * Math.Sin(OpenTK.MathHelper.DegreesToRadians(angle)));
					var vector = new Vector2I(y - minY, x - minX);
					if (!considered.Contains(vector))
					{
						considered.Add(vector);

						if (CommonCore.Math.MathHelper.IsInRange(y - minY, 0, maxY - minY + 1) && CommonCore.Math.MathHelper.IsInRange(x - minX, 0, maxX - minX + 1))
						{
							//var alpha = (8.0f - distance) / 8.0f;
							var alpha = 1.0f / Math.Pow((distance + 1) / chunk.AmbientLightLevel, 2);

							fovMap[y - minY, x - minX] = OpenTK.MathHelper.Clamp((int)(fovMap[y - minY, x - minX] + alpha * 255.0f), 0, 255);
						}
						else
						{
							break;
						}
					}

					if (chunk[ChunkLayer.Blocking, x, y] != 0)
					{
						break;
					}
				}
			}
			return fovMap;
		}
Esempio n. 4
0
		public void Load(string filename)
		{
			Player = PlayerEntity.Load("adccda30-36f7-4574-ae18-9a2eb48fe271.player");
			//Player = PlayerEntity.Load(Player);

			var fileStream = new FileStream(filename, FileMode.Open);
			var formatter = new BinaryFormatter();
			try
			{
				// The player is not loaded from the level file.
				Level = (Level)formatter.Deserialize(fileStream);
				Level.AddEntity(Player);
				Console.WriteLine($"Level loaded from '{filename}'.");
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.Message);
			}
			finally
			{
				fileStream.Close();
			}
		}
Esempio n. 5
0
		private void GeneratePlayer()
		{
			Player = new PlayerEntity();

			Player.Toolbelt.SetFirstCompatibleSlot(ItemStack.FromName("Pickaxe"));
			Player.Toolbelt.SetFirstCompatibleSlot(ItemStack.FromName("Hoe"));
			Player.Toolbelt.SetFirstCompatibleSlot(ItemStack.FromName("Wheat Seed", 64));
			Player.Toolbelt.SetFirstCompatibleSlot(ItemStack.FromName("Grass", 64));

			var spawnPoint = Level.GetChunk(Player).FindSpawnPoint();
			if (!spawnPoint.HasValue)
			{
				throw new Exception("Unable to spawn the player.");
			}
			else
			{
				Player.MoveTo(Level, new Vector2(spawnPoint.Value.X, spawnPoint.Value.Y));
			}

			Level.GetChunk(Player).AddEntity(Player);
		}
Esempio n. 6
0
		public static void Save(PlayerEntity player)
		{
			var fileStream = new FileStream(player.Filename, FileMode.Create);
			var formatter = new BinaryFormatter();
			try
			{
				formatter.Serialize(fileStream, player);
				Console.WriteLine($"Player saved to '{player.Filename}'.");
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.Message);
			}
			finally
			{
				fileStream.Close();
			}
		}
Esempio n. 7
0
		/// <summary>
		/// Re-load the given player.
		/// </summary>
		public static PlayerEntity Load(PlayerEntity player)
		{
			return Load(player.Filename);
		}