Contract 4: GameEngine - The Game Logic Hub

Combat System

function fightEnemy(uint256 tokenId) external nonReentrant {
    require(evolutionNFT.ownerOf(tokenId) == msg.sender, "Not owner");
    require(block.timestamp >= lastActionTime[tokenId] + 1 hours, "Cooldown active");
    
    uint256 currentLevel = traitFusion.getTraitAsUint(address(evolutionNFT), tokenId, "level");
    uint256 currentEnergy = traitFusion.getTraitAsUint(address(evolutionNFT), tokenId, "energy");
    
    require(currentEnergy >= 20, "Not enough energy");

What's happening here?

  • Checks if player owns the NFT

  • Enforces 1-hour cooldown between fights

  • Gets current level and energy from TraitFusion

  • Requires at least 20 energy to fight

// Consume energy
traitFusion.setTraitUint(address(evolutionNFT), tokenId, "energy", currentEnergy - 20);

// Gain XP and potentially level up
uint256 xpGain = 10 + (currentLevel / 2);
uint256 newLevel = currentLevel + (xpGain / 100);

if (newLevel > currentLevel) {
    traitFusion.setTraitUint(address(evolutionNFT), tokenId, "level", newLevel);
    // Increase strength on level up
    uint256 currentStrength = traitFusion.getTraitAsUint(address(evolutionNFT), tokenId, "strength");
    traitFusion.setTraitUint(address(evolutionNFT), tokenId, "strength", currentStrength + 5);
}

What's happening here?

  • Reduces energy by 20 points

  • Calculates XP gain (higher level = more XP)

  • Every 100 XP = 1 level up

  • When leveling up, adds 5 strength points

  • This is where the evolution happens!

Real-World Data Integration

What's happening here?

  • Only oracle can call this (real-world data source)

  • Converts real walking steps to in-game stamina

  • 100 steps = 1 stamina point

  • Caps stamina at 200 to prevent infinite growth

What's happening here?

  • If player walks 10,000+ steps, they get 200 game tokens

  • Real-world activity = in-game rewards!

What's happening here?

  • Player burns 100 tokens to open loot box

  • Requests truly random number from Chainlink VRF

  • Maps the request ID to the token ID for later processing

What's happening here?

  • Chainlink calls this function with random number

  • Converts random number to percentage (0-99)

  • 5% chance for legendary loot (+50 strength)

  • 15% chance for epic loot (+25 strength)

  • 30% chance for rare loot (+10 strength)

  • 50% chance for common loot (+5 strength)

Last updated