--- title: Composants NPC type: docs weight: 2 --- Les composants NPC sont des composants ECS qui stockent les données et l'état des NPCs. Le système inclut plus de 300 composants de base pour diverses fonctionnalités NPC. **Package:** `com.hypixel.hytale.server.npc.corecomponents` ## Vue d'Ensemble des Composants Les composants NPC suivent le pattern ECS où les composants sont des conteneurs de données pures attachés aux entity stores. ```java // Accéder aux composants NPC EntityStore store = npc.getEntityStore(); NPCBrainComponent brain = store.getComponent(NPCBrainComponent.class); ``` ## Composants Cerveau ### NPCBrainComponent Le composant principal de traitement IA : ```java public class NPCBrainComponent { private DecisionMaker decisionMaker; private float thinkInterval; private float lastThinkTime; public void setDecisionMaker(DecisionMaker maker); public DecisionMaker getDecisionMaker(); public boolean shouldThink(float currentTime); } ``` ### NPCMemoryComponent Stocke les mémoires et connaissances du NPC : ```java public class NPCMemoryComponent { private Map memories; private float memoryDuration; public void remember(String key, Object value, float duration); public T recall(String key, Class type); public boolean hasMemory(String key); public void forget(String key); } ``` ## Composants d'État ### NPCStateComponent État comportemental actuel : ```java public class NPCStateComponent { private NPCState currentState; private NPCState previousState; private float stateEnterTime; public enum NPCState { IDLE, WALKING, RUNNING, ATTACKING, FLEEING, INTERACTING, SLEEPING, DEAD } } ``` ### NPCAlertComponent Niveau de vigilance et de conscience : ```java public class NPCAlertComponent { private AlertLevel alertLevel; private float alertDecayRate; private Entity alertSource; public enum AlertLevel { RELAXED, // État normal CURIOUS, // Quelque chose a attiré l'attention ALERT, // Investigation active ALARMED, // Menace détectée COMBAT // En combat } } ``` ## Composants de Cible ### NPCTargetComponent Suivi de cible actuel : ```java public class NPCTargetComponent { private Ref currentTarget; private TargetType targetType; private float targetAcquiredTime; private Vector3d lastKnownPosition; public enum TargetType { HOSTILE, FRIENDLY, NEUTRAL, OBJECT } } ``` ### NPCFocusComponent Point de focus visuel : ```java public class NPCFocusComponent { private Vector3d focusPoint; private Ref focusEntity; private float focusStrength; private boolean shouldLookAt; } ``` ## Composants de Mouvement ### NPCMovementComponent Configuration de mouvement : ```java public class NPCMovementComponent { private float walkSpeed; private float runSpeed; private float turnSpeed; private boolean canJump; private boolean canSwim; private boolean canClimb; } ``` ### NPCPathComponent Données de suivi de chemin : ```java public class NPCPathComponent { private List currentPath; private int currentWaypointIndex; private float pathRecalculateInterval; private float lastPathTime; public Vector3d getCurrentWaypoint(); public Vector3d getNextWaypoint(); public boolean hasReachedWaypoint(Vector3d position, float threshold); } ``` ### NPCNavigationComponent Paramètres de navigation : ```java public class NPCNavigationComponent { private float avoidanceRadius; private float pathfindingRange; private int maxPathLength; private NavigationFlags flags; public static class NavigationFlags { public boolean avoidWater; public boolean avoidFire; public boolean canUseDoors; public boolean canBreakBlocks; } } ``` ## Composants de Combat ### NPCCombatComponent Capacités de combat : ```java public class NPCCombatComponent { private float attackRange; private float attackCooldown; private float lastAttackTime; private DamageType preferredDamageType; private List availableAttacks; } ``` ### NPCAggroComponent Gestion de l'aggro : ```java public class NPCAggroComponent { private Map, Float> aggroTable; private float aggroDecayRate; private float aggroRange; public void addAggro(Ref entity, float amount); public void removeAggro(Ref entity); public Ref getHighestAggroTarget(); } ``` ## Composants Sociaux ### NPCFactionComponent Affiliation de faction : ```java public class NPCFactionComponent { private String factionId; private Map relations; public enum FactionRelation { ALLIED, FRIENDLY, NEUTRAL, UNFRIENDLY, HOSTILE } public FactionRelation getRelation(String otherFaction); } ``` ### NPCDialogueComponent Capacités de dialogue : ```java public class NPCDialogueComponent { private String dialogueTreeId; private Map dialogueFlags; private Ref currentSpeaker; public boolean hasDialogue(); public void startDialogue(Player player); } ``` ## Composants Utilitaires ### NPCScheduleComponent Programme journalier : ```java public class NPCScheduleComponent { private Map schedule; public static class ScheduleEntry { public int startHour; public int endHour; public String activity; public Vector3d location; } public ScheduleEntry getCurrentActivity(int worldHour); } ``` ### NPCInventoryComponent Inventaire NPC : ```java public class NPCInventoryComponent { private ItemContainer inventory; private ItemStack equippedWeapon; private ItemStack equippedArmor; public ItemStack getEquippedWeapon(); public void equipItem(ItemStack item); } ``` ## Enregistrer des Composants Personnalisés ```java public class MyPlugin extends JavaPlugin { @Override public void start() { // Enregistrer un composant NPC personnalisé getEntityStoreRegistry().registerComponent( "custom_npc_data", CustomNPCComponent.class, CustomNPCComponent::new ); } } public class CustomNPCComponent { private String customData; private int customValue; // Champs de données du composant } ``` ## Patterns d'Accès aux Composants ```java // Accès sécurisé aux composants public void processNPC(NPCEntity npc) { EntityStore store = npc.getEntityStore(); // Vérifier si le composant existe if (store.hasComponent(NPCCombatComponent.class)) { NPCCombatComponent combat = store.getComponent(NPCCombatComponent.class); // Traiter la logique de combat } // Obtenir ou créer un composant NPCStateComponent state = store.getOrCreateComponent( NPCStateComponent.class, NPCStateComponent::new ); } ``` ## Bonnes Pratiques {{< callout type="info" >}} **Directives des Composants :** - Les composants doivent être des données pures - pas de logique complexe - Utilisez les composants pour l'état qui doit persister - Accédez aux composants via EntityStore, pas directement - Vérifiez l'existence du composant avant l'accès - Utilisez le composant approprié pour chaque type de données {{< /callout >}}