// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; import "@openzeppelin/contracts/utils/Counters.sol"; import "@openzeppelin/contracts/access/Ownable.sol"; contract GreekMythologyGame is ERC721, Ownable { using Counters for Counters.Counter; Counters.Counter private _tokenIdCounter; struct Hero { string name; uint256 strength; uint256 agility; uint256 defense; uint256 rarity; // 1 a 5, donde 5 es muy raro } // Mapeo de héroes mapping(uint256 => Hero) public heroes; // Mapeo de héroes por jugador mapping(address => uint256[]) public playerHeroes; // Eventos event NewHero(address indexed owner, uint256 heroId); event BattleResult(address indexed winner, uint256 winnerHeroId, uint256 loserHeroId); // Constructor constructor() ERC721("GreekHeroes", "GHERO") {} // Función para crear un héroe mitológico function createHero( string memory name, uint256 strength, uint256 agility, uint256 defense, uint256 rarity ) public onlyOwner { require(rarity >= 1 && rarity <= 5, "La rareza debe estar entre 1 y 5"); uint256 tokenId = _tokenIdCounter.current(); _safeMint(msg.sender, tokenId); heroes[tokenId] = Hero(name, strength, agility, defense, rarity); playerHeroes[msg.sender].push(tokenId); _tokenIdCounter.increment(); emit NewHero(msg.sender, tokenId); } // Función para iniciar una batalla entre dos héroes function battle(uint256 hero1Id, uint256 hero2Id) public { require(ownerOf(hero1Id) == msg.sender, "No eres el dueño de este heroe"); require(_exists(hero2Id), "El heroe o criatura no existe"); Hero storage hero1 = heroes[hero1Id]; Hero storage hero2 = heroes[hero2Id]; // Sistema básico de combate uint256 hero1Power = hero1.strength + hero1.agility + hero1.defense; uint256 hero2Power = hero2.strength + hero2.agility + hero2.defense; address winner; uint256 winnerHeroId; uint256 loserHeroId; if (hero1Power >= hero2Power) { winner = ownerOf(hero1Id); winnerHeroId = hero1Id; loserHeroId = hero2Id; } else { winner = ownerOf(hero2Id); winnerHeroId = hero2Id; loserHeroId = hero1Id; } emit BattleResult(winner, winnerHeroId, loserHeroId); } // Obtener héroes de un jugador function getPlayerHeroes(address player) public view returns (uint256[] memory) { return playerHeroes[player]; } }
0.4.18