From 0a7e8c6dbcb81c719069b1c66f2d71e876bb6640 Mon Sep 17 00:00:00 2001 From: Rajahalme Tomi Tapani <tomi.rajahalme.tr@gmail.com> Date: Sat, 29 Mar 2025 02:36:41 +0200 Subject: [PATCH] =?UTF-8?q?H=C3=A4kkien=20ja=20kissojen=20toiminta?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cat Rescue/Cat Rescue/Cat Rescue.cs | 144 ++++++++++++++++++++++------ 1 file changed, 114 insertions(+), 30 deletions(-) diff --git a/Cat Rescue/Cat Rescue/Cat Rescue.cs b/Cat Rescue/Cat Rescue/Cat Rescue.cs index 25c38b4..be4eaa5 100644 --- a/Cat Rescue/Cat Rescue/Cat Rescue.cs +++ b/Cat Rescue/Cat Rescue/Cat Rescue.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using Jypeli; using Jypeli.Assets; using Jypeli.Controls; @@ -14,25 +15,6 @@ namespace Cat_Rescue; /// </summary> public class Cat_Rescue : Game { - 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 ", @@ -49,11 +31,13 @@ public class Cat_Rescue : Game " Y X X Y ", " Y 1 X 2 X Y ", " Y X X Y ", - " YC * X X Y ", + " Y * X C 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[] playerPos = new int[2]; + private int[] catPos = new int[6]; private bool playerMoveCooldown = true; private int[,] map_entities = new int[24,24]; @@ -67,6 +51,7 @@ public class Cat_Rescue : Game private GameObject player; private GameObject key; private GameObject[] cage = new GameObject[3]; + private GameObject[] cat = new GameObject[3]; private bool keyHeld = false; @@ -96,7 +81,7 @@ public class Cat_Rescue : Game tiles.SetTileMethod('C', CreatePlayer, Color.White); for (int i = 0; i < 3; i++) { - tiles.SetTileMethod(Convert.ToChar((char)(i+48)), CreateCage, Color.LightBlue, i); + tiles.SetTileMethod(Convert.ToChar((char)(i+48)), CreateCageAndCat, Color.LightBlue, i); } tiles.Execute(32, 32); @@ -121,12 +106,22 @@ public class Cat_Rescue : Game Add(floor); } - private void CreateCage(Vector location, double width, double height, Color color, int id) + private void CreateCageAndCat(Vector location, double width, double height, Color color, int id) { cage[id] = new GameObject(width, height); + cat[id] = new GameObject(width, height); cage[id].Position = location; cage[id].Color = color; + + cat[id] = new GameObject(width, height); + cat[id].Position = location; + cat[id].Color = Color.Purple; + catPos[2*id] = Convert.ToInt16(cat[id].X/32+11.5); + catPos[2*id+1] = Convert.ToInt16(cat[id].Y/32+8); + 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]); } @@ -147,6 +142,14 @@ public class Cat_Rescue : Game Add(guard); } + private void CreateCat(Vector location, double width, double height, Color color) + { + GameObject guard = new GameObject(width, height); + guard.Position = location; + guard.Color = color; + Add(guard); + } + private void CreatePlayer(Vector location, double width, double height, Color color) { player = new GameObject(width, height); @@ -174,38 +177,119 @@ public class Cat_Rescue : Game if (map_entities[playerPos[0] + x, playerPos[1] + y] != 1) { playerMoveCooldown = false; - if (map_entities[playerPos[0] + x, playerPos[1] + y] == 2) + player.MoveTo(new Vector((playerPos[0]-11.5+ x)*32, (playerPos[1]-8+y)*32), 50, playerPosSet(x, y)); + if (map_entities[playerPos[0], playerPos[1]] == 2) { key.Destroy(); keyHeld = true; Console.WriteLine("Key Found!"); + map_entities[playerPos[0], playerPos[1]] = 0; } - if (map_entities[playerPos[0] + x, playerPos[1] + y] > 2 && keyHeld == true) + + if (map_entities[playerPos[0], playerPos[1]] > 2 && keyHeld == true) { - for (int i = 0; i < 3; i++) + for (int i = 0; i < 4; i++) { - if (map_entities[playerPos[0] + x, playerPos[1] + y] == (i + 3)) + if (map_entities[playerPos[0], playerPos[1]] == (i + 3)) { cage[i].Destroy(); + ObjectMove(cat[i], catRoutes[i], catPos[2*i], catPos[2*i+1], 100); + map_entities[playerPos[0], playerPos[1]] = 0; } } + Console.WriteLine("Cat Freed!"); } - player.MoveTo(new Vector((playerPos[0]-11.5+ x)*32, (playerPos[1]-8+y)*32), 50, playerPosSet(x, y)); } } } + + + private void ObjectMove(GameObject entity, int[] route, int posX, int posY, int speed) + { + Console.WriteLine("Starting..."); + Timer timer = new Timer(0.1); + Console.WriteLine("Cat Step: " + route[route.Length-1]); + timer.Timeout += delegate + { + MovementFunction(entity, route, posX, posY, speed); + posX++; posY++; + posX--; posY--; + if (route[2*(route[route.Length-1])] == 1) //up + { + posY += route[2*(route[route.Length-1])+1]; + Console.WriteLine("up"); + } + if (route[2*(route[route.Length-1])] == 2) //right + { + posX += route[2*(route[route.Length-1])+1]; + Console.WriteLine("right"); + } + if (route[2*(route[route.Length-1])] == 3) //down + { + posY += -route[2*(route[route.Length-1])+1]; + Console.WriteLine("down"); + } + if (route[2*(route[route.Length-1])] == 4) //left + { + posX += -route[2*(route[route.Length-1])+1]; + Console.WriteLine("left"); + } + + timer.Interval = route[2*(route[route.Length-1])+1]*0.32; + + route[route.Length-1] += 1; + if (route[route.Length-1] == 7) + { + Remove(entity); + } + }; + timer.Start(7); + } + + private System.Action MovementFunction(GameObject entity, int[] route, int posX, int posY, int speed) + { + if (route[2*(route[route.Length-1])] == 1) //up + { + entity.MoveTo(new Vector((posX - 11.5) * 32, (route[2*(route[route.Length-1])+1] + posY - 8) * 32), speed); + Console.WriteLine("Coordinates: " + posX + ", " + (route[(route[route.Length-1])+1] + posY)); + } + if (route[2*(route[route.Length-1])] == 2) //right + { + entity.MoveTo(new Vector((route[2*(route[route.Length-1])+1] + posX - 11.5) * 32, (posY - 8) * 32), speed); + Console.WriteLine("Coordinates: " + posX + ", " + (route[(route[route.Length-1])+1] + posY)); + } + if (route[2*(route[route.Length-1])] == 3) //down + { + entity.MoveTo(new Vector((posX - 11.5) * 32, (- route[2*(route[route.Length-1])+1] + posY - 8) * 32), speed); + Console.WriteLine("Coordinates: " + posX + ", " + (route[(route[route.Length-1])+1] + posY)); + } + if (route[2*(route[route.Length-1])] == 4) //left + { + entity.MoveTo(new Vector((- route[2*(route[route.Length-1])+1] + posX - 11.5) * 32, (posY - 8) * 32), speed); + Console.WriteLine("Coordinates: " + posX + ", " + (route[(route[route.Length-1])+1] + posY)); + } + Console.WriteLine("Problem: (2 * " + (route[route.Length-1]) + ") + 1 = " + (2*(route[route.Length-1])+1) ); + return null; + } private System.Action playerPosSet(int x, int y) { playerPos[0] += x; playerPos[1] += y; - playerMoveCooldown = true; + Console.WriteLine(playerPos[0]+", "+playerPos[1]); + Timer movecooldown = new Timer(); + movecooldown.Interval = 30.0/80.0; //movement cooldown! + movecooldown.Timeout += delegate + { + playerMoveCooldown = true; + }; + movecooldown.Start(1); return null; } - private void KillMe() + private void cd() { - Console.WriteLine("Fuck!"); + Console.WriteLine("Player step!"); } } \ No newline at end of file -- GitLab