|
@@ -20,10 +20,11 @@ package com.iohao.mmo.equip.entity;
|
|
|
|
|
|
import com.iohao.mmo.common.kit.RandomKit;
|
|
|
import lombok.AccessLevel;
|
|
|
-import lombok.Builder;
|
|
|
import lombok.Data;
|
|
|
import lombok.experimental.FieldDefaults;
|
|
|
import org.springframework.data.mongodb.core.mapping.Document;
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.lang.reflect.Method;
|
|
|
|
|
|
/**
|
|
|
* 装备基础属性
|
|
@@ -35,7 +36,6 @@ import org.springframework.data.mongodb.core.mapping.Document;
|
|
|
* @date 2023-07-24
|
|
|
*/
|
|
|
@Data
|
|
|
-@Builder(toBuilder = true)
|
|
|
@Document
|
|
|
@FieldDefaults(level = AccessLevel.PRIVATE)
|
|
|
public class EquipPropertyBasic {
|
|
@@ -84,50 +84,77 @@ public class EquipPropertyBasic {
|
|
|
{
|
|
|
return equipPropertyBasic;
|
|
|
}
|
|
|
- if(fixedEquipPropertyMin.getPhysicsAttackMin()!=null&&fixedEquipPropertyMax.getPhysicsAttackMin()!=null){
|
|
|
- equipPropertyBasic.toBuilder().physicsAttackMin(RandomKit.randomFromInt(fixedEquipPropertyMin.physicsAttackMin.intValue(),
|
|
|
- fixedEquipPropertyMax.physicsAttackMin.intValue()));
|
|
|
+ try{
|
|
|
+ //通过getDeclaredFields()方法获取属性下限对象类中的所有属性(含私有)
|
|
|
+ Field[] minFields = fixedEquipPropertyMin.getClass().getDeclaredFields();
|
|
|
+ //遍历属性
|
|
|
+ for (Field minField : minFields) {
|
|
|
+ //设置允许通过反射访问私有变量
|
|
|
+ minField.setAccessible(true);
|
|
|
+ //获取属性下限类中对应字段的值
|
|
|
+ Object minValue = minField.get(fixedEquipPropertyMin);
|
|
|
+ //如果获取属性下限类中对应的值为空则跳过
|
|
|
+ if(minValue==null){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ //通过getDeclaredFields()方法获取属性上限对象类中的所有属性(含私有)
|
|
|
+ Field[] maxFields = fixedEquipPropertyMax.getClass().getDeclaredFields();
|
|
|
+ //遍历属性
|
|
|
+ for (Field maxField : maxFields) {
|
|
|
+ //设置允许通过反射访问私有变量
|
|
|
+ maxField.setAccessible(true);
|
|
|
+ //获取属性下限类中当前字段属性名称
|
|
|
+ String minName = minField.getName();
|
|
|
+
|
|
|
+ //获取属性上限类中对应字段的值
|
|
|
+ Object maxValue = maxField.get(fixedEquipPropertyMax);
|
|
|
+ //获取属性上限类中当前字段属性名称
|
|
|
+ String maxName = maxField.getName();
|
|
|
+ //如果下限和上限都取到了同一个属性,并且对应的值都不为空,那么取出来在其中进行范围随机
|
|
|
+ if(minName.equals(maxName)&&maxValue!=null){
|
|
|
+ //在下限和上限之间随机
|
|
|
+ Integer randomValue = RandomKit.randomFromInt(Integer.parseInt(minValue.toString()),
|
|
|
+ Integer.parseInt(maxValue.toString()));
|
|
|
+ //通过属性名称获取对应set方法的名称
|
|
|
+ String methodName = "set" + capitalize(minName);
|
|
|
+ //获取class对象
|
|
|
+ Class<?> clazz = equipPropertyBasic.getClass();
|
|
|
+ //获取赋值的字段类型
|
|
|
+ Class<?> parameterType = randomValue.getClass();
|
|
|
+ //获取set方法
|
|
|
+ Method method = clazz.getMethod(methodName, parameterType);
|
|
|
+ //对equipPropertyBasic的set方法赋值randomValue
|
|
|
+ method.invoke(equipPropertyBasic, randomValue);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ } catch (Exception ex){
|
|
|
+ //处理异常
|
|
|
+ }
|
|
|
+ return equipPropertyBasic;
|
|
|
+ }
|
|
|
+ private static String capitalize(String str) {
|
|
|
+ if (str == null || str.length() == 0) {
|
|
|
+ return str;
|
|
|
}
|
|
|
- return EquipPropertyBasic.builder()
|
|
|
- .physicsAttackMin(
|
|
|
- RandomKit.randomFromInt(fixedEquipPropertyMin.getPhysicsAttackMin(),
|
|
|
- fixedEquipPropertyMax.getPhysicsAttackMin()))
|
|
|
- .physicsAttackMax(
|
|
|
- RandomKit.randomFromInt(fixedEquipPropertyMin.getPhysicsAttackMax(),
|
|
|
- fixedEquipPropertyMax.getPhysicsAttackMax()))
|
|
|
- .magicAttackMin(
|
|
|
- RandomKit.randomFromInt(fixedEquipPropertyMin.getMagicAttackMin(),
|
|
|
- fixedEquipPropertyMax.getMagicAttackMin()))
|
|
|
- .magicAttackMax(
|
|
|
- RandomKit.randomFromInt(fixedEquipPropertyMin.getMagicAttackMax(),
|
|
|
- fixedEquipPropertyMax.getMagicAttackMax()))
|
|
|
- .physicsDefense(
|
|
|
- RandomKit.randomFromInt(fixedEquipPropertyMin.getPhysicsDefense(),
|
|
|
- fixedEquipPropertyMax.getPhysicsDefense()))
|
|
|
- .magicDefense(
|
|
|
- RandomKit.randomFromInt(fixedEquipPropertyMin.getMagicDefense(),
|
|
|
- fixedEquipPropertyMax.getMagicDefense()))
|
|
|
- .magicDefense(
|
|
|
- RandomKit.randomFromInt(fixedEquipPropertyMin.getMagicDefense(),
|
|
|
- fixedEquipPropertyMax.getMagicDefense()))
|
|
|
- .physicsHit(
|
|
|
- RandomKit.randomFromInt(fixedEquipPropertyMin.getPhysicsHit(),
|
|
|
- fixedEquipPropertyMax.getPhysicsHit()))
|
|
|
- .physicsDodge(
|
|
|
- RandomKit.randomFromInt(fixedEquipPropertyMin.getPhysicsDodge(),
|
|
|
- fixedEquipPropertyMax.getPhysicsDodge()))
|
|
|
- .magicHit(
|
|
|
- RandomKit.randomFromInt(fixedEquipPropertyMin.getMagicHit(),
|
|
|
- fixedEquipPropertyMax.getMagicHit()))
|
|
|
- .magicDodge(
|
|
|
- RandomKit.randomFromInt(fixedEquipPropertyMin.getMagicDodge(),
|
|
|
- fixedEquipPropertyMax.getMagicDodge()))
|
|
|
- .hpPct(
|
|
|
- RandomKit.randomFromBigDecimal(fixedEquipPropertyMin.getHpPct(),
|
|
|
- fixedEquipPropertyMax.getHpPct()))
|
|
|
- .durability(
|
|
|
- RandomKit.randomFromInt(fixedEquipPropertyMin.getDurability(),
|
|
|
- fixedEquipPropertyMax.getDurability()))
|
|
|
- .build();
|
|
|
+ char firstChar = str.charAt(0);
|
|
|
+ if (Character.isUpperCase(firstChar)) {
|
|
|
+ return str;
|
|
|
+ } else {
|
|
|
+ return Character.toUpperCase(firstChar) + str.substring(1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public static void main(String[] args) {
|
|
|
+ EquipPropertyBasic fixedEquipPropertyMin = new EquipPropertyBasic();
|
|
|
+ fixedEquipPropertyMin.setHp(10);
|
|
|
+ fixedEquipPropertyMin.setMp(100);
|
|
|
+ EquipPropertyBasic fixedEquipPropertyMax = new EquipPropertyBasic();
|
|
|
+ fixedEquipPropertyMax.setHp(50);
|
|
|
+ fixedEquipPropertyMax.setMp(200);
|
|
|
+
|
|
|
+ EquipPropertyBasic equipPropertyBasic = randomFixed(fixedEquipPropertyMin,fixedEquipPropertyMax);
|
|
|
+ System.out.println(equipPropertyBasic.getHp());
|
|
|
}
|
|
|
}
|