Added function to attack ship
This commit is contained in:
parent
b101d0f1d9
commit
16a2b28791
|
@ -8,7 +8,7 @@ contract SpaceBattle {
|
|||
struct ShipState {
|
||||
uint x;
|
||||
uint y;
|
||||
uint health;
|
||||
int health;
|
||||
address owner;
|
||||
uint lastActionBlock;
|
||||
}
|
||||
|
@ -21,7 +21,9 @@ contract SpaceBattle {
|
|||
|
||||
event ShipJoinedBattle(uint indexed shipId, uint x, uint y);
|
||||
event ShipLeftBattle(uint indexed shipId);
|
||||
event ShipRepaired(uint indexed shipId, uint health);
|
||||
event ShipRepaired(uint indexed shipId, int health);
|
||||
event ShipDestroyed(uint indexed shipId, uint indexed destroyedBy);
|
||||
event ShipAttacked(uint indexed shipId, uint indexed attackedBy, int health);
|
||||
|
||||
constructor(SpaceshipToken _token) public{
|
||||
token = _token;
|
||||
|
@ -34,7 +36,7 @@ contract SpaceBattle {
|
|||
|
||||
gameGrid[_x][_y] = _myShipId;
|
||||
|
||||
uint HP;
|
||||
int HP;
|
||||
(HP,,,,,,) = token.getAttributes(_myShipId);
|
||||
|
||||
shipState[_myShipId].owner = msg.sender;
|
||||
|
@ -48,9 +50,12 @@ contract SpaceBattle {
|
|||
modifier requiresCooldown(uint _myShipId){
|
||||
uint cooldown;
|
||||
(,,,,,, cooldown) = token.getAttributes(_myShipId);
|
||||
ShipState memory state = shipState[_myShipId];
|
||||
ShipState storage state = shipState[_myShipId];
|
||||
require(state.lastActionBlock + cooldown < block.number);
|
||||
|
||||
_;
|
||||
|
||||
state.lastActionBlock = block.number;
|
||||
}
|
||||
|
||||
function hasEnemy(uint _x, uint _y) private returns(bool){
|
||||
|
@ -91,20 +96,56 @@ contract SpaceBattle {
|
|||
state.y = _y;
|
||||
}
|
||||
|
||||
function attack(uint _myShipId, uint _shipId) public requiresCooldown(_myShipId) {
|
||||
// Attack must take in account the level
|
||||
// Attack will reduce HP, and takes in account defense and level
|
||||
// Enemigo debe estar al lado
|
||||
function attack(uint _myShipId, uint _enemyShipId) public requiresCooldown(_myShipId) {
|
||||
ShipState storage myState = shipState[_myShipId];
|
||||
ShipState storage enemyState = shipState[_enemyShipId];
|
||||
|
||||
require(myState.owner == msg.sender);
|
||||
require(canAttack(_myShipId, _enemyShipId));
|
||||
|
||||
int myHP;
|
||||
uint myAttack;
|
||||
uint enemyAttack;
|
||||
uint enemyDefense;
|
||||
uint myLevel;
|
||||
uint enemyLevel;
|
||||
|
||||
(myHP,myAttack,,,,myLevel,) = token.getAttributes(_myShipId);
|
||||
(,enemyAttack,enemyDefense,,,enemyLevel,) = token.getAttributes(_enemyShipId);
|
||||
|
||||
int attackPower = int(myLevel * myAttack - enemyLevel * enemyLevel);
|
||||
if(attackPower < 0)
|
||||
attackPower = 0;
|
||||
|
||||
enemyState.health -= attackPower;
|
||||
|
||||
if(enemyState.health < 0){
|
||||
// Reparamos la mitad del dano de la nave
|
||||
myState.health += (myHP - myState.health) / 2;
|
||||
if(token.gainExperience(_myShipId, enemyLevel * enemyAttack * enemyDefense)){
|
||||
(myHP,,,,,,) = token.getAttributes(_myShipId);
|
||||
myState.health = myHP;
|
||||
}
|
||||
|
||||
// Destruir enemigo
|
||||
token.destroyShip(_enemyShipId);
|
||||
delete gameGrid[enemyState.x][enemyState.y];
|
||||
delete shipState[_enemyShipId];
|
||||
|
||||
emit ShipDestroyed(_enemyShipId, _myShipId);
|
||||
} else {
|
||||
emit ShipAttacked(_enemyShipId, _myShipId, enemyState.health);
|
||||
}
|
||||
}
|
||||
|
||||
function repair(uint _myShipId) payable requiresCooldown(_myShipId) {
|
||||
uint cost = repairCost(_myShipId);
|
||||
uint cost = uint(repairCost(_myShipId));
|
||||
ShipState storage myState = shipState[_myShipId];
|
||||
|
||||
require(msg.value >= cost);
|
||||
require(myState.owner == msg.sender);
|
||||
|
||||
uint HP;
|
||||
int HP;
|
||||
(HP,,,,,,) = token.getAttributes(_myShipId);
|
||||
|
||||
myState.health = HP;
|
||||
|
@ -177,13 +218,13 @@ contract SpaceBattle {
|
|||
(myState.x == enemyState.x && myState.y - 1 == enemyState.y);
|
||||
}
|
||||
|
||||
function repairCost(uint _myShipId) public view returns(uint) {
|
||||
function repairCost(uint _myShipId) public view returns(int) {
|
||||
ShipState memory state = shipState[_myShipId];
|
||||
|
||||
uint HP;
|
||||
int HP;
|
||||
(HP,,,,,,) = token.getAttributes(_myShipId);
|
||||
|
||||
uint missingHealth = HP - state.health;
|
||||
int missingHealth = HP - state.health;
|
||||
|
||||
return missingHealth * 2000; // TODO: tune this
|
||||
}
|
||||
|
|
|
@ -12,23 +12,25 @@ contract SpaceshipToken is ERC721Token("SpaceshipToken", "SST"), Ownable {
|
|||
/// @dev Estructura que representa nuestra nave spacial
|
||||
struct Spaceship {
|
||||
bytes32 metadataHash; // IPFS Hash
|
||||
uint HP;
|
||||
int HP;
|
||||
uint8 attack;
|
||||
uint8 defense;
|
||||
uint8 speed;
|
||||
uint cooldown;
|
||||
uint8 level;
|
||||
uint level;
|
||||
uint experience;
|
||||
}
|
||||
|
||||
// Todas las naves que se han creado.
|
||||
Spaceship[] public spaceships;
|
||||
|
||||
event LevelUp(uint spaceshipId, uint level);
|
||||
|
||||
// Precio de las naves
|
||||
mapping(uint => uint) spaceshipPrices;
|
||||
|
||||
function mint(bytes32 _metadataHash,
|
||||
uint8 _HP,
|
||||
int _HP,
|
||||
uint8 _attack,
|
||||
uint8 _defense,
|
||||
uint8 _speed,
|
||||
|
@ -86,43 +88,38 @@ contract SpaceshipToken is ERC721Token("SpaceshipToken", "SST"), Ownable {
|
|||
_;
|
||||
}
|
||||
|
||||
function updateShipHP(uint _shipId, uint _HP) onlySpaceBattle {
|
||||
require(ownerOf(_shipId) != address(0));
|
||||
|
||||
Spaceship storage s = spaceships[_shipId];
|
||||
|
||||
if(_HP == 0){
|
||||
// Destroy token
|
||||
_burn(msg.sender, _shipId);
|
||||
} else {
|
||||
s.HP = _HP;
|
||||
}
|
||||
function destroyShip(uint _spaceshipId) onlySpaceBattle {
|
||||
require(ownerOf(_spaceshipId) != address(0));
|
||||
_burn(msg.sender, _spaceshipId);
|
||||
}
|
||||
|
||||
function gainExperience(uint _shipId, uint _experience) onlySpaceBattle {
|
||||
require(ownerOf(_shipId) != address(0));
|
||||
function gainExperience(uint _spaceshipId, uint _experience) onlySpaceBattle returns(bool levelUp){
|
||||
require(ownerOf(_spaceshipId) != address(0));
|
||||
|
||||
Spaceship storage s = spaceships[_shipId];
|
||||
Spaceship storage s = spaceships[_spaceshipId];
|
||||
|
||||
uint currentExperience = s.experience;
|
||||
if(currentExperience + _experience > s.level * 100){
|
||||
s.level++;
|
||||
s.HP = s.HP * 120 / 100; // TODO: tune this
|
||||
s.experience = 0;
|
||||
levelUp = true;
|
||||
emit LevelUp(_spaceshipId, s.level);
|
||||
} else {
|
||||
s.experience += _experience;
|
||||
}
|
||||
}
|
||||
|
||||
function getAttributes(uint _shipId) public view returns (
|
||||
uint HP,
|
||||
function getAttributes(uint _spaceshipId) public view returns (
|
||||
int HP,
|
||||
uint8 attack,
|
||||
uint8 defense,
|
||||
uint8 speed,
|
||||
uint experience,
|
||||
uint8 level,
|
||||
uint level,
|
||||
uint cooldown
|
||||
){
|
||||
Spaceship storage s = spaceships[_shipId];
|
||||
Spaceship storage s = spaceships[_spaceshipId];
|
||||
|
||||
HP = s.HP;
|
||||
attack = s.attack;
|
||||
|
|
Loading…
Reference in New Issue