Skip to content
Snippets Groups Projects
Commit 855880fd authored by Rajahalme Tomi Tapani's avatar Rajahalme Tomi Tapani
Browse files

Häkkien ja kissojen toiminta

parents 65058f48 89acccac
No related branches found
No related tags found
No related merge requests found
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Jypeli;
using Jypeli.Assets;
using Jypeli.Controls;
......@@ -15,6 +14,26 @@ namespace Cat_Rescue;
/// </summary>
public class Cat_Rescue : Game
{
//kentät merkkijonoina
private static readonly String[] lines1 = {
" YYYYYYYYYYYYYYYYYYYYYYYY ",
" Y X Y ",
" Y # X = Y ",
" Y * X Y ",
" Y X XXX XXX Y ",
" Y X X X Y ",
" YXX XXXX X X Y ",
" Y X X Y ",
" Y = X X Y ",
" Y X X Y ",
" YXX XXXXXXXX X Y ",
" Y X X Y ",
" Y X X Y ",
" Y # X # X Y ",
" Y X X C Y ",
" Y X X Y ",
" YYYYYYYYYYYYYYYYYYYYYYYY ",
};
private static readonly String[] level1 = {
" YYYYYYYYYYYYYYYYYYYYYYYY ",
" Y X Y ",
......@@ -31,23 +50,23 @@ public class Cat_Rescue : Game
" Y X X Y ",
" Y 1 X 2 X Y ",
" Y X X Y ",
" Y * X C X Y ",
" YC * X X Y ",
" YYYYYYYYYYYYYYYYYYYYYYYY ",
};
private int[][] catRoutes = new int[][] {[4, 2, 3, 6, 2, 6, 1, 6, 2, 11, 3, 13, 0, 0], [4, 2, 1, 5, 2, 6, 1, 6, 2, 11, 3, 13, 0, 0],[2, 5, 1, 11, 2, 6, 3, 11, 3, 1, 3, 1, 0, 0]};
private int[] catPos = new int[6];
//Yksittäisiä pelaajan liikkumiseen liittyviä muuttujia
private int[] playerPos = new int[2];
private int[] catPos = new int[6];
private bool playerMoveCooldown = true;
private int[,] map_entities = new int[24,24];
//Kartan luomiseen ja käyttämiseen liittyviä muuttujia
//private Image planksImage = LoadImage("");
//private Image oWallImage = LoadImage("");
//private Image iWallImage = LoadImage("");
//private Image keyImage = LoadImage("");
//private Image cageImage = LoadImage("");
private int[,] map_entities = new int[24,24];
private GameObject player;
private GameObject key;
private GameObject[] cage = new GameObject[3];
......@@ -65,14 +84,12 @@ public class Cat_Rescue : Game
Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
}
private void CreateMap()
private void CreateMap() //Aliohjelma, joka luo kartan käyttäen muita aliohjelmia
{
Level.Background.CreateGradient(Color.SlateGray, Color.ForestGreen);
CreateFloor(750, 500, Color.LightGray);
TileMap tiles = TileMap.FromStringArray(level1);
tiles.SetTileMethod('X', CreateWall, Color.Gray);
tiles.SetTileMethod('Y', CreateWall, Color.DarkGray);
......@@ -90,23 +107,25 @@ public class Cat_Rescue : Game
private void CreateWall(Vector location, double width, double height, Color color)
private void CreateWall(Vector location, double width, double height, Color color) //Aliohjema, joka luo kentän seinät
{
GameObject wall = new GameObject(width, height);
wall.Position = location;
wall.Color = color;
map_entities[Convert.ToInt16(wall.X/32+11.5), Convert.ToInt16(wall.Y/32+8)] = 1;
Add(wall);
//Tallennetaan seinät koordinaatistoon, helppoa viittaamista varten
map_entities[Convert.ToInt16(wall.X/32+11.5), Convert.ToInt16(wall.Y/32+8)] = 1;
}
private void CreateFloor(double width, double height, Color color)
private void CreateFloor(double width, double height, Color color) //Aliohjelma, joka luo kentän lattian
{
GameObject floor = new GameObject(width, height);
floor.Color = color;
Add(floor);
}
private void CreateCageAndCat(Vector location, double width, double height, Color color, int id)
private void CreateCageAndCat(Vector location, double width, double height, Color color, int id)//Aliohjelma, joka luo kissojen häkit kenttään
{
cage[id] = new GameObject(width, height);
cat[id] = new GameObject(width, height);
......@@ -119,22 +138,25 @@ public class Cat_Rescue : Game
catPos[2*id] = Convert.ToInt16(cat[id].X/32+11.5);
catPos[2*id+1] = Convert.ToInt16(cat[id].Y/32+8);
//Tallennetaan häkit koordinaatistoon, helppoa viittaamista varten
map_entities[Convert.ToInt16(cage[id].X/32+11.5), Convert.ToInt16(cage[id].Y/32+8)] = (id + 3);
Add(cat[id]);
Add(cage[id]);
}
private void CreateKey(Vector location, double width, double height, Color color)
private void CreateKey(Vector location, double width, double height, Color color) //Aliohjelma, joka luo avaimen kenttään
{
key = new GameObject(width, height);
key.Position = location;
key.Color = color;
map_entities[Convert.ToInt16(key.X/32+11.5), Convert.ToInt16(key.Y/32+8)] = 2;
Add(key);
//Tallennetaan häkit koordinaatistoon, helppoa viittaamista varten
map_entities[Convert.ToInt16(key.X/32+11.5), Convert.ToInt16(key.Y/32+8)] = 2;
}
private void CreateGuard(Vector location, double width, double height, Color color)
private void CreateGuard(Vector location, double width, double height, Color color) //Aliohjelma, joka luo vartijat
{
GameObject guard = new GameObject(width, height);
guard.Position = location;
......@@ -150,19 +172,19 @@ public class Cat_Rescue : Game
Add(guard);
}
private void CreatePlayer(Vector location, double width, double height, Color color)
private void CreatePlayer(Vector location, double width, double height, Color color) //Aliohjelma, joka luo pelaaja hahmon ja määrää pelaajan muuttujat
{
player = new GameObject(width, height);
player.Position = location;
player.Color = color;
player.Tag = "Player";
Add(player);
playerPos[0] = 1;
playerPos[1] = 1;
Add(player);
}
private void CreateKeys()
private void CreateKeys() //Kääntää näppäinten painallukset pelaajan liikkeeeksi
{
Keyboard.Listen(Key.W, ButtonState.Pressed, MovePlayer, "Liikkuu vasemmalle", player, 0, 1);
Keyboard.Listen(Key.A, ButtonState.Pressed, MovePlayer, "Liikkuu vasemmalle", player, -1, 0);
......@@ -170,14 +192,18 @@ public class Cat_Rescue : Game
Keyboard.Listen(Key.D, ButtonState.Pressed, MovePlayer, "Liikkuu vasemmalle", player, 1, 0);
}
private void MovePlayer(GameObject entity, int x, int y)
private void MovePlayer(GameObject entity, int x, int y) //Aliohjema, joka liikutta pelaajaa ja aloitaa kaiken vuorovaikutuksen kentän kanssa.
{
//estää liikkuumisen, kon edellinen liike on kesken
if (playerMoveCooldown)
{
//estää liikkumisen, jos edessä on seinä
if (map_entities[playerPos[0] + x, playerPos[1] + y] != 1)
{
playerMoveCooldown = false;
player.MoveTo(new Vector((playerPos[0]-11.5+ x)*32, (playerPos[1]-8+y)*32), 50, playerPosSet(x, y));
//tarkistaa, jos edessä on avain ja kerää avaimen mikäli tämä on tosi
if (map_entities[playerPos[0], playerPos[1]] == 2)
{
key.Destroy();
......@@ -186,6 +212,7 @@ public class Cat_Rescue : Game
map_entities[playerPos[0], playerPos[1]] = 0;
}
//tarkistaa, jos edessä on häkki (ja onko pelaajalla avain) ja avaa häkin mikäli nämä ovat tosi
if (map_entities[playerPos[0], playerPos[1]] > 2 && keyHeld == true)
{
for (int i = 0; i < 4; i++)
......@@ -197,7 +224,6 @@ public class Cat_Rescue : Game
map_entities[playerPos[0], playerPos[1]] = 0;
}
}
Console.WriteLine("Cat Freed!");
}
}
......@@ -273,7 +299,7 @@ public class Cat_Rescue : Game
return null;
}
private System.Action playerPosSet(int x, int y)
private System.Action playerPosSet(int x, int y) //Tallentaa pelaajan sijainnin koordinaatiostoon joka kerta, kun pelaaja liikkuu
{
playerPos[0] += x;
playerPos[1] += y;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment