Skip to content

Instantly share code, notes, and snippets.

@llSourcell
Created March 5, 2022 17:23
Show Gist options
  • Save llSourcell/a91255bb2ed163979cb5a847740d24ea to your computer and use it in GitHub Desktop.
Save llSourcell/a91255bb2ed163979cb5a847740d24ea to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
//this is an NFT contract
contract NFTGAMEPLAY is ERC721, Ownable {
//define characters in the game
enum type_character { PROFESSOR, LISBON }
//counter variable
uint nextId = 0;
//defining features of a character
struct Character {
uint8 attack;
uint8 defense;
uint life;
uint32 experience;
uint lastHeal;
uint lastFight;
type_character typeCharacter;
}
//create character NFT
function mint(type_character _typeCharacter) public {
require(balanceOf(msg.sender) <= 4, "Already create max characters.");
require(_typeCharacter == type_character.PROFESSOR || _typeCharacter == type_character.LISBON, "Not valid");
if(_typeCharacter == type_character.PROFESSOR) {
Character memory thisCharacter = Character(20, 15, 100, 1, block.timestamp, block.timestamp, type_character.PROFESSOR);
_characterDetails[nextId] = thisCharacter;
_safeMint(msg.sender, nextId);
nextId++;
}
if(_typeCharacter == type_character.LISBON) {
Character memory thisCharacter = Character(13, 25, 80, 1, block.timestamp, block.timestamp, type_character.LISBON);
_characterDetails[nextId] = thisCharacter;
_safeMint(msg.sender, nextId);
nextId++;
}
}
//NFT with more HP wins
function fight(uint _tokenId1, uint _tokenId2) public payable {
// Character 1 kills character 2, character 2 cannot reply
if(_characterDetails[_tokenId2].life - substractLifeToCharacter2 <= 0) {
_characterDetails[_tokenId2].life = 0;
_characterDetails[_tokenId1].experience++;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment