|
| 1 | +/obj/machinery/food_cart_TGUI |
| 2 | + name = "food cart" |
| 3 | + desc = "New generation hot dog stand." |
| 4 | + icon = 'icons/obj/kitchen.dmi' |
| 5 | + icon_state = "foodcart" |
| 6 | + density = TRUE |
| 7 | + anchored = FALSE |
| 8 | + use_power = NO_POWER_USE |
| 9 | + //Max amount of items that can be in cart's storage |
| 10 | + var/storage_capacity = 80 |
| 11 | + //Sound made when an item is dispensed |
| 12 | + var/dispense_sound = 'sound/machines/click.ogg' |
| 13 | + //List for items to be shown in UI |
| 14 | + var/list/ui_list = list() |
| 15 | + |
| 16 | +/obj/machinery/food_cart_TGUI/ui_interact(mob/user, datum/tgui/ui) |
| 17 | + ui = SStgui.try_update_ui(user, src, ui) |
| 18 | + if(!ui) |
| 19 | + ui = new(user, src, "FoodCart", name) |
| 20 | + ui.open() |
| 21 | + ui.set_autoupdate(TRUE) |
| 22 | + |
| 23 | +/obj/machinery/food_cart_TGUI/ui_data(mob/user) |
| 24 | + //Define variables from UI |
| 25 | + var/list/data = list() |
| 26 | + data["food"] = list() |
| 27 | + data["storage"] = list() |
| 28 | + |
| 29 | + //Loop through starting list for data to send to main tab |
| 30 | + for(var/item_detail in ui_list) |
| 31 | + |
| 32 | + //Create needed list and variable for geting data for UI |
| 33 | + var/list/details = list() |
| 34 | + var/obj/item/reagent_containers/food/item = new item_detail |
| 35 | + |
| 36 | + //Get information for UI |
| 37 | + details["item_name"] = item.name |
| 38 | + details["item_quantity"] = find_amount(item) |
| 39 | + details["item_type_path"] = item.type |
| 40 | + |
| 41 | + //Get an image for the UI |
| 42 | + var/icon/item_pic = getFlatIcon(item) |
| 43 | + var/md5 = md5(fcopy_rsc(item_pic)) |
| 44 | + if(!SSassets.cache["photo_[md5]_[item.name]_icon.png"]) |
| 45 | + SSassets.transport.register_asset("photo_[md5]_[item.name]_icon.png", item_pic) |
| 46 | + SSassets.transport.send_assets(user, list("photo_[md5]_[item.name]_icon.png" = item_pic)) |
| 47 | + details["item_image"] = SSassets.transport.get_asset_url("photo_[md5]_[item.name]_icon.png") |
| 48 | + |
| 49 | + //Add to food list |
| 50 | + data["food"] += list(details) |
| 51 | + |
| 52 | + //Get content and capacity data |
| 53 | + data["contents_length"] = contents.len |
| 54 | + data["storage_capacity"] = storage_capacity |
| 55 | + |
| 56 | + //Send stored information to UI |
| 57 | + return data |
| 58 | + |
| 59 | +/obj/machinery/food_cart_TGUI/ui_act(action, list/params) |
| 60 | + . = ..() |
| 61 | + if(.) |
| 62 | + |
| 63 | + return |
| 64 | + |
| 65 | + switch(action) |
| 66 | + if("dispense") |
| 67 | + var/itemPath = text2path(params["itemPath"]) |
| 68 | + dispense_item(itemPath) |
| 69 | + |
| 70 | +//For adding items to storage |
| 71 | +/obj/machinery/food_cart_TGUI/attackby(obj/item/A, mob/user, params) |
| 72 | + //Check to make sure it is a food item |
| 73 | + if(istype(A, /obj/item/reagent_containers/food)) |
| 74 | + storage_single(A) |
| 75 | + |
| 76 | +/obj/machinery/food_cart_TGUI/proc/dispense_item(received_item, mob/user = usr) |
| 77 | + |
| 78 | + //Make a variable for checking the type of the selected item |
| 79 | + var/obj/item/reagent_containers/food/ui_item = new received_item |
| 80 | + |
| 81 | + //If the vat has some of the desired item, dispense it |
| 82 | + if(find_amount(ui_item) > 0) |
| 83 | + //Select the last(most recent) of desired item |
| 84 | + var/obj/item/reagent_containers/food/snacks/dispensed_item = LAZYACCESS(contents, last_index(ui_item)) |
| 85 | + //Drop it on the floor and then move it into the user's hands |
| 86 | + dispensed_item.forceMove(loc) |
| 87 | + user.put_in_hands(dispensed_item) |
| 88 | + user.visible_message(span_notice("[user] dispenses [ui_item.name] from [src]."), span_notice("You dispense [ui_item.name] from [src].")) |
| 89 | + playsound(src, dispense_sound, 25, TRUE, extrarange = -3) |
| 90 | + //If the last one was dispenced, remove from UI |
| 91 | + if(find_amount(ui_item) == 0) |
| 92 | + LAZYREMOVE(ui_list, received_item) |
| 93 | + else |
| 94 | + //For Alt click and because UI buttons are slow to disable themselves |
| 95 | + user.balloon_alert(user, "All out!") |
| 96 | + |
| 97 | +/obj/machinery/food_cart_TGUI/proc/storage_single(obj/item/target_item, mob/user = usr) |
| 98 | + //Check if there is room |
| 99 | + if(contents.len < storage_capacity) |
| 100 | + //If item's typepath is not already in ui_list, add it |
| 101 | + if(!LAZYFIND(ui_list, target_item.type)) |
| 102 | + LAZYADD(ui_list, target_item.type) |
| 103 | + //Move item to content |
| 104 | + target_item.forceMove(src) |
| 105 | + user.visible_message(span_notice("[user] inserts [target_item] into [src]."), span_notice("You insert [target_item] into [src].")) |
| 106 | + playsound(src, 'sound/effects/rustle2.ogg', 50, TRUE, extrarange = -3) |
| 107 | + |
| 108 | + return |
| 109 | + else |
| 110 | + //Warn about full capacity |
| 111 | + user.balloon_alert(user, "No space!") |
| 112 | + |
| 113 | +/obj/machinery/food_cart_TGUI/proc/find_amount(obj/item/counting_item, target_name = null, list/target_list = null) |
| 114 | + var/amount = 0 |
| 115 | + |
| 116 | + //If target_list is null, search contents for type paths |
| 117 | + if(!target_list) |
| 118 | + //Loop through contents, counting every instance of the given target |
| 119 | + for(var/obj/item/list_item in contents) |
| 120 | + if(list_item.type == counting_item.type) |
| 121 | + amount += 1 |
| 122 | + //Else, search target_list |
| 123 | + else |
| 124 | + for(var/list_item in target_list) |
| 125 | + if(list_item == target_name) |
| 126 | + amount += 1 |
| 127 | + |
| 128 | + return amount |
| 129 | + |
| 130 | +/obj/machinery/food_cart_TGUI/proc/last_index(obj/item/search_item) |
| 131 | + |
| 132 | + var/obj/item/reagent_containers/food/snacks/item_index = null |
| 133 | + |
| 134 | + //Search for the same item path in storage |
| 135 | + for(var/i in 1 to LAZYLEN(contents)) |
| 136 | + //Loop through entire list to get last/most recent item |
| 137 | + if(contents[i].type == search_item.type) |
| 138 | + item_index = i |
| 139 | + |
| 140 | + return item_index |
0 commit comments