1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using System.Linq; |
---|
4 | using Microsoft.Xna.Framework; |
---|
5 | using Microsoft.Xna.Framework.Content; |
---|
6 | using Microsoft.Xna.Framework.Graphics; |
---|
7 | |
---|
8 | namespace WindowsGame1 |
---|
9 | { |
---|
10 | class MapRenderer |
---|
11 | { |
---|
12 | //renderöi periaatteessa kaiken paitsi GUI:n. sisältää tekstuurit |
---|
13 | SpriteBatch spriteBatch; |
---|
14 | Map map; |
---|
15 | Vector2 screenSize; |
---|
16 | Vector3 baseSunPosition = new Vector3(0, 0, 0); |
---|
17 | float sunAngle = 24; |
---|
18 | |
---|
19 | public MapRenderer(SpriteBatch spriteBatch, Map map, Vector2 screenSize) |
---|
20 | { |
---|
21 | this.spriteBatch = spriteBatch; |
---|
22 | this.map = map; |
---|
23 | this.screenSize = screenSize; |
---|
24 | baseSunPosition = new Vector3(map.Size.X/2, map.Size.Y/2, 160); |
---|
25 | } |
---|
26 | void renderGroundTile(Texture2D texture, int x, int y) |
---|
27 | {//renderöi maata |
---|
28 | //maatilen koko on 64x64. |
---|
29 | spriteBatch.Draw(texture, new Rectangle(x * 64, y * 64, texture.Width, texture.Height), null, Color.White, 0f, Vector2.Zero, SpriteEffects.None, 0.01f); |
---|
30 | } |
---|
31 | void renderGroundObject(Texture2D texture, int x, int y, Vector2 size, Matrix matrix) |
---|
32 | {//renderöi maassa olevan asian (ei liiku) |
---|
33 | Vector2 paikka = Vector2.Transform(new Vector2((x + 0.5f) * 64, (y + 0.5f) * 64), matrix) - new Vector2(size.X / 2, size.Y); |
---|
34 | spriteBatch.Draw(texture, new Rectangle((int)paikka.X, (int)paikka.Y, (int)size.X, (int)size.Y), null, Color.White, 0, Vector2.Zero, SpriteEffects.None, (paikka.Y + size.Y) / (screenSize.Y + size.Y)); |
---|
35 | } |
---|
36 | |
---|
37 | public void RenderMap(Camera camera) |
---|
38 | {//renderöi koko mapin, kutsu ulkopuolelta |
---|
39 | Matrix matrix = camera.getMatrix(new Vector2(64, 64), map.Size, screenSize);//mapin tilen koko 64x64 |
---|
40 | |
---|
41 | sunAngle += 1/3000.0f;//auringon pyörimisnopeus |
---|
42 | |
---|
43 | Vector3 sunPosition = baseSunPosition + new Vector3((float)Math.Sin(sunAngle), (float)Math.Cos(sunAngle), 0) * 400; |
---|
44 | |
---|
45 | spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend); |
---|
46 | Vector3 fixedSun = sunPosition; |
---|
47 | fixedSun.Z *= -1.0f; |
---|
48 | fixedSun.Z += 162;//alaspäin että näkyy pelissä |
---|
49 | Vector3 sun = Vector3.Transform(fixedSun * 64.0f, matrix); |
---|
50 | Texture2D sunTexture = Pikseli.Instance.textures["aurinko"]; |
---|
51 | spriteBatch.Draw(sunTexture, new Rectangle((int)(sun.X), (int)(sun.Y), sunTexture.Width, (int)(sunTexture.Height)), null, new Color(1f, 1f, 0f, 1f), 0f, new Vector2(sunTexture.Width / 2, sunTexture.Height / 2), SpriteEffects.None, 0f); |
---|
52 | spriteBatch.End(); |
---|
53 | |
---|
54 | spriteBatch.Begin(SpriteSortMode.FrontToBack, null, null, null, null, null, matrix); |
---|
55 | for (var x = 0; x < map.Size.X; x++) |
---|
56 | { |
---|
57 | for (var y = 0; y < map.Size.Y; y++) |
---|
58 | { |
---|
59 | renderGroundTile(Pikseli.Instance.textures[map.Tiles[x, y].textureId], x, y); |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | //varjot testi |
---|
64 | |
---|
65 | foreach (MapObject obj in map.Objects) |
---|
66 | { |
---|
67 | Texture2D texture = Pikseli.Instance.textures[obj.textureId]; |
---|
68 | Vector2 size = new Vector2(texture.Width, texture.Height) * camera.zoomFactor;//ditto |
---|
69 | |
---|
70 | Vector2 erotus = new Vector2(sunPosition.X, sunPosition.Y) - obj.position; |
---|
71 | |
---|
72 | |
---|
73 | float shadowAngle = (float)(Math.Atan2(erotus.Y, erotus.X) - Math.PI / 2.0); |
---|
74 | |
---|
75 | var suunta = sunPosition - new Vector3(obj.position.X, obj.position.Y, 0f); |
---|
76 | suunta.Normalize(); |
---|
77 | |
---|
78 | double dot = Vector3.Dot(Vector3.UnitZ, suunta); |
---|
79 | double testi = 1.0 / dot; |
---|
80 | double shadowHeight = (28000.0 / sunPosition.Z); |
---|
81 | |
---|
82 | spriteBatch.Draw(texture, new Rectangle((int)((obj.position.X + 0.5) * 64), (int)((obj.position.Y + 0.5) * 64), texture.Width, (int)(testi * texture.Height - texture.Height)), null, new Color(0f, 0f, 0f, 0.5f), shadowAngle, new Vector2(texture.Width / 2, texture.Height), SpriteEffects.None, 0.5f); |
---|
83 | //spriteBatch.Draw(obj.texture, new Rectangle((int)(obj.position.X + 0.5) * 64, (int)(obj.position.Y + 0.5) * 64, (int)obj.texture.Width, (int)obj.texture.Height), null, Color.Gray, MathHelper.ToRadians(30), new Vector2(obj.texture.Width / 2, obj.texture.Height), SpriteEffects.None, 0f); |
---|
84 | } |
---|
85 | spriteBatch.End(); |
---|
86 | |
---|
87 | |
---|
88 | |
---|
89 | spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend); |
---|
90 | |
---|
91 | foreach (MapObject obj in map.Objects) |
---|
92 | { |
---|
93 | Texture2D texture = Pikseli.Instance.textures[obj.textureId]; |
---|
94 | Vector2 size = new Vector2(texture.Width, texture.Height) * camera.zoomFactor;//ditto |
---|
95 | renderGroundObject(texture, (int)obj.position.X, (int)obj.position.Y, size, matrix); |
---|
96 | } |
---|
97 | |
---|
98 | spriteBatch.End(); |
---|
99 | } |
---|
100 | } |
---|
101 | } |
---|