Skip to content

Commit

Permalink
Update:add move system and battle system
Browse files Browse the repository at this point in the history
  • Loading branch information
LidamaoHub committed Oct 11, 2023
1 parent eb06cdd commit 99e64ba
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 116 deletions.
1 change: 1 addition & 0 deletions packages/contracts/mud.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default mudConfig({
PlayerState: ["Idle", "Preparing", "Exploring", "Attacking"],
ActionType: ["Attack", "Escape", "Props"],
Buff: ["None", "Fire", "Water", "Wind"],
RandomState:["Inited","Pending","Confirmed"]
},
tables: {
Season: {
Expand Down
30 changes: 14 additions & 16 deletions packages/contracts/src/systems/BattleSystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,21 @@ import { BattleUtils } from "./library/BattleUtils.sol";
import { GAME_CONFIG_KEY, BATTLE_CONFIG_KEY } from "../Constants.sol";

contract BattleSystem is System {
// modifier onlyBattlePlayer(uint256 _battleId, BattleState _battleState) {
// BattleListData memory battle = BattleList.get(_battleId);

// BattleState battleState = battle.attacker == _msgSender()
// ? battle.attackerState
// : battle.defenderState;

// require(
// battle.attacker == _msgSender() || battle.defender == _msgSender(),
// "You are not in this battle"
// );
// require(battleState == _battleState, "You are in the wrong state");

// require(!battle.isEnd, "Battle is end");

// _;
// }
function joinBattlefield(address _user) public {
// 加入战区,用户实际上是送到原点,状态改为探索中
// User storage player = Player[_user];
PlayerState playerState = Player.getState(_user);
require(
playerState == PlayerState.Preparing || playerState == PlayerState.Idle,
"You should in preparing state"
);
//实际上是送到原点//TODO通过常数设置原点参数
Player.setX(_user, GameConfig.getOriginX(GAME_CONFIG_KEY));
Player.setY(_user, GameConfig.getOriginY(GAME_CONFIG_KEY));
BattleConfig.pushBattlefieldPlayers(BATTLE_CONFIG_KEY, _user);
Player.setState(_user, PlayerState.Exploring);
}

function checkBattlePlayer(BattleListData memory battle, BattleState _battleState) internal view {
// BattleListData memory battle = BattleList.get(_battleId);
Expand Down
68 changes: 68 additions & 0 deletions packages/contracts/src/systems/BoxSystem.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;

import { System } from "@latticexyz/world/src/System.sol";
import { BattleState, Buff, PlayerState } from "../codegen/Types.sol";
import { GameConfig, BattleConfig, RandomList, RandomListData, BoxList, BoxListData, Player, PlayerData, PlayerLocationLock} from "../codegen/Tables.sol";
import { GAME_CONFIG_KEY, BATTLE_CONFIG_KEY } from "../Constants.sol";
import { CommonUtils } from "./library/CommonUtils.sol";


contract BoxSystem is System {


function creatBox(uint16 _x, uint16 _y) internal {
uint256 roomId = GameConfig.getRoomId(GAME_CONFIG_KEY);
uint256 boxId = GameConfig.getBoxId(GAME_CONFIG_KEY);
BoxList.setX(roomId, boxId, _x);
BoxList.setY(roomId, boxId, _y);

uint256 randomId = GameConfig.getRandomId(GAME_CONFIG_KEY);
BoxList.setRandomId(roomId, boxId, randomId);
GameConfig.setRandomId(GAME_CONFIG_KEY, randomId + 1);
GameConfig.setBoxId(GAME_CONFIG_KEY, boxId + 1);
}

function openBox(uint256 _boxId) external {
// 宝箱打开时init内容物,根据自带randomId来实现随机
uint256 roomId = GameConfig.getRoomId(GAME_CONFIG_KEY);
uint256 boxId = GameConfig.getBoxId(GAME_CONFIG_KEY);
require(BoxList.getDropTime(roomId, _boxId) != 0, "Invalid box");
BoxListData memory _box = BoxList.get(roomId, boxId);
PlayerData memory _user = Player.get(_box.owner);
require(
CommonUtils.isNear(_box.x, _user.x, _box.y, _user.y),
"You are not near the box"
);
require(_box.opened == false, "Box is opened");
uint8[] memory randomNumberList = getRandom(_box.randomId, 2);
uint8 oreBalance = CommonUtils.dice(randomNumberList[0],20,10,1);
uint8 treasureBalance = CommonUtils.dice(randomNumberList[1],80,10,1);
BoxList.setOreBalance(roomId, boxId, oreBalance);
BoxList.setTreasureBalance(roomId, boxId, treasureBalance);
BoxList.setOpened(roomId, boxId, true);
BoxList.setOpenTime(roomId, boxId, block.timestamp);
}

function getCollections(uint256 _boxId, uint16 _oreAmount,uint16 _treasureAmount) internal {
uint256 roomId = GameConfig.getRoomId(GAME_CONFIG_KEY);
uint256 boxId = GameConfig.getBoxId(GAME_CONFIG_KEY);
require(BoxList.getDropTime(roomId, _boxId) != 0, "Invalid box");
BoxListData memory _box = BoxList.get(roomId, boxId);
PlayerData memory _user = Player.get(_box.owner);
require(
CommonUtils.isNear(_box.x, _user.x, _box.y, _user.y),
"You are not near the box"
);
require(_box.opened == true, "Box is not opened");
if(block.timestamp<_box.openTime + BattleConfig.getMaxBoxBindTime(BATTLE_CONFIG_KEY)){
require(msg.sender==_box.owner,"The box is waiting for its opener, please wait");
}
require(_oreAmount<=_box.oreBalance&&_treasureAmount<=_box.treasureBalance,"Invalid amount");
BoxList.setOreBalance(roomId, boxId, _box.oreBalance - _oreAmount);
BoxList.setTreasureBalance(roomId, boxId, _box.treasureBalance - _treasureAmount);
Player.setOreBalance(_box.owner, _user.oreBalance + _oreAmount);
Player.setTreasureBalance(_box.owner, _user.treasureBalance + _treasureAmount);
}

}
11 changes: 1 addition & 10 deletions packages/contracts/src/systems/GameSystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,7 @@ contract GameSystem is System {
event MoveEvent(address indexed player, uint16 x, uint16 y);
event AttackStart(address player, address target);

// constructor(bytes32 root) {
// GameConfig.setMerkleRoot(GAME_CONFIG_KEY, root);
// GameConfig.setMaxAttackzDistance(GAME_CONFIG_KEY, 10);
// GameConfig.setMaxMoveDistance(GAME_CONFIG_KEY, 15);
// GameConfig.setMaxTimeLimit(GAME_CONFIG_KEY, 120);
// GameConfig.setMaxUserLocationLockTime(GAME_CONFIG_KEY, 120);
// GameConfig.setOriginX(GAME_CONFIG_KEY, 100);
// GameConfig.setOriginY(GAME_CONFIG_KEY, 100);
// GameConfig.setMaxBoxBindTime(GAME_CONFIG_KEY, 120);
// }


modifier CheckContinuity(Move[] memory moveList) {
// 验证行走轨迹合法且连续
Expand Down
32 changes: 32 additions & 0 deletions packages/contracts/src/systems/MoveSystem.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;

import { System } from "@latticexyz/world/src/System.sol";
import { BattleState, Buff, PlayerState } from "../codegen/Types.sol";
import { GameConfig, BattleConfig, RandomList, RandomListData, BoxList, BoxListData, Player, PlayerData, PlayerLocationLock} from "../codegen/Tables.sol";
import { GAME_CONFIG_KEY, BATTLE_CONFIG_KEY } from "../Constants.sol";
import { CommonUtils } from "./library/CommonUtils.sol";


contract MoveSystem is System {
function unlockUserLocation() external {
// 用户自行解锁
require(PlayerLocationLock.get(_msgSender()) != 0, "You are not locked");
require(
PlayerLocationLock.get(_msgSender()) + BattleConfig.getMaxUserLocationLockTime(BATTLE_CONFIG_KEY) <
block.timestamp,
"You are not locked"
);
PlayerLocationLock.set(_msgSender(), 0);
}


function transfer(uint16 x, uint16 y) external {
//传送门,将用户在战区和非战区移动
// 将用户坐标随机转移到指定位置
Player.setX(_msgSender(), x);
Player.setY(_msgSender(), y);
}


}
87 changes: 2 additions & 85 deletions packages/contracts/src/systems/RandomSystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { CommonUtils } from "./library/CommonUtils.sol";


contract RandomSystem is System {

event NewRandom(uint256 randomId, address author);

function raiseUserHP(
Expand All @@ -19,38 +20,7 @@ contract RandomSystem is System {
Player.setHP(_user, (_targetHP * _percent) / 100);
}

function unlockUserLocation() external {
// 用户自行解锁
require(PlayerLocationLock.get(_msgSender()) != 0, "You are not locked");
require(
PlayerLocationLock.get(_msgSender()) + BattleConfig.getMaxUserLocationLockTime(BATTLE_CONFIG_KEY) <
block.timestamp,
"You are not locked"
);
PlayerLocationLock.set(_msgSender(), 0);
}

function transfer(uint16 x, uint16 y) external {
//传送门,将用户在战区和非战区移动
// 将用户坐标随机转移到指定位置
Player.setX(_msgSender(), x);
Player.setY(_msgSender(), y);
}

function joinBattlefield(address _user) public {
// 加入战区,用户实际上是送到原点,状态改为探索中
// User storage player = Player[_user];
PlayerState playerState = Player.getState(_user);
require(
playerState == PlayerState.Preparing || playerState == PlayerState.Idle,
"You should in preparing state"
);
//实际上是送到原点//TODO通过常数设置原点参数
Player.setX(_user, GameConfig.getOriginX(GAME_CONFIG_KEY));
Player.setY(_user, GameConfig.getOriginY(GAME_CONFIG_KEY));
BattleConfig.pushBattlefieldPlayers(BATTLE_CONFIG_KEY, _user);
Player.setState(_user, PlayerState.Exploring);
}


function getRandom(
uint256 _randomId,
Expand Down Expand Up @@ -87,59 +57,6 @@ contract RandomSystem is System {
emit NewRandom(randomId, _msgSender());
}

function creatBox(uint16 _x, uint16 _y) internal {
uint256 roomId = GameConfig.getRoomId(GAME_CONFIG_KEY);
uint256 boxId = GameConfig.getBoxId(GAME_CONFIG_KEY);
BoxList.setX(roomId, boxId, _x);
BoxList.setY(roomId, boxId, _y);

uint256 randomId = GameConfig.getRandomId(GAME_CONFIG_KEY);
BoxList.setRandomId(roomId, boxId, randomId);
GameConfig.setRandomId(GAME_CONFIG_KEY, randomId + 1);
GameConfig.setBoxId(GAME_CONFIG_KEY, boxId + 1);
}

function openBox(uint256 _boxId) external {
// 宝箱打开时init内容物,根据自带randomId来实现随机
uint256 roomId = GameConfig.getRoomId(GAME_CONFIG_KEY);
uint256 boxId = GameConfig.getBoxId(GAME_CONFIG_KEY);
require(BoxList.getDropTime(roomId, _boxId) != 0, "Invalid box");
BoxListData memory _box = BoxList.get(roomId, boxId);
PlayerData memory _user = Player.get(_box.owner);
require(
CommonUtils.isNear(_box.x, _user.x, _box.y, _user.y),
"You are not near the box"
);
require(_box.opened == false, "Box is opened");
uint8[] memory randomNumberList = getRandom(_box.randomId, 2);
uint8 oreBalance = CommonUtils.dice(randomNumberList[0],20,10,1);
uint8 treasureBalance = CommonUtils.dice(randomNumberList[1],80,10,1);
BoxList.setOreBalance(roomId, boxId, oreBalance);
BoxList.setTreasureBalance(roomId, boxId, treasureBalance);
BoxList.setOpened(roomId, boxId, true);
BoxList.setOpenTime(roomId, boxId, block.timestamp);
}

function getCollections(uint256 _boxId, uint16 _oreAmount,uint16 _treasureAmount) internal {
uint256 roomId = GameConfig.getRoomId(GAME_CONFIG_KEY);
uint256 boxId = GameConfig.getBoxId(GAME_CONFIG_KEY);
require(BoxList.getDropTime(roomId, _boxId) != 0, "Invalid box");
BoxListData memory _box = BoxList.get(roomId, boxId);
PlayerData memory _user = Player.get(_box.owner);
require(
CommonUtils.isNear(_box.x, _user.x, _box.y, _user.y),
"You are not near the box"
);
require(_box.opened == true, "Box is not opened");
if(block.timestamp<_box.openTime + BattleConfig.getMaxBoxBindTime(BATTLE_CONFIG_KEY)){
require(msg.sender==_box.owner,"The box is waiting for its opener, please wait");
}
require(_oreAmount<=_box.oreBalance&&_treasureAmount<=_box.treasureBalance,"Invalid amount");
BoxList.setOreBalance(roomId, boxId, _box.oreBalance - _oreAmount);
BoxList.setTreasureBalance(roomId, boxId, _box.treasureBalance - _treasureAmount);
Player.setOreBalance(_box.owner, _user.oreBalance + _oreAmount);
Player.setTreasureBalance(_box.owner, _user.treasureBalance + _treasureAmount);
}


}
6 changes: 1 addition & 5 deletions packages/contracts/src/systems/library/MRandom.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
pragma solidity >=0.8.0;

contract MRandom {
enum RandomState {
Inited,
Pending,
Confirmed
}

struct Random {
uint256 blockNumber;
address author;
Expand Down

0 comments on commit 99e64ba

Please sign in to comment.