forked from jzitnik/twodcraft
feat: Added reducing fall damage
This commit is contained in:
parent
f20d17adf6
commit
36e57bbb8d
@ -330,7 +330,10 @@ public class Game extends AutoTransientSupport {
|
||||
|
||||
screenRenderer.render(this);
|
||||
} else {
|
||||
player.fell();
|
||||
ArrayList<Block> combinedList = new ArrayList<>();
|
||||
combinedList.addAll(world[cords2[1]][cords2[0]]);
|
||||
combinedList.addAll(world[cords2[1] + 1][cords2[0]]);
|
||||
player.fell(combinedList);
|
||||
screenRenderer.render(this);
|
||||
break;
|
||||
}
|
||||
|
@ -0,0 +1,16 @@
|
||||
package cz.jzitnik.game.annotations;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import cz.jzitnik.game.core.reducefalldamage.Reducer;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
@RequireAnnotation(BlockRegistry.class)
|
||||
public @interface ReduceFallDamage {
|
||||
Class<? extends Reducer> value();
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package cz.jzitnik.game.core.reducefalldamage;
|
||||
|
||||
public class BedFallDamageReducer implements Reducer {
|
||||
@Override
|
||||
public int reduce(int initial) {
|
||||
return (int) (initial * 0.5);
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package cz.jzitnik.game.core.reducefalldamage;
|
||||
|
||||
public class HaybaleFallDamageReducer implements Reducer {
|
||||
@Override
|
||||
public int reduce(int initial) {
|
||||
return (int) (initial * 0.2);
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package cz.jzitnik.game.core.reducefalldamage;
|
||||
|
||||
public interface Reducer {
|
||||
int reduce(int initial);
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package cz.jzitnik.game.core.reducefalldamage;
|
||||
|
||||
public class WaterFallDamageReducer implements Reducer {
|
||||
@Override
|
||||
public int reduce(int ignored) {
|
||||
return 0;
|
||||
}
|
||||
}
|
@ -4,6 +4,11 @@ import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.List;
|
||||
|
||||
import cz.jzitnik.game.annotations.ReduceFallDamage;
|
||||
import cz.jzitnik.game.core.reducefalldamage.Reducer;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ -33,8 +38,20 @@ public class Player implements Serializable {
|
||||
fallDistance++;
|
||||
}
|
||||
|
||||
public void fell() {
|
||||
public void fell(List<Block> fallblock) {
|
||||
var block = fallblock.stream().filter(b -> b.getClass().isAnnotationPresent(ReduceFallDamage.class)).findFirst();
|
||||
int damage = Math.max(fallDistance - 3, 0);
|
||||
if (block.isPresent()) {
|
||||
var reducerClass = block.get().getClass().getAnnotation(ReduceFallDamage.class).value();
|
||||
try {
|
||||
Reducer reducer = reducerClass.getDeclaredConstructor().newInstance();
|
||||
damage = reducer.reduce(damage);
|
||||
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
|
||||
| InvocationTargetException | NoSuchMethodException | SecurityException e) {
|
||||
e.printStackTrace();
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
dealDamage(damage);
|
||||
fallDistance = 0;
|
||||
}
|
||||
|
@ -2,12 +2,15 @@ package cz.jzitnik.game.entities.items.registry.blocks.blocks;
|
||||
|
||||
import cz.jzitnik.game.SpriteLoader;
|
||||
import cz.jzitnik.game.annotations.BlockRegistry;
|
||||
import cz.jzitnik.game.annotations.ReduceFallDamage;
|
||||
import cz.jzitnik.game.core.reducefalldamage.HaybaleFallDamageReducer;
|
||||
import cz.jzitnik.game.entities.Block;
|
||||
import cz.jzitnik.game.entities.items.ItemType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
@BlockRegistry("haybale")
|
||||
@ReduceFallDamage(HaybaleFallDamageReducer.class)
|
||||
public class HaybaleBlock extends Block {
|
||||
public HaybaleBlock() {
|
||||
super("haybale", SpriteLoader.SPRITES.HAYBALE, 3, ItemType.HOE, new ArrayList<>());
|
||||
|
@ -2,10 +2,13 @@ package cz.jzitnik.game.entities.items.registry.blocks.blocks;
|
||||
|
||||
import cz.jzitnik.game.SpriteLoader;
|
||||
import cz.jzitnik.game.annotations.BlockRegistry;
|
||||
import cz.jzitnik.game.annotations.ReduceFallDamage;
|
||||
import cz.jzitnik.game.core.reducefalldamage.WaterFallDamageReducer;
|
||||
import cz.jzitnik.game.entities.Block;
|
||||
import cz.jzitnik.game.logic.services.flowing.FlowingData;
|
||||
import cz.jzitnik.game.sprites.Water;
|
||||
|
||||
@ReduceFallDamage(WaterFallDamageReducer.class)
|
||||
@BlockRegistry(value = "water", drops = "water_bucket")
|
||||
public class WaterBlock extends Block {
|
||||
public WaterBlock() {
|
||||
|
@ -2,9 +2,12 @@ package cz.jzitnik.game.entities.items.registry.blocks.work;
|
||||
|
||||
import cz.jzitnik.game.SpriteLoader;
|
||||
import cz.jzitnik.game.annotations.BlockRegistry;
|
||||
import cz.jzitnik.game.annotations.ReduceFallDamage;
|
||||
import cz.jzitnik.game.core.reducefalldamage.BedFallDamageReducer;
|
||||
import cz.jzitnik.game.entities.Block;
|
||||
import cz.jzitnik.game.sprites.Bed;
|
||||
|
||||
@ReduceFallDamage(BedFallDamageReducer.class)
|
||||
@BlockRegistry("bed")
|
||||
public class BedBlock extends Block {
|
||||
public BedBlock() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user