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

Revision 5448, 11.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");
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["planet2"] = LoadImage("graphics/Planets/MapPlanet1");
49        images["planet1"] = LoadImage("graphics/Planets/MapPlanet3");
50        images["player"] = LoadImage("graphics/ships/player1.0");
51        images["galaxy_map"] = LoadImage("graphics/HUD/galaxy_map");
52        imageLists["planet_map"] = LoadImages("graphics/HUD/RadarPlaneetta1.0", "graphics/HUD/RadarPlaneetta1.1");
53
54        //kaikki tilet
55        images["grass1"] = LoadImage("graphics/tiles/tilenurmi");
56        images["soil1"] = LoadImage("graphics/tiles/tilemulta");
57        images["wood1"] = LoadImage("graphics/tiles/tilepuu");
58
59        imageLists["player_thruster"] = LoadImages("graphics/effects/thrusters/thruster0.1", "graphics/effects/thrusters/thruster0.2");
60        imageLists["enemy1"] = LoadImages("graphics/Enemy/Enemy1.0", "graphics/Enemy/Enemy1.1", "graphics/Enemy/Enemy1.2");
61        imageLists["HUD_radar"] = LoadImages("graphics/HUD/HUD_radar1.0", "graphics/HUD/HUD_radar1.1");
62        images["HUD_ship"] = LoadImage("graphics/HUD/cross1.0");
63    }
64    void LoadAllMusic()
65    {
66        this.music = new Music();
67    }
68    public SoundEffect loadSound(string ef) {
69        return LoadSoundEffect(ef);
70    }
71    public void LoadLevel(string levelId, Angle planetAngle/*PlanetAngle määrittää missä kulmassa planeettaan tullaan tai sieltä poistutaan*/)
72    {
73        ClearAll();
74
75        ProtoLevel level = this.levels[levelId];
76        MessageDisplay.Add("loading level '" + level.TileMapSrc + "'...");
77        currentLevel = level;
78
79        Level.Background.Color = Color.Black;
80        this.player.Velocity = Vector.Zero;//resetoi pelaajan velocity
81        Add(this.player, 1);
82        Camera.Follow(this.player);
83
84        LevelFromImage(level);
85
86        if (level.IsPlanet)
87        {
88            Gravity = new Vector(0, -200);
89            player.LinearDamping = 0.99;//ns ilmanvastus
90            galaxy.CurrentPlanet = galaxy.Planets[level.Id];//aseta galaksiin tiedot nykyisestä planeetasta
91            player.Y = Level.Top - Screen.Height;
92            Camera.Zoom(0.8);
93            MediaPlayer.Stop();
94            MediaPlayer.Play(galaxy.Planets[level.Id].Music);
95            player.X = (planetAngle.Degrees / Angle.FullAngle.Degrees) * Level.Width - Level.Width / 2;
96        }
97        else
98        {
99            MediaPlayer.Stop();
100            MediaPlayer.Play(music.SpaceMusic[RandomGen.NextInt(0,music.SpaceMusic.Count)]);
101            Gravity = new Vector(0, 0);
102            player.LinearDamping = 1;//avaruudessa ei ilmanvastusta
103            Camera.Zoom(0.6);
104            if (galaxy.CurrentPlanet != null)//current planet voi olla null jos pelaaja ei vielä ole käynyt millään planeetalla.
105            {
106                //aseta pelaaja galaksiin
107                player.Position = galaxy.CurrentPlanet.Position;
108                //laske vielä vektori, mistä suunnasta pelaaja poistui
109                player.Position += Vector.FromLengthAndAngle(galaxy.CurrentPlanet.Radius + 50, planetAngle);
110                player.AbsoluteAngle = planetAngle - Angle.RightAngle;//JOSSAIN VAIHEESSA KÄÄNNETÄÄN ALUS NIIN ETTEI NÄITÄ TARVITA
111            }
112            Layers[-3].RelativeTransition = new Vector(0.06, 0.06);
113        }
114
115        AssignKeys();
116        initializeHUD();
117    }
118    void CreateBg(string id, string valettaId, int offset, double scale = 1)
119    {
120        double h = 1, w = 1;
121        if (currentLevel.IsPlanet)
122        {
123            h = 150 * 40;
124            w = Level.Width;
125        }
126        else
127        {
128            h = Level.Height * scale;
129            w = Level.Width * scale;
130        }
131        GameObject bg = new GameObject(w, h);
132        bg.Image = images[id];
133        bg.X += Level.Width * offset;
134        if (currentLevel.IsPlanet)
135        {
136            GameObject bg2 = new GameObject(Level.Width, Level.Height);
137            bg.Y = Level.Top - (150 * 40) / 2;
138            bg2.X += Level.Width * offset;
139            bg2.Image = images[valettaId];
140            Add(bg2, -2);
141        }
142        else
143        {
144            bg.Y += Level.Height * offset;
145        }
146        Add(bg, -3);
147    }
148    void LevelFromImage(ProtoLevel level)
149    {
150        Dictionary<String, String> convert = new Dictionary<String, String>();
151        convert.Add("000000", "grass1");
152        convert.Add("00ffff", "wood1");
153        ColorTileMap tileMap = ColorTileMap.FromLevelAsset(level.TileMapSrc);
154        tileMap.SetTileMethod(Color.FromHexCode("000000"), createTile, convert["000000"], false);
155        tileMap.SetTileMethod(Color.FromHexCode("00ffff"), createTile, convert["00ffff"], true);
156        tileMap.SetTileMethod(Color.FromHexCode("ff0000"), spawnPlayer);
157        tileMap.SetTileMethod(Color.FromHexCode("00ff00"), spawnEnemy);
158
159        //kaikki spacessa olevat jutut voisi alkaa CC
160        tileMap.SetTileMethod(Color.FromHexCode("ccffff"), createPlanet, "planet2");//planeetta 2
161        tileMap.SetTileMethod(Color.FromHexCode("ccffcc"), createPlanet, "planet1");//planeetta 1
162
163        double w = 0, h = 0;
164        if (CurrentLevel.IsPlanet)
165        {
166            w = 40; h = 40;
167        }
168        else
169        {
170            w = 2000; h = 2000;
171        }
172        tileMap.Execute(w, h);
173        map = tileMap;
174
175        //lasketaan montako tiiltä tarvitaan
176        var tilesNeeded = Math.Floor((Screen.Width / 2 / 40) * Camera.ZoomFactor);
177
178        //vasen puoli
179        for (int x = 0; x < tilesNeeded; x++)
180        {
181            for (int y = 0; y < tileMap.RowCount; y++)
182            {
183                String hexColor = tileMap.GetTile(y, x).ToString().Substring(2, 6).ToLower();
184                if (convert.ContainsKey(hexColor))
185                    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);
186            }
187        }
188        //oikea puoli
189        for (int x = tileMap.ColumnCount - 1; x > tileMap.ColumnCount - 1 - tilesNeeded; x--)
190        {
191            for (int y = 0; y < tileMap.RowCount; y++)
192            {
193                String hexColor = tileMap.GetTile(y, x).ToString().Substring(2, 6).ToLower();
194                if (convert.ContainsKey(hexColor))
195                    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);
196            }
197        }
198        if (currentLevel.IsPlanet)
199        {
200            //luo backgroundit (2 valetta ja 1 oikea)
201            CreateBg(level.BackgroundId, level.ValettaId, -1);
202            CreateBg(level.BackgroundId, level.ValettaId, 0);
203            CreateBg(level.BackgroundId, level.ValettaId, 1);
204        }
205        else
206        {
207            CreateBg(level.BackgroundId, "", 0, 0.06);
208        }
209    }
210    void initializeHUD()
211    {
212        if (hud != null)
213        {
214            //poista vanha hud
215            hud.Destroy();
216        }
217        hud = new Hud(this);
218    }
219    void createTile(Vector position, double w, double h, string id, bool breakable = false)
220    {
221        if (id == "") { return; }
222        if (breakable)
223        {
224            MikonPhysicsObject tile = new MikonPhysicsObject(this, w, h);
225            tile.MakeStatic();
226            tile.AbsolutePosition = position;
227            tile.Image = images[id];
228            Add(tile);
229        }
230        else
231        {
232            PhysicsObject tile = new PhysicsObject(w, h);
233            tile.MakeStatic();
234            tile.AbsolutePosition = position;
235            tile.Image = images[id];
236            Add(tile);
237        }
238    }
239    void createPlanet(Vector position, double w, double h, string id)
240    {
241        Planet planet = new Planet(this, position, 1000, id);
242        Add(planet);
243    }
244    void spawnPlayer(Vector position, double w, double h)
245    {
246        player.Position = position;
247    }
248    void spawnEnemy(Vector position, double w, double h)
249    {
250        new Enemy1(this, w, h, position);
251    }
252    void LoadAllLevels()
253    {
254        this.levels["space"] = new ProtoLevel(this, "!space", false, "space_background");
255        this.levels["planet2"] = new ProtoLevel(this, "planet2", true, "planet1_background");
256        this.levels["planet1"] = new ProtoLevel(this, "planet3", true, "planet2_background");
257    }
258    public override void Begin()
259    {
260        // TODO: Kirjoita peli tähän
261        SmoothTextures = false;
262        LoadAllMusic();
263        LoadAllImages();
264        LoadAllLevels();
265
266        this.player = new Player(this);
267        this.player.attachWeapon();
268        this.player.IsUpdated = true;
269
270        this.gameScreen = Screen;
271        this.galaxy = new Galaxy(this);
272
273        LoadLevel("space", Angle.Zero);//avaruus ladataan jotta galaksi saa planeettansa
274        LoadLevel("planet2", Angle.Zero);
275    }
276}
Note: See TracBrowser for help on using the repository browser.