add retire and evolve method of hero

This commit is contained in:
zhl 2021-12-27 18:25:18 +08:00
parent 6b77ef79d7
commit 40abe9a694

View File

@ -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,12 +58,20 @@ contract CryptoHero is Ownable, IERC721 {
return (_interfaceId == _InterfaceIdERC721 || _interfaceId == _InterfaceIdERC165);
}
function createHeroGen0(uint256 genes) public onlyOwner returns (uint256) {
/**
* 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(
uint256 _mumId,
uint256 _dadId,
@ -77,10 +89,21 @@ contract CryptoHero is Ownable, IERC721 {
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);
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,
uint256 birthTime,