Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

RPG Abilities

rpg_abilities.livemd

RPG Abilities

Mix.install([
  {:kino, github: "livebook-dev/kino", override: true},
  {:kino_lab, "~> 0.1.0-dev", github: "jonatanklosko/kino_lab"},
  {:vega_lite, "~> 0.1.4"},
  {:kino_vega_lite, "~> 0.1.1"},
  {:benchee, "~> 0.1"},
  {:ecto, "~> 3.7"},
  {:math, "~> 0.7.0"},
  {:faker, "~> 0.17.0"},
  {:utils, path: "#{__DIR__}/../utils"},
  {:tested_cell, git: "https://github.com/BrooklinJazz/tested_cell"}
])

Navigation

Return Home Report An Issue

Overview

You’re creating an RPG game. In your game there are characters, Consumable items, and Wearable items.

classDiagram
    class Character {
        class: :rogue, :wizard, :warrior
        health: 100
        speed: 20
        clothing: []
    }

items will modify a character’s base stats.

Create A Character Struct

In the Elixir cell below, define a Character struct with :health, :speed, :class, and :items.

  • :health should be an integer defaulted to 100.
  • :speed should be an integer defaulted to 20.
  • :class should expect be an atom. (:wizard, :rogue, or :warrior).
  • :equipment should be a list that defaults to an empty list [].
%Character{health: 100, speed: 20, class: :wizard, equipment: []}
defmodule Character do
end

Utils.feedback(:rpg_character, Character)

Create Items

Create a HealingPotion struct. It should have a :level defaulted to 1.

defmodule HealingPotion do
end

Utils.feedback(:rpg_healing_potion, HealingPotion)

Create an Armor struct. It should have a :defense defaulted to 10.

defmodule Armor do
end

Utils.feedback(:rpg_armor, Armor)

Create A Consumable Protocol

Create a Consumable protocol and a HealingPotion implementation. The Consumable.consume/2 function should accept an item and a character.

A HealingPotion should increase a character’s health by ten times the potion’s level.

character = %Character{class: :wizard, health: 10},
item = %HealingPotion{level: 1}

Consumable.consume(item, character)
%Character{health: 20, speed: 20, class: :wizard, equipment: []}
defprotocol Consumable do
end

defimpl Consumable, for: HealingPotion do
end

Utils.feedback(:rpg_consumable, [Consumable, Character, HealingPotion])

Create A Wearable Protocol

Create a Wearable protocol and an Armor implementation. The Wearable.wear/2 function should accept a character and an item.

Armor should increase a character’s health by it’s :defense value. It should also reduce the character’s speed by 5.

While worn, the armor should be in the character’s :equipment list.

item = %Armor{defense: 10}
character = %Character{class: :warrior}

Wearable.wear(item, character)
%Character{health: 110, speed: 15, class: :warrior, equipment: [%Armor{defense: 10}]}

The Wearable.remove/2 function should undo the affects of some equipment, and remove the item from the :equipment list.

item = %Armor{defense: 10}
character = %Character{health: 110, speed: 15, class: :warrior, equipment: [%Armor{defense: 10}]}

Wearable.remove(item, character)
%Character{health: 100, speed: 20, class: :warrior, equipment: []}
defprotocol Wearable do
end

defimpl Wearable, for: Armor do
end

Utils.feedback(:rpg_wearable, [Wearable, Character, Armor])

Create A Custom Items (BONUS)

Consumable

Time to stretch your creativity! Create a custom Consumable item of your choice. For example, you might create a SpeedPotion that increases a character’s speed.

Wearable

Create a custom items that implement the Wearable Protocol.

  • wear/2 should expect a Character as a parameter and an item that implements the Wearable protocol
  • wear/2 should return a new Character with modified stats, and the item in their items list.
  • remove should return a new Character with the unmodified stats and the item removed.

Create A Custom Protocol (BONUS)

Take everything you’ve learned above and create a custom protocol with and item that implements it. You can add any feature that you want to the game.

For example, you might create a Castable protocol for spells the character can cast.

Commit Your Progress

Run the following in your command line from the project folder to track and save your progress in a Git commit.

$ git add .
$ git commit -m "finish rpg abilities exercise"