-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add experience attribute * init hungerManager and experienceManager to function initEntity()
- Loading branch information
1 parent
9bc5073
commit 4f9b9f4
Showing
7 changed files
with
379 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 175 additions & 0 deletions
175
src/main/java/org/sculk/entity/manager/ExperienceManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
package org.sculk.entity.manager; | ||
|
||
|
||
import org.sculk.entity.Attribute; | ||
import org.sculk.entity.AttributeFactory; | ||
import org.sculk.entity.Entity; | ||
import org.sculk.entity.HumanEntity; | ||
import org.sculk.event.player.PlayerExperienceChangeEvent; | ||
import org.sculk.utils.ExperienceUtils; | ||
|
||
import java.util.Map; | ||
|
||
/* | ||
* ____ _ _ | ||
* / ___| ___ _ _| | | __ | ||
* \___ \ / __| | | | | |/ / | ||
* ___) | (__| |_| | | < | ||
* |____/ \___|\__,_|_|_|\_\ | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* @author: SculkTeams | ||
* @link: http://www.sculkmp.org/ | ||
*/ | ||
public class ExperienceManager { | ||
|
||
private Attribute levelAttribute; | ||
private Attribute progressAttribute; | ||
|
||
private int totalXp = 0; | ||
private boolean canAttractXpOrbs = true; | ||
private int xpCooldown = 0; | ||
|
||
private HumanEntity humanEntity; | ||
|
||
public ExperienceManager(HumanEntity humanEntity) { | ||
this.humanEntity = humanEntity; | ||
this.levelAttribute = fetchAttribute(humanEntity, Attribute.EXPERIENCE_LEVEL); | ||
this.progressAttribute = fetchAttribute(humanEntity, Attribute.EXPERIENCE); | ||
} | ||
|
||
private static Attribute fetchAttribute(Entity entity, String attributeId) { | ||
Attribute attribute = AttributeFactory.getINSTANCE().mustGet(attributeId); | ||
// TODO next step add attribute to entity | ||
return attribute; | ||
} | ||
|
||
public boolean setXpAndProgress(Integer level, Float progress) { | ||
PlayerExperienceChangeEvent playerExperienceChangeEvent = new PlayerExperienceChangeEvent(this.humanEntity, getXpLevel(), getXpProgress(), level, progress); | ||
playerExperienceChangeEvent.call(); | ||
|
||
if(playerExperienceChangeEvent.isCancelled()) { | ||
return false; | ||
} | ||
level = playerExperienceChangeEvent.getNewLevel(); | ||
progress = playerExperienceChangeEvent.getNewProgress(); | ||
|
||
if(level != null) { | ||
this.levelAttribute.setValue(level, true, true); | ||
} | ||
if(progress != null) { | ||
this.progressAttribute.setValue(progress, true, true); | ||
} | ||
return true; | ||
} | ||
|
||
public int getXpLevel() { | ||
return (int) this.levelAttribute.getCurrentValue(); | ||
} | ||
|
||
public boolean setXpLevel(int level) { | ||
return this.setXpAndProgress(level, null); | ||
} | ||
|
||
public boolean addXpLevels(int amount) { | ||
int oldLevel = this.getXpLevel(); | ||
return this.setXpLevel(oldLevel + amount); | ||
} | ||
|
||
public boolean substractXpLevels(int amount) { | ||
return this.addXpLevels(-amount); | ||
} | ||
|
||
public float getXpProgress() { | ||
return this.progressAttribute.getCurrentValue(); | ||
} | ||
|
||
public boolean setXpProgress(float progress) { | ||
return this.setXpAndProgress(null, progress); | ||
} | ||
|
||
public int getRemainderXp() { | ||
return (int) (ExperienceUtils.getXpToCompleteLevel(this.getXpLevel()) * this.getXpProgress()); | ||
} | ||
|
||
public int getCurrentTotalXp() { | ||
return ExperienceUtils.getXpToReachLevel(this.getXpLevel()) + this.getRemainderXp(); | ||
} | ||
|
||
public boolean setCurrentTotalXp(int amount) { | ||
float newLevel = ExperienceUtils.getLevelFromXp(amount); | ||
int xpLevel = (int) (newLevel - (int) newLevel); | ||
float xpProgress = (int) (newLevel - (int) newLevel); | ||
return setXpAndProgress(xpLevel, xpProgress); | ||
} | ||
|
||
public boolean addXp(int amount) { | ||
amount = Math.min(amount, Integer.MAX_VALUE - this.totalXp); | ||
int oldLevel = this.getXpLevel(); | ||
int oldTotal = this.getCurrentTotalXp(); | ||
if(this.setCurrentTotalXp(oldTotal + amount)) { | ||
if(amount > 0) { | ||
this.totalXp += amount; | ||
} | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public boolean subtractXp(int amount) { | ||
return this.addXp(-amount); | ||
} | ||
|
||
public void setXpAndProgressNoEvent(int level, float progress) { | ||
this.levelAttribute.setValue(level, true, true); | ||
this.progressAttribute.setValue(progress, true, true); | ||
} | ||
|
||
public int getLifetimeTotalXp() { | ||
return this.totalXp; | ||
} | ||
|
||
public void setLifetimeTotalXp(int amount) { | ||
if(amount < 0 || amount > Integer.MAX_VALUE) { | ||
throw new IllegalArgumentException("XP must be greater than 0 and less than " + Integer.MAX_VALUE); | ||
} | ||
this.totalXp = amount; | ||
} | ||
|
||
public boolean canPickupXp() { | ||
return this.xpCooldown == 0; | ||
} | ||
|
||
public void onPickupXp(int xpValue) { | ||
int mainHandIndex = -1; | ||
int offHandIndex = -2; | ||
|
||
// TODO: Logic for repair item | ||
|
||
this.addXp(xpValue); | ||
this.resetXpCooldown(); | ||
} | ||
|
||
public void resetXpCooldown() { | ||
this.xpCooldown = 2; | ||
} | ||
|
||
public void tick(int tickDiff) { | ||
if(this.xpCooldown > 0) { | ||
this.xpCooldown = Math.max(0, this.xpCooldown - tickDiff); | ||
} | ||
} | ||
|
||
public boolean canAttractXpOrbs() { | ||
return this.canAttractXpOrbs; | ||
} | ||
|
||
public void setCanAttractXpOrbs(boolean canAttractXpOrbs) { | ||
this.canAttractXpOrbs = canAttractXpOrbs; | ||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/org/sculk/event/player/PlayerExperienceChangeEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package org.sculk.event.player; | ||
|
||
|
||
import org.sculk.entity.HumanEntity; | ||
import org.sculk.event.Cancellable; | ||
import org.sculk.event.EntityEvent; | ||
|
||
/* | ||
* ____ _ _ | ||
* / ___| ___ _ _| | | __ | ||
* \___ \ / __| | | | | |/ / | ||
* ___) | (__| |_| | | < | ||
* |____/ \___|\__,_|_|_|\_\ | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* @author: SculkTeams | ||
* @link: http://www.sculkmp.org/ | ||
*/ | ||
public class PlayerExperienceChangeEvent extends EntityEvent implements Cancellable { | ||
|
||
protected HumanEntity humanEntity; | ||
protected int oldLevel; | ||
protected float oldProgress; | ||
protected int newLevel; | ||
protected float newProgress; | ||
|
||
public PlayerExperienceChangeEvent(HumanEntity humanEntity, int oldLevel, float oldProgress, int newLevel, float newProgress) { | ||
this.humanEntity = humanEntity; | ||
this.oldLevel = oldLevel; | ||
this.oldProgress = oldProgress; | ||
this.newLevel = newLevel; | ||
this.newProgress = newProgress; | ||
} | ||
|
||
public int getOldLevel() { | ||
return oldLevel; | ||
} | ||
|
||
public float getOldProgress() { | ||
return oldProgress; | ||
} | ||
|
||
public int getNewLevel() { | ||
return newLevel; | ||
} | ||
|
||
public float getNewProgress() { | ||
return newProgress; | ||
} | ||
|
||
public void setNewLevel(int newLevel) { | ||
this.newLevel = newLevel; | ||
} | ||
|
||
public void setNewProgress(float newProgress) { | ||
if(newProgress < 0.0 || newProgress > 1.0) { | ||
throw new IllegalArgumentException("XP progress must be range 0-1"); | ||
} | ||
this.newProgress = newProgress; | ||
} | ||
} |
Oops, something went wrong.