Nimbin[12]?SDL & Graphics / mapcreator / src/vegetables/vegetables.cpp

mapcreator git · master

SDL3 2.5D game and engine using assets, with map editor

sdl3 c++ game engine map-editor cmake · first commit 2025-10-31 · last commit 2026-09-07 (1 month ago) · synced 3 days ago · upstream: git.ide3.de/hsnr/sdl-spieleentwicklung/map_creator

C++ 99.3%
git clone https://git.christianimmanuel.de/sdl-graphics/mapcreator.gitwget https://git.christianimmanuel.de/sdl-graphics/mapcreator/archive/mapcreator.tar.gz
src/vegetables/vegetables.cpp 4.9 KB · 165 lines raw
#include "vegetables.hpp"

#include "inventory/inventory.hpp"


namespace VegetableData {
    std::vector<Vegetable*> all_vegetables;

    const CropDefinition crop_defs[(int)Type::COUNT] = {
        //        STAGE: 0    1    2    3    4
        { "",0,0,0,0,0,0,0,0,0,0,0,false , { 5.0f, 5.0f, 0.0f, 0.0f, 300.0f } },
        { "carrot",   2,1, 5,1, 6,1, 7,1, 23,0, (5-2+1)+1, false, { 0.0f, 10.0f, 0.0f, 0.0f, 300.0f } },
        { "raddish",  2,3, 5,3, 6,3, 7,3, 23,1, (5-2+1)+1, false, { 5.0f, 5.0f, 0.0f, 0.0f, 300.0f } },
        { "cabbage",  2,5, 5,5, 6,5, 7,5, 23,2, (5-2+1)+1, false, { 10.0f, 0.0f, 0.0f, 0.0f, 300.0f } },
        { "salad",    2,7, 5,7, 6,7, 7,7, 23,3, (5-2+1)+1, false, { 5.0f, 5.0f, 0.0f, 0.0f, 300.0f } },
        { "broccoli", 2,9, 5,9, 6,9, 7,9, 23,4, (5-2+1)+1, false, { 5.0f, 5.0f, 0.0f, 0.0f, 300.0f } },
        { "onion",    2,11,5,11,6,11,7,11,23,5, (5-2+1)+1, false, { 5.0f, 5.0f, 0.0f, 0.0f, 300.0f } }
    };

    const CropDefinition* get_DataByType(Type type) {
        return &crop_defs[static_cast<size_t>(type)];
    }

    const SDL_Point get_IconTilePosByType(Type type, bool is_seed) {
        CropDefinition data = crop_defs[(int)type];
        return {data.icon_tile_x - (!is_seed ? 0 : 1), data.icon_tile_y};
    }
}

Vegetable::Vegetable( VegetableData::Type t, SDL_Point world_pos, bool is_s)
    :
    Object(ObjectType::VEGETABLE,
           ObjectCategory::FARM_CROP,
           TileType::TILE_FARM,
           0,
           0,
           world_pos,
           {1,1},
           0),
    type(t), is_seed(is_s),
    growth_stage(0),
    next_update_sec(Default::get_TimeSinceStartInSec()),
    equipement_slot(EquipmentSlot::HAND_RIGHT)
{
    definition = &VegetableData::crop_defs[static_cast<size_t>(type)];
    tileset_object_x = definition->start_tile_x;
    tileset_object_y = definition->start_tile_y;
    VegetableData::all_vegetables.push_back(this);

    collecting_duration_max = 3.2;


    objects.push_back(this);
    update_TilesetId();
}


const VegetableData::CropDefinition&
get_DataByType(const VegetableData::Type& type) {
    return VegetableData::crop_defs[static_cast<size_t>(type)];
}



const VegetableData::CropDefinition&
Vegetable::get_DataByType() {
    return *definition;
}


SDL_Point
Vegetable::get_SetPosition() {
    return { tileset_object_x, tileset_object_y };
}


static constexpr double SECONDS_PER_STAGE = 5.0;

void Vegetable::Grow(double current_seconds) {
    if (current_seconds <= next_update_sec)
        return;

    double delta_sec = current_seconds - next_update_sec;

    size_t delta_stages = static_cast<size_t>(delta_sec / SECONDS_PER_STAGE);
    //std::cout << "GROW " << delta_stages << " " << delta_sec << " " << SECONDS_PER_STAGE << std::endl;

    if (delta_stages == 0)
        return;


    // MULTIPLY LAST STAGE
    int grow_multiplier = 3;
    if (growth_stage == (definition->total_stages - 3))
        next_update_sec += static_cast<double>(delta_stages) * SECONDS_PER_STAGE*grow_multiplier;
    else
        next_update_sec += static_cast<double>(delta_stages) * SECONDS_PER_STAGE;

    growth_stage += delta_stages;
    if (growth_stage >= definition->total_stages)
        growth_stage = definition->total_stages - 1;

    update_TilesetId();
}

bool Vegetable::is_Rotten() const {
    return growth_stage == definition->total_stages - 1;
}

bool Vegetable::is_FullyGrown() const {
    return growth_stage == (definition->total_stages - 2) || growth_stage == (definition->total_stages - 1);
}

void Vegetable::pick_Up() {
    // mark as removed → caller inserts into inventory
    for (auto it = VegetableData::all_vegetables.begin(); it != VegetableData::all_vegetables.end(); ++it) {
        if (*it == this) {
            VegetableData::all_vegetables.erase(it);
            break;
        }
    }

    std::cout << "REMOVE? " << id << std::endl;
    Object::Remove(Object::get_ObjectById(id));
}

bool Vegetable::is_Collectable() const {
    return is_FullyGrown();
}

void Vegetable::Collect(Inventory*, Messages*) {
    if (!is_Collectable()) return;

    // Convert to inventory item
    //Inventory::AddItem(definition->item_type, 1);

    std::cout << "I AM NOT GONNA GET CALLED ?! COLLLLEC? " << id << std::endl;
    // Remove from world
    delete this;
}




void Vegetable::update_TilesetId() {
    const auto& d = *definition;

    int grown_end = d.grown_tile_x - d.start_tile_x;

    if (growth_stage <= static_cast<size_t>(grown_end)) {
        // Regular growth stages, all on the start row
        tileset_object_x = static_cast<size_t>(d.start_tile_x) + growth_stage;
        tileset_object_y = d.start_tile_y;
    }
    else if (growth_stage == d.total_stages - 1) {
        // Rotten stage
        tileset_object_x = d.rotten_tile_x;
        tileset_object_y = d.rotten_tile_y;
    }
    else {
        // Fully grown but not rotten
        tileset_object_x = d.grown_tile_x;
        tileset_object_y = d.grown_tile_y;
    }
}