diff --git a/contracts/CryptoHero.sol b/contracts/CryptoHero.sol index 073ee90..b53fa5b 100644 --- a/contracts/CryptoHero.sol +++ b/contracts/CryptoHero.sol @@ -13,6 +13,8 @@ contract CryptoHero is Ownable, IERC721 { uint256 public gen0Counter = 0; + uint256 initHeroPrice = 0.01 ether; + bytes4 internal constant _ERC721Checksum = bytes4(keccak256("onERC721Received(address,address,uint256,bytes)")); bytes4 private constant _InterfaceIdERC721 = 0x80ac58cd; @@ -38,7 +40,9 @@ contract CryptoHero is Ownable, IERC721 { mapping(address => mapping (address => bool)) private _operatorApprovals; - event Birth(address owner, uint256 heroId, uint256 mumId, uint256 dadId, uint256 genes); + event HeroBirth(address owner, uint256 _heroId, uint256 mumId, uint256 dadId, uint256 genes); + event HeroEvolved(uint256 indexed _heroId, uint256 _oldGenes, uint256 _newGenes); + event HeroRetired(uint256 indexed _heroId); constructor(string memory dname, string memory dsymbol) { _name = dname; @@ -54,10 +58,18 @@ contract CryptoHero is Ownable, IERC721 { return (_interfaceId == _InterfaceIdERC721 || _interfaceId == _InterfaceIdERC165); } - function createHeroGen0(uint256 genes) public onlyOwner returns (uint256) { - require(gen0Counter < maxGen0Hero, "Maximum number of Heros is reached. No new hero allowed!"); - gen0Counter = SafeMath.add(gen0Counter, 1); - return _createHero(0, 0, 0, genes, msg.sender); + /** + * for player buy init heros + */ + function buyInitHero(uint256 genes) public payable returns (uint256) { + require(gen0Counter < maxGen0Hero, "Maximum number of Heros is reached. No new hero allowed!"); + require(msg.value >= initHeroPrice, "not enough money!"); + gen0Counter = SafeMath.add(gen0Counter, 1); + return _createHero(0, 0, 0, genes, msg.sender); + } + + function setInitHeroPrice(uint _price) external onlyOwner { + initHeroPrice = _price; } function _createHero( @@ -67,19 +79,30 @@ contract CryptoHero is Ownable, IERC721 { uint256 _genes, address _owner ) internal returns (uint256) { - Hero memory _hero = Hero({ - genes: _genes, - birthTime: uint64(block.timestamp), - mumId: uint32(_mumId), //easier to input 256 and later convert to 32. - dadId: uint32(_dadId), - generation: uint16(_generation) - }); - heros.push(_hero); - uint256 newHeroId = SafeMath.sub(heros.length, 1);//want to start with zero. - _transfer(address(0), _owner, newHeroId);//transfer from nowhere. Creation event. - emit Birth(_owner, newHeroId, _mumId, _dadId, _genes); - return newHeroId; - } + Hero memory _hero = Hero({ + genes: _genes, + birthTime: uint64(block.timestamp), + mumId: uint32(_mumId), //easier to input 256 and later convert to 32. + dadId: uint32(_dadId), + generation: uint16(_generation) + }); + heros.push(_hero); + uint256 newHeroId = SafeMath.sub(heros.length, 1);//want to start with zero. + _transfer(address(0), _owner, newHeroId);//transfer from nowhere. Creation event. + emit HeroBirth(_owner, newHeroId, _mumId, _dadId, _genes); + return newHeroId; + } + + function retireHero(uint256 heroId) internal { + delete heros[heroId]; + emit HeroRetired(heroId); + } + + function evolveHero(uint256 _heroId, uint256 _newGenes) internal { + uint256 _oldGenes = heros[_heroId].genes; + heros[_heroId].genes = _newGenes; + emit HeroEvolved(_heroId, _oldGenes, _newGenes); + } function getHero(uint256 tokenId) public view returns ( uint256 genes,