Create an algorithm that removes and adds an item

Asked

Viewed 53 times

-1

I’m working on a complex code, so I’m just gonna pass on the necessary.

Basically I’m trying to create an algorithm in php that simulates a video game inventory, where we can equip items in the character, and remove them later. to ensure that the items are equipped and unadjusted, I made this code:

if($equip == 0) {
    if($sword == 1) {
        $damage = $damage+3; 
        $weapon = 'Sword'; $sword=3;
    } elseif($sword == 2 && $setweapon == 1) {
        $damage = $damage-3;
        $sword = 3;
    } 

    if($plate == 1) {
        $defense = $defense+3; 
        $armor = "Chestplate"; 
        $plate = 3;
    } elseif($plate == 2) {
        $defense = $defense-3; 
        $plate = 3;
    }

    if($staff == 1) {
        $damage = $damage+1;
        $mana = $mana+15; 
        $weapon = 'Staff'; 
        $staff = 3;
    } elseif($sword == 2 && $setweapon == 0){
        $damage = $damage-1;
        $mana = $mana-15;
        $staff = 3;
    }

    if($cape == 1) {
        $defense = $defense+2;
        $mana = $mana+15;
        $armor = 'Cape';
        $cape = 3;
    } elseif($cape == 2) {
        $defense = $defense-2;
        $mana = $mana-15; 
        $cape = 3;
    }

    if($bow == 1) {
        $damage = $damage+2; 
        $distance = $distance+2; 
        $weapon = 'Bow'; 
        $bow = 3;
    } elseif($bow == 2 && $setweapon == 0) {
        $damage = $damage-2; 
        $distance = $distance-2; 
        $bow = 3;
    }

    if($vest == 1) {
        $dextery = $dextery+15;
        $defense = $defense+2; 
        $armor = 'Vest'; 
        $vest = 3;
    } elseif($vest == 2) {
        $dextery = $dextery-15;
        $defense = $defense-2; 
        $vest = 3;
    }

    $equip = 1;
}


All right, this works well for when the game starts and automatically equips it according to the character’s class. My intention is to try to make a small menu, where I choose an item that is with the number 3 (Items number 3 would appear in my inventory) and turn it to 1, equipping in my character.

But how will I identify this item that will be equipped in html?

  • Have you studied classes?

  • To be frank, I don’t know

  • I just saw what classes are. I tried to use classes in C# and it didn’t work (just like all my code), I won’t use classes in my code (too complicated for my level).

  • In my opinion, your question as a whole you will hardly get an adequate answer. If something brief is enough for you: create a link in HTML with a query string (index.php?item_id=1234) pointing out the ID of such an item. Then in PHP you get this id via GET: $_GET['item_id']. I believe that in addition will make your question outside the scope of the site.

  • and how I query?

1 answer

2


Since you don’t want to use classes, you can structure your objects with arrays.

Each player must have a name, basic attributes and an inventory. Items that are equipped are also in the player’s inventory, so to differentiate just have a flag in the item that defines whether or not it is equipped.

$woss = [
    'name' => 'Woss',
    'title' => 'Lord of the Universe',
    'stats' => [
        'life' => 100,
        'energy' => 100,
        'stamina' => 100,
        'strength' => 5,
        'defense' => 3
    ],
    'items' => []
];

In this case items will be a list of arrays that define items. An item must have a name and attributes. For example, a sword increases attack attributes; a part of the armor increases defense attributes.

$excaliburSword = [
    'name' => 'Excalibur Sword',
    'equipped' => false,
    'stats' => [
        'strength' => 4
    ]
];

$dragonPants = [
    'name' => 'Dragon Pants',
    'equipped' => false,
    'stats' => [
        'defense' => 7
    ]
];

To manipulate the data you create functions - many functions, use and abuse. For example, let’s create a function that adds an item to a player’s inventory:

function player_add_item(&$player, $item) {
    $player['items'][] = $item;
}

So we could add the sword to the player’s inventory:

print_r($woss);

player_add_item($woss, $excaliburSword);

The player would stay:

Array
(
    [name] => Woss
    [title] => Lord of the Universe
    [stats] => Array
        (
            [life] => 100
            [energy] => 100
            [stamina] => 100
            [strength] => 5
            [defense] => 3
        )

    [items] => Array
        (
            [0] => Array
                (
                    [name] => Excalibur Sword
                    [equipped] => false
                    [stats] => Array
                        (
                            [strength] => 4
                        )

                )

        )

)

To equip an item it would be enough to create a function that changes the value of equipped from item to true; to remove you could create a function that removes the item from the array; to calculate the attributes of the player considering the equipped items you could create a function that sums the equipped attributes to the player attributes; etc.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.