source: 2014/27/AleksanteriV/Protokolla236TrueSurvivor/Protokolla236TrueSurvivor/Protokolla236TrueSurvivor/Protokolla236TrueSurvivor.cs @ 5408

Revision 5408, 10.3 KB checked in by mijoilmo, 9 years ago (diff)
Line 
1using System;
2using System.Collections.Generic;
3using Jypeli;
4using Jypeli.Assets;
5using Jypeli.Controls;
6using Jypeli.Effects;
7using Jypeli.Widgets;
8
9public class Proto236b : PhysicsGame //true survivor
10{
11    private Player player;
12    public Player Player { get { return player; } set { player = value; } }
13    Hud hud;
14    private ColorTileMap map;
15    public ColorTileMap Map { get { return map; } set { map = value; } }
16    private Dictionary<string, Image> images = new Dictionary<string, Image>();
17    public Dictionary<string, Image> Images { get { return images; } set { images = value; } }
18    private Dictionary<string, Image[]> imageLists = new Dictionary<string, Image[]>();
19    public Dictionary<string, Image[]> ImageLists { get { return imageLists; } set { imageLists = value; } }
20    private Music music;
21    public Music Music { get { return music; } set { music = value; } }
22    private ProtoLevel currentLevel;
23    public ProtoLevel CurrentLevel { get { return currentLevel; } set { currentLevel = value; } }
24    private ScreenView gameScreen;
25    public ScreenView GameScreen { get { return gameScreen; } set { gameScreen = value; } }
26    private Dictionary<string, ProtoLevel> levels = new Dictionary<string, ProtoLevel>();
27    private Galaxy galaxy;
28    public Galaxy Galaxy { get { return galaxy; } set { galaxy = value; } }
29
30    void AssignKeys()
31    {
32        Keyboard.Listen(Key.Escape, ButtonState.Down, Exit, "Lento");
33        Keyboard.Listen(Key.Up, ButtonState.Down, player.thrusterStart, "Lento", 1.0);
34        Keyboard.Listen(Key.Up, ButtonState.Released, player.thrusterEnd, "Lento");
35        Keyboard.Listen(Key.Down, ButtonState.Down, player.thrusterStart, "Lento", -0.2);
36        Keyboard.Listen(Key.Down, ButtonState.Released, player.thrusterEnd, "Lento");
37        Keyboard.Listen(Key.Left, ButtonState.Down, player.rotate, "Lento", 4.0);
38        Keyboard.Listen(Key.Right, ButtonState.Down, player.rotate, "Lento", -4.0);
39        Keyboard.Listen(Key.LeftControl, ButtonState.Down, player.shoot, "Lento");
40    }
41    void LoadAllImages()
42    {
43        images["space_background"] = LoadImage("graphics/backgrounds/Space_esim");
44        images["mars_background"] = LoadImage("graphics/backgrounds/Mars");
45        images["planet1_background"] = LoadImage("graphics/backgrounds/planet1");
46        images["planet2_background"] = LoadImage("graphics/backgrounds/planet2");
47        images["planet2_valetta"] = LoadImage("graphics/backgrounds/planet2_valetta");
48        images["player"] = LoadImage("graphics/ships/player");
49        images["galaxy_map"] = LoadImage("graphics/HUD/galaxy_map");
50
51        //kaikki tilet
52        images["grass1"] = LoadImage("graphics/tiles/tilenurmi");
53        images["soil1"] = LoadImage("graphics/tiles/tilemulta");
54
55        imageLists["player_thruster"] = LoadImages("graphics/effects/thrusters/thruster0.1", "graphics/effects/thrusters/thruster0.2");
56        imageLists["enemy1"] = LoadImages("graphics/Enemy/Enemy1.0", "graphics/Enemy/Enemy1.1", "graphics/Enemy/Enemy1.2");
57        imageLists["HUD_radar"] = LoadImages("graphics/HUD/HUD_radar1.0", "graphics/HUD/HUD_radar1.1");
58        images["HUD_ship"] = LoadImage("graphics/HUD/cross1.0");
59    }
60    void LoadAllMusic()
61    {
62        this.music = new Music();
63    }
64    public void LoadLevel(string levelId, Angle planetAngle/*PlanetAngle määrittää missä kulmassa planeettaan tullaan tai sieltä poistutaan*/)
65    {
66        ClearAll();
67
68        ProtoLevel level = this.levels[levelId];
69        MessageDisplay.Add("loading level '" + level.TileMapSrc + "'...");
70        currentLevel = level;
71
72        Level.Background.Color = Color.Black;
73        this.player.Velocity = Vector.Zero;//resetoi pelaajan velocity
74        Add(this.player, 1);
75        Camera.Follow(this.player);
76
77        LevelFromImage(level);
78
79        if (level.IsPlanet)
80        {
81            Gravity = new Vector(0, -200);
82            player.LinearDamping = 0.99;//ns ilmanvastus
83            galaxy.CurrentPlanet = galaxy.Planets[level.Id];//aseta galaksiin tiedot nykyisestä planeetasta
84            player.Y = Level.Top - Screen.Height;
85            MediaPlayer.Play(galaxy.Planets[level.Id].Music);
86            //player.x on jotain
87        }
88        else
89        {
90            Gravity = new Vector(0, 0);
91            player.LinearDamping = 1;//avaruudessa ei ilmanvastusta
92            Camera.Zoom(0.8);
93            if (galaxy.CurrentPlanet != null)//current planet voi olla null jos pelaaja ei vielä ole käynyt millään planeetalla.
94            {
95                //aseta pelaaja galaksiin
96                player.Position = galaxy.CurrentPlanet.Position;
97                //laske vielä vektori, mistä suunnasta pelaaja poistui
98                player.Position += Vector.FromLengthAndAngle(galaxy.CurrentPlanet.Radius + 50, planetAngle);
99                player.AbsoluteAngle = planetAngle - Angle.RightAngle;//JOSSAIN VAIHEESSA KÄÄNNETÄÄN ALUS NIIN ETTEI NÄITÄ TARVITA
100            }
101            Layers[-3].RelativeTransition = new Vector(0.06, 0.06);
102        }
103
104        AssignKeys();
105        initializeHUD();
106    }
107    void CreateBg(string id, int offset, double scale = 1)
108    {
109        double h = 1, w = 1;
110        if (currentLevel.IsPlanet)
111        {
112            h = 150 * 40;
113            w = Level.Width;
114        }
115        else
116        {
117            h = Level.Height * scale;
118            w = Level.Width * scale;
119        }
120        GameObject bg = new GameObject(w, h);
121        GameObject bg2 = new GameObject(Level.Width, Level.Height);
122        bg.Image = images[id];
123        if (currentLevel.IsPlanet)
124        {
125            bg.Y = Level.Top - (150 * 40) / 2;
126        }
127        else
128        {
129            bg.Y += Level.Height * offset;
130        }
131        bg.X += Level.Width * offset;
132        bg2.X += Level.Width * offset;
133        Add(bg, -3);
134       
135        bg2.Image = images["planet2_valetta"];
136        Add(bg2, -2);
137    }
138    void LevelFromImage(ProtoLevel level)
139    {
140        Dictionary<String, String> convert = new Dictionary<String, String>();
141        convert.Add("000000", "grass1");
142        ColorTileMap tileMap = ColorTileMap.FromLevelAsset(level.TileMapSrc);
143        tileMap.SetTileMethod(Color.FromHexCode("000000"), createTile, convert["000000"], false);
144        //tileMap.SetTileMethod(Color.FromHexCode("0000ff"), createTile, convert["0000ff"], true);
145        tileMap.SetTileMethod(Color.FromHexCode("ff0000"), spawnPlayer);
146        tileMap.SetTileMethod(Color.FromHexCode("00ff00"), spawnEnemy);
147
148        //kaikki spacessa olevat jutut voisi alkaa CC
149        tileMap.SetTileMethod(Color.FromHexCode("ccffff"), createPlanet, "planet2");//planeetta 2
150
151        double w = 0, h = 0;
152        if (CurrentLevel.IsPlanet)
153        {
154            w = 40; h = 40;
155        }
156        else
157        {
158            w = 2000; h = 2000;
159        }
160
161        tileMap.Execute(w, h);
162        map = tileMap;
163
164        //lasketaan montako tiiltä tarvitaan
165        var tilesNeeded = Math.Floor((Screen.Width / 2 / 40) * Camera.ZoomFactor);
166
167        //vasen puoli
168        for (int x = 0; x < tilesNeeded; x++)
169        {
170            for (int y = 0; y < tileMap.RowCount; y++)
171            {
172                String hexColor = tileMap.GetTile(y, x).ToString().Substring(2, 6).ToLower();
173                if (convert.ContainsKey(hexColor))
174                    createTile(new Vector(x * w + w / 2 - Level.Width / 2 + Level.Width, (tileMap.RowCount - y - 1) * h + h / 2 - Level.Height / 2), w, h, convert[hexColor], false);
175            }
176        }
177        //oikea puoli
178        for (int x = tileMap.ColumnCount - 1; x > tileMap.ColumnCount - 1 - tilesNeeded; x--)
179        {
180            for (int y = 0; y < tileMap.RowCount; y++)
181            {
182                String hexColor = tileMap.GetTile(y, x).ToString().Substring(2, 6).ToLower();
183                if (convert.ContainsKey(hexColor))
184                    createTile(new Vector(x * w + w / 2 - Level.Width / 2 - Level.Width, (tileMap.RowCount - y - 1) * h + h / 2 - Level.Height / 2), w, h, convert[hexColor], false);
185            }
186        }
187        if (currentLevel.IsPlanet)
188        {
189            //luo backgroundit (2 valetta ja 1 oikea)
190            CreateBg(level.BackgroundId, -1);
191            CreateBg(level.BackgroundId, 0);
192            CreateBg(level.BackgroundId, 1);
193        }
194        else
195        {
196            CreateBg(level.BackgroundId, 0, 0.06);
197        }
198    }
199    void initializeHUD()
200    {
201        if (hud != null)
202        {
203            //poista vanha hud
204            hud.Destroy();
205        }
206        hud = new Hud(this);
207    }
208    void createTile(Vector position, double w, double h, string id, bool cheap = false)
209    {
210        if (id == "") { return; }
211        if (!cheap)
212        {
213            MikonPhysicsObject tile = new MikonPhysicsObject(this, w, h);
214            tile.MakeStatic();
215            tile.AbsolutePosition = position;
216            tile.Image = images[id];
217            Add(tile);
218        }
219        else
220        {
221            GameObject tile = new GameObject(w, h);
222            tile.AbsolutePosition = position;
223            tile.Image = images[id];
224            Add(tile);
225        }
226    }
227    void createPlanet(Vector position, double w, double h, string id)
228    {
229        Planet planet = new Planet(this, position, 1000, id);
230        Add(planet);
231    }
232    void spawnPlayer(Vector position, double w, double h)
233    {
234        player.Position = position;
235    }
236    void spawnEnemy(Vector position, double w, double h)
237    {
238        new Enemy1(this, w, h, position);
239    }
240    void LoadAllLevels()
241    {
242        this.levels["space"] = new ProtoLevel(this, "!space", false, "space_background");
243        this.levels["planet2"] = new ProtoLevel(this, "planet2", true, "planet1_background");
244    }
245    public override void Begin()
246    {
247        // TODO: Kirjoita peli tähän
248        LoadAllImages();
249        LoadAllMusic();
250        LoadAllLevels();
251
252        this.player = new Player(this);
253        this.player.attachWeapon();
254        this.player.IsUpdated = true;
255
256        this.gameScreen = Screen;
257        this.galaxy = new Galaxy(this);
258
259        LoadLevel("space", Angle.Zero);//avaruus ladataan jotta galaksi saa planeettansa
260        LoadLevel("planet2", Angle.Zero);
261    }
262}
Note: See TracBrowser for help on using the repository browser.