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 sunPosition = new Vector3(0, 0, 15); |
---|
17 | |
---|
18 | public MapRenderer(SpriteBatch spriteBatch, Map map, Vector2 screenSize) |
---|
19 | { |
---|
20 | this.spriteBatch = spriteBatch; |
---|
21 | this.map = map; |
---|
22 | this.screenSize = screenSize; |
---|
23 | } |
---|
24 | void renderGroundTile(Texture2D texture, int x, int y) |
---|
25 | {//renderöi maata |
---|
26 | //maatilen koko on 64x64. |
---|
27 | spriteBatch.Draw(texture, new Rectangle(x * 64, y * 64, texture.Width, texture.Height), null, Color.White, 0f, Vector2.Zero, SpriteEffects.None, 0f); |
---|
28 | } |
---|
29 | void renderGroundObject(Texture2D texture, int x, int y, Vector2 size, Matrix matrix) |
---|
30 | {//renderöi maassa olevan asian (ei liiku) |
---|
31 | Vector2 paikka = Vector2.Transform(new Vector2((x + 0.5f) * 64, (y + 0.5f) * 64), matrix) - new Vector2(size.X / 2, size.Y); |
---|
32 | 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)); |
---|
33 | } |
---|
34 | |
---|
35 | public void RenderMap(Camera camera) |
---|
36 | {//renderöi koko mapin, kutsu ulkopuolelta |
---|
37 | Matrix matrix = camera.getMatrix(new Vector2(64, 64), map.Size, screenSize);//mapin tilen koko 64x64 |
---|
38 | spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, matrix); |
---|
39 | for (var x = 0; x < map.Size.X; x++) |
---|
40 | { |
---|
41 | for (var y = 0; y < map.Size.Y; y++) |
---|
42 | { |
---|
43 | renderGroundTile(Pikseli.Instance.textures[map.Tiles[x, y].textureId], x, y); |
---|
44 | } |
---|
45 | } |
---|
46 | |
---|
47 | //varjot testi |
---|
48 | |
---|
49 | sunPosition = new Vector3(-20 + (float)Pikseli.Instance.gameTime.TotalGameTime.TotalSeconds + 1.42f, -20 + (float)Pikseli.Instance.gameTime.TotalGameTime.TotalSeconds, 0); |
---|
50 | |
---|
51 | foreach (MapObject obj in map.Objects) |
---|
52 | { |
---|
53 | Texture2D texture = Pikseli.Instance.textures[obj.textureId]; |
---|
54 | Vector2 size = new Vector2(texture.Width, texture.Height) * camera.zoomFactor;//ditto |
---|
55 | |
---|
56 | Vector2 erotus = new Vector2(sunPosition.X, sunPosition.Y) - obj.position; |
---|
57 | |
---|
58 | |
---|
59 | float shadowAngle = (float)(Math.Atan2(erotus.Y, erotus.X) - Math.PI / 2); |
---|
60 | double shadowOffsetX = Math.Cos(shadowAngle + Math.PI / 2) * 5, |
---|
61 | shadowOffsetY = Math.Sin(shadowAngle + Math.PI / 2) * 5; |
---|
62 | |
---|
63 | spriteBatch.Draw(texture, new Rectangle((int)((obj.position.X + 0.5) * 64), (int)((obj.position.Y + 0.5) * 64), texture.Width, texture.Height), null, new Color(0f, 0f, 0f, 0.5f), shadowAngle, new Vector2(texture.Width / 2, texture.Height), SpriteEffects.None, 0f); |
---|
64 | //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); |
---|
65 | } |
---|
66 | spriteBatch.End(); |
---|
67 | |
---|
68 | |
---|
69 | |
---|
70 | spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend); |
---|
71 | foreach (MapObject obj in map.Objects) |
---|
72 | { |
---|
73 | Texture2D texture = Pikseli.Instance.textures[obj.textureId]; |
---|
74 | Vector2 size = new Vector2(texture.Width, texture.Height) * camera.zoomFactor;//ditto |
---|
75 | renderGroundObject(texture, (int)obj.position.X, (int)obj.position.Y, size, matrix); |
---|
76 | } |
---|
77 | spriteBatch.End(); |
---|
78 | } |
---|
79 | } |
---|
80 | } |
---|