source: 2014/30/MikkoI/WindowsGame1/WindowsGame1/WindowsGame1/Render/MapRenderer.cs @ 5626

Revision 5626, 4.3 KB checked in by mijoilmo, 9 years ago (diff)

varjosimulaattori

Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using Microsoft.Xna.Framework;
5using Microsoft.Xna.Framework.Content;
6using Microsoft.Xna.Framework.Graphics;
7
8namespace WindowsGame1
9{
10    class MapRenderer
11    {
12        //renderöi periaatteessa kaiken paitsi GUI:n. sisältää tekstuurit
13        Map map;
14        Vector3 baseSunPosition = new Vector3(0, 0, 0);
15        Camera camera;
16        RendererBase r;
17
18        float sunAngle = 24;
19        Vector3 sunPosition = Vector3.Zero;
20
21        public MapRenderer(RendererBase r, Map map, Camera camera)
22        {
23            this.map = map;
24            this.baseSunPosition = new Vector3(map.Size.X / 2, map.Size.Y / 2, 40);//alusta auringon pyörityksen joint-piste
25            this.camera = camera;
26            this.r = r;
27        }
28
29        void renderGroundObjects(Matrix matrix) {
30            //renderöi kaikki maassa olevat dynaamiset objektit, eli puut, olennot ym.
31            r.spriteBatch.Begin(SpriteSortMode.FrontToBack, null, null, null, null, null, matrix);
32
33            //VARJOJEN RENDERÖINTI
34            foreach (MapObject obj in map.Objects)
35            {
36                Texture2D texture = Pikseli.Instance.textures[obj.textureId];
37                Vector2 size = new Vector2(texture.Width, texture.Height) * camera.zoomFactor;//ditto
38
39                Vector2 erotus = new Vector2(sunPosition.X, sunPosition.Y) - obj.position;
40
41
42                float shadowAngle = (float)(Math.Atan2(erotus.Y, erotus.X) - Math.PI / 2.0);
43
44                var shadowDirection = sunPosition - new Vector3(obj.position.X, obj.position.Y, 0f);
45                shadowDirection.Normalize();
46
47                double dot = Vector3.Dot(Vector3.UnitZ, shadowDirection);
48
49                r.spriteBatch.Draw(texture, new Rectangle((int)((obj.position.X + 0.5) * 64), (int)((obj.position.Y + 0.5) * 64), texture.Width, (int)((1.0 / dot) * texture.Height - texture.Height)), null, new Color(0f, 0f, 0f, 0.5f), shadowAngle, new Vector2(texture.Width / 2, texture.Height + obj.shadowOffset), SpriteEffects.None, 0.5f);
50            }
51
52            r.spriteBatch.End();
53
54            r.spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);
55
56            //OBJEKTIEN RENDERÖINTI
57            foreach (MapObject obj in map.Objects)
58            {
59                Texture2D texture = Pikseli.Instance.textures[obj.textureId];
60                Vector2 size = new Vector2(texture.Width, texture.Height) * camera.zoomFactor;//ditto
61                r.renderGroundObject(texture, (int)obj.position.X, (int)obj.position.Y, size, matrix, camera);
62            }
63
64            r.spriteBatch.End();
65        }
66
67        void renderSky(Matrix matrix) {
68            r.spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);
69
70            Texture2D starBackground = Pikseli.Instance.textures["taivas/tähdet"];
71
72            r.renderBackground(starBackground, new Vector2(starBackground.Width, starBackground.Height));
73
74            r.spriteBatch.End();
75        }
76
77        public void Render(Matrix matrix)
78        {//renderöi koko mapin, kutsu ulkopuolelta
79            renderSky(matrix);
80
81            r.spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, matrix);
82            for (var x = 0; x < map.Size.X; x++)
83            {
84                for (var y = 0; y < map.Size.Y; y++)
85                {
86                    r.renderGroundTile(Pikseli.Instance.textures[map.Tiles[x, y].textureId], x, y);
87                }
88            }
89            r.spriteBatch.End();
90
91            renderGroundObjects(matrix);
92
93            r.spriteBatch.Begin();
94
95            sunAngle += 1 / 3000.0f;//auringon pyörimisnopeus
96
97            sunPosition = baseSunPosition + new Vector3((float)Math.Sin(sunAngle), (float)Math.Cos(sunAngle), 0) * 100;
98
99            Vector3 fixedSun = sunPosition;
100            fixedSun.Z *= -1.0f;
101            fixedSun.Z += 22;//alaspäin että näkyy pelissä
102
103            Texture2D sunTexture = Pikseli.Instance.textures["taivas/aurinko"];
104
105            r.renderDistantSprite(sunTexture, fixedSun, new Vector2(sunTexture.Width * camera.zoomFactor * 5, sunTexture.Height * camera.zoomFactor * 5), matrix);
106            r.spriteBatch.End();
107        }
108    }
109}
Note: See TracBrowser for help on using the repository browser.