From 28b9404fe972b379d9a3ac846233211fc452b003 Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 30 Jun 2021 13:32:05 +0200 Subject: [PATCH] added default texture --- Zoelda/src/main/SheetLoader.java | 54 +++++++++++---------- Zoelda/src/main/entities/LivingEntity.java | 20 +++++++- Zoelda/src/main/entities/Snake.java | 7 ++- Zoelda/src/main/entities/Spider.java | 7 +-- Zoelda/src/main/entities/player/Player.java | 5 +- Zoelda/src/main/worlds/TestWorld.java | 6 +-- 6 files changed, 58 insertions(+), 41 deletions(-) diff --git a/Zoelda/src/main/SheetLoader.java b/Zoelda/src/main/SheetLoader.java index 17acd13..0dfb025 100644 --- a/Zoelda/src/main/SheetLoader.java +++ b/Zoelda/src/main/SheetLoader.java @@ -12,44 +12,46 @@ import ea.internal.gra.PixelFeld; public class SheetLoader { private Figur[] figures; + private BufferedImage sheet; /** * Macht aus einem Spritesheet Figuren. - * @param path - Der pfad zum sheet - * @param width - die Breite von einem Bild - * @param height - die Höhe von einem Bild (nicht das ganze sheet) + * + * @param path - Der pfad zum sheet + * @param width - die Breite von einem Bild + * @param height - die Höhe von einem Bild (nicht das ganze sheet) * @param lengths - die Anzahl der Frames der verschiedenen Animationen */ - public SheetLoader(String path, int width, int height, int[] frames) { - + public SheetLoader(String path) { try { // Bild laden - BufferedImage sheet = ImageIO.read(Tile.class.getResourceAsStream(path)); - - // Anzahl der Animationen - int rows = frames.length; - - // Sprites anordnen *magie* - PixelFeld[][] sprites = new PixelFeld[rows][]; - for (int i = 0; i < frames.length; i++) { - sprites[i] = new PixelFeld[frames[i]]; - } - for (int i = 0; i < sprites.length; i++) { - loadSprites(sprites[i], i, width, height, sheet); - } - Figur fig; - figures = new Figur[rows]; - for (int i = 0; i < figures.length; i++) { - fig = new Figur(); - fig.animationSetzen(sprites[i]); - figures[i] = fig; - } - + sheet = ImageIO.read(Tile.class.getResourceAsStream(path)); } catch (IOException e) { e.printStackTrace(); } } + public void generateFigures(int width, int height, int[] frames) { + // Anzahl der Animationen + int rows = frames.length; + + // Sprites anordnen *magie* + PixelFeld[][] sprites = new PixelFeld[rows][]; + for (int i = 0; i < frames.length; i++) { + sprites[i] = new PixelFeld[frames[i]]; + } + for (int i = 0; i < sprites.length; i++) { + loadSprites(sprites[i], i, width, height, sheet); + } + Figur fig; + figures = new Figur[rows]; + for (int i = 0; i < figures.length; i++) { + fig = new Figur(); + fig.animationSetzen(sprites[i]); + figures[i] = fig; + } + } + public Figur getFigur(int y) { return figures[y]; } diff --git a/Zoelda/src/main/entities/LivingEntity.java b/Zoelda/src/main/entities/LivingEntity.java index 7e86c60..686a428 100644 --- a/Zoelda/src/main/entities/LivingEntity.java +++ b/Zoelda/src/main/entities/LivingEntity.java @@ -1,7 +1,10 @@ package main.entities; +import java.awt.Color; + import ea.ActionFigur; import ea.Figur; +import ea.internal.gra.PixelFeld; import main.worlds.World; /* @@ -16,12 +19,25 @@ public abstract class LivingEntity extends Entity { protected float hp = 1f; //hp des Entitys protected boolean mirrored; + private static Figur noTexture = new Figur(); + + static { + PixelFeld[] img = new PixelFeld[1]; + img[0] = new PixelFeld(16, 16, 1); + for (int x = 0; x < 16; x++) { + for (int y = 0; y < 16; y++) { + img[0].farbeSetzen(x, y, Color.MAGENTA); + } + } + noTexture.animationSetzen(img); + } + /** * @param figur - erstes (standart) Sprite * @param name - name des Zustands */ - public LivingEntity(Figur figur, String name) { - actionFigur = new ActionFigur(figur, name) { + public LivingEntity() { + actionFigur = new ActionFigur(noTexture, "undefined") { // DEBUG: render boxes // @Override diff --git a/Zoelda/src/main/entities/Snake.java b/Zoelda/src/main/entities/Snake.java index b6150d7..cb6230d 100644 --- a/Zoelda/src/main/entities/Snake.java +++ b/Zoelda/src/main/entities/Snake.java @@ -7,15 +7,13 @@ import main.entities.player.Player; public class Snake extends LivingEntity { - private static SheetLoader loader = new SheetLoader("/res/images/snake_spritesheet_calciumtrice.png", 32, 32, - new int[] { 10, 10, 10, 10, 13, 8 }); + private static SheetLoader loader = new SheetLoader("/res/images/snake_spritesheet_calciumtrice.png"); private float damage = 0.1f; private boolean attackOnce; public Snake() { - super(loader.getFigur(0), "idle"); - + loader.generateFigures(32, 32, new int[] { 10, 10, 10, 10, 13, 8 }); posX = 9f; posY = 9f; accelleration = 0.006f; @@ -24,6 +22,7 @@ public class Snake extends LivingEntity { height = 0.8f; mirrored = true; + actionFigur.neuerZustand(loader.getFigur(0), "idle"); actionFigur.neuerZustand(loader.getFigur(1), "lost_sight"); actionFigur.neuerZustand(loader.getFigur(2), "walk"); actionFigur.neuerZustand(loader.getFigur(3), "attack"); diff --git a/Zoelda/src/main/entities/Spider.java b/Zoelda/src/main/entities/Spider.java index 4cd2782..786d644 100644 --- a/Zoelda/src/main/entities/Spider.java +++ b/Zoelda/src/main/entities/Spider.java @@ -7,10 +7,10 @@ import main.entities.player.Player; public class Spider extends LivingEntity { - private static SheetLoader loader = new SheetLoader("/res/images/spider_sprite_sheet.png", 32, 32, new int[] { 5, 3, 6, 9, 9, 8 }); + private static SheetLoader loader = new SheetLoader("/res/images/spider_sprite_sheet.png"); public Spider() { - super(loader.getFigur(0), "idle"); + loader.generateFigures(32, 32, new int[] { 5, 3, 6, 9, 9, 8 }); width = 0.7f; height = 0.8f; @@ -19,7 +19,8 @@ public class Spider extends LivingEntity { posX = 4f; posY = 4f; mirrored = true; - + + actionFigur.neuerZustand(loader.getFigur(0), "idle"); actionFigur.neuerZustand(loader.getFigur(1), "lost_sight"); actionFigur.neuerZustand(loader.getFigur(2), "walk"); actionFigur.neuerZustand(loader.getFigur(3), "attack"); diff --git a/Zoelda/src/main/entities/player/Player.java b/Zoelda/src/main/entities/player/Player.java index 5820567..bd4eff1 100644 --- a/Zoelda/src/main/entities/player/Player.java +++ b/Zoelda/src/main/entities/player/Player.java @@ -11,13 +11,12 @@ import main.entities.LivingEntity; public class Player extends LivingEntity { - private static SheetLoader loader = new SheetLoader("/res/images/player_sprite_sheet.png", 64, 32, - new int[] { 5, 8, 7, 6, 2, 5, 4, 7 }); + private static SheetLoader loader = new SheetLoader("/res/images/player_sprite_sheet.png"); private boolean onlyAttackOnceTrigger; private float range = 1.2f; // die reichweite der Attacke public Player() { - super(loader.getFigur(0), "idle"); + loader.generateFigures(64, 32, new int[] { 5, 8, 7, 6, 2, 5, 4, 7 }); // Entity-Eigenschaften werden festgelegt width = 0.7f; diff --git a/Zoelda/src/main/worlds/TestWorld.java b/Zoelda/src/main/worlds/TestWorld.java index 41e0542..9b4f459 100644 --- a/Zoelda/src/main/worlds/TestWorld.java +++ b/Zoelda/src/main/worlds/TestWorld.java @@ -6,11 +6,11 @@ import main.maps.Map; public class TestWorld extends World { public TestWorld() { - super(5); + super(4); - Map m2 = maps[7].clone(); + Map m2 = maps[3].clone(); getCurrentMap().connectDoors(getCurrentMap().leftDoor, m2.leftDoor); - + getCurrentMap().addLivingEntity(new Snake()); }