RPG 구축 기초 - Encode Club Sui 시리즈 #5

총 6개의 교육용 동영상 중 다섯 번째인 이 동영상에서는 Shayan이 Sui 에서 롤플레잉 게임 제작의 기본을 안내합니다.

RPG 구축 기초 - Encode Club Sui 시리즈 #5

Encode Club의 Sui 시리즈 다섯 번째 동영상에서는 롤플레잉 게임(RPG)의 캐릭터와 아이템을 만드는 방법과 상호작용을 만드는 방법을 보여드립니다.

Sui 재단은 Encode Club과 제휴하여 6개의 개발자 중심 동영상 시리즈를 제공합니다. 이 시리즈는 Sui 의 기본 사항부터 Sui 의 스마트 컨트랙트 구축 및 객체 작업에 대한 튜토리얼까지 다양합니다.

학습 하이라이트

Sui 의 객체 중심 프로그래밍 모델과 확장성 덕분에 Sui 은 진정한 웹2 경험을 웹3에 제공하는 최초의 블록체인이 될 것입니다. 이러한 경험의 최전선에는 게임이 포함됩니다. 게임은 본질적으로 프로그래밍이 복잡할 뿐만 아니라 플레이어에게 원활한 경험을 제공하기 위해 강력한 인프라가 필요합니다. 앞서 언급한 두 가지 요건 덕분에 Sui 는 그 도전을 감당할 수 있습니다.

Sui 에서 온체인 RPG의 코딩 예시를 살펴봅시다. 다음 예시는 샘 블랙셔의 hero.move 코드를 각색한 것입니다.

오브젝트

/// Our hero!
struct Hero has key, store {
	id: UID,
	/// Hit points. If they go to zero, the hero can't do anything
	hp: u64,
	/// Experience of the hero. Begins at zero
	experience: u64,
	/// The hero's minimal inventory
	sword: Option<Sword>,
	/// An ID of the game user is playing
	game_id: ID,
}

위의 코드는 플레이 가능한 캐릭터를 정의합니다. 필드에서 볼 수 있듯이 이 영웅은 RPG의 다른 캐릭터와 비슷합니다. 체력(HP), 경험치, 인벤토리가 있습니다.

/// The hero's trusty sword
struct Sword has key, store {
	id: UID,
	/// Constant set at creation. Acts as a multiplier on sword's strength.
	/// Swords with high magic are rarer (because they cost more).
	magic: u64,
	/// Sword grows in strength as we use it
	strength: u64,
	/// An ID of the game
	game_id: ID,
}

위의 코드는 영웅의 검을 보여줍니다. 이 검에는 키와 저장소 어빌리티가 있습니다. 이 시리즈의 이전 강의를 요약하면, 키는 소유 가능한 에셋이며 최상위 스토리지에 존재할 수 있음을 의미합니다. Move 이 카테고리의 오브젝트는 외부 API에서도 액세스할 수 있으므로 Sui여러 게임에서 아이템을 사용할 수 있는 고유한 가능성을 만들어냅니다. 그리고 store는 이 오브젝트를 자유롭게 래핑하고 전송할 수 있음을 의미합니다.

/// A creature that the hero can slay to level up
struct Boar has key {
	id: UID,
	/// Hit points before the boar is slain
	hp: u64,
	/// Strength of this particular boar
	strength: u64,
	/// An ID of the game
	game_id: ID,
}

위에서는 게임에서 멧돼지, 비플레이가능 캐릭터(NPC) 또는 적을 정의했습니다. 같은 장르의 다른 게임과 마찬가지로 영웅이 싸워서 경험치를 얻거나 아이템을 구매하고 퀘스트를 받을 수 있는 NPC를 만들 수 있습니다.

액션

/// Slay the `boar` with the `hero`'s sword, get experience.
/// Aborts if the hero has 0 HP or is not strong enough to slay the boar
public entry fun slay(
	game: &GameInfo, hero: &mut Hero, boar: Boar, ctx: &TxContext
) {
	check_id(game, hero.game_id);
	check_id(game, boar.game_id);
	let Boar { id: boar_id, strength: boar_strength, hp, game_id: _ } = boar;
	let hero_strength = hero_strength(hero);
	let boar_hp = hp;
	let hero_hp = hero.hp;
	// attack the boar with the sword until its HP goes to zero
	while (boar_hp > hero_strength) {
		// first, the hero attacks
		boar_hp = boar_hp - hero_strength;
		// then, the boar gets a turn to attack. if the boar would kill
		// the hero, abort--we can't let the boar win!
		assert!(hero_hp >= boar_strength , EBOAR_WON);
		hero_hp = hero_hp - boar_strength;
	};
	// hero takes their licks
	hero.hp = hero_hp;
	// hero gains experience proportional to the boar, sword grows in
	// strength by one (if hero is using a sword)
	hero.experience = hero.experience + hp;
	if (option::is_some(&hero.sword)) {
		level_up_sword(option::borrow_mut(&mut hero.sword), 1)
	};
	// let the world know about the hero's triumph by emitting an event!
	event::emit(BoarSlainEvent {
		slayer_address: tx_context::sender(ctx),
		hero: object::uid_to_inner(&hero.id),
		boar: object::uid_to_inner(&boar_id),
		game_id: id(game)
	});
	object::delete(boar_id);
}

위 코드에 표시된 액션은 처치 함수를 설명합니다. 이 함수를 자세히 살펴보면, 먼저 영웅과 멧돼지가 같은 게임 인스턴스에 속해 있는지 확인합니다. 그런 다음 영웅과 멧돼지 간의 결투가 진행되며, 영웅의 HP가 0에 도달하지 않는지 확인합니다. 결투가 완료되면 영웅은 멧돼지에 비례해 경험치를 획득하고 영웅의 검의 공격력이 1씩 증가합니다(영웅이 검을 휘두르는 경우). 마지막으로, 이 함수는 BoarSlayEvent라는 이벤트를 발생시킵니다. Move 의 이벤트를 통해 인덱서는 온체인 액션을 추적할 수 있으며, 이는 보편적으로 인식되는 오브젝트 상태를 달성하는 데 중요한 수단입니다.

위의 코드 예시는 Sam의 hero.move 코드를 간략하게 발췌한 것입니다. 이 코드는 Sui 에서 게임 빌더에게 유용한 예제로 제공되며, 오픈 소스이므로 자유롭게 리포지토리를 포크하여 자신만의 게임을 빌드할 수 있습니다!

전체 시리즈 보기

  1. Sui?
  2. 스마트 계약
  3. 오브젝트 및 NFT 생성
  4. 동적 필드 및 컬렉션
  5. RPG 빌드 기본 사항
  6. 블록체인에 게임 배포