Skip to content

Commit

Permalink
Update to component port
Browse files Browse the repository at this point in the history
  • Loading branch information
thgvr committed Oct 10, 2024
1 parent c786dd2 commit 7c604bd
Showing 1 changed file with 42 additions and 30 deletions.
72 changes: 42 additions & 30 deletions code/datums/components/cell_component.dm
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@ component_cell_out_of_charge/component_cell_removed proc using loc where necessa
var/obj/item/equipment
/// How much power do we use each process?
var/power_use_amount = POWER_CELL_USE_NORMAL
/// Are we using a robot's powersource?
var/inside_robot = FALSE
/// Callback interaction for when the cell is removed.
var/datum/callback/on_cell_removed = null
///Can this cell be removed from the parent?
var/cell_can_be_removed = TRUE
///Our reference to the cell overlay
var/mutable_appearance/cell_overlay = null
///Do we have cell overlays to be applied?
var/has_cell_overlays

/datum/component/cell/Initialize(cell_override, _on_cell_removed, _power_use_amount, start_with_cell = TRUE, _cell_can_be_removed, _has_cell_overlays = TRUE)
if(QDELETED(parent))
qdel(src)
return

/datum/component/cell/Initialize(cell_override, _on_cell_removed, _power_use_amount, start_with_cell = TRUE, _cell_can_be_removed)
if(!isitem(parent)) //Currently only compatable with items.
return COMPONENT_INCOMPATIBLE

Expand All @@ -43,6 +47,8 @@ component_cell_out_of_charge/component_cell_removed proc using loc where necessa
if(_on_cell_removed)
src.on_cell_removed = _on_cell_removed

has_cell_overlays = _has_cell_overlays

if(_power_use_amount)
power_use_amount = _power_use_amount
else
Expand All @@ -53,10 +59,10 @@ component_cell_out_of_charge/component_cell_removed proc using loc where necessa

if(start_with_cell)
var/obj/item/stock_parts/cell/new_cell
if(cell_override)
new_cell = new cell_override()
else
if(!cell_override)
new_cell = new /obj/item/stock_parts/cell/upgraded()
else
new_cell = new cell_override()
inserted_cell = new_cell
new_cell.forceMove(parent) //We use the parents location so things like EMP's can interact with the cell.
handle_cell_overlays()
Expand All @@ -75,17 +81,23 @@ component_cell_out_of_charge/component_cell_removed proc using loc where necessa
UnregisterSignal(parent, COMSIG_CLICK_CTRL_SHIFT)
UnregisterSignal(parent, COMSIG_PARENT_EXAMINE)

/datum/component/cell/Destroy(force, silent)
/datum/component/cell/Destroy(force)
if(on_cell_removed)
QDEL_NULL(on_cell_removed)
on_cell_removed = null
if(inserted_cell)
if(!inside_robot) //We really don't want to be deleting the robot's cell.
QDEL_NULL(inserted_cell)
QDEL_NULL(inserted_cell)
inserted_cell = null
return ..()

/// This proc is the basic way of processing the cell, with included feedback. It will return a bitflag if it failed to use the power, or COMPONENT_POWER_SUCCESS if it succeeds.
/// The user is sent the feedback, use_amount is an override, check_only will only return if it can use the cell and feedback relating to that.
/**
* The basic way of processing the cell, with included feedback.
*
* This proc is the basic way of processing the cell, with included feedback.
* It will return a bitflag if it failed to use the power, or COMPONENT_POWER_SUCCESS if it succeeds.
* Arguments:
* * use_amount - an override
* * check_only - will only return if it can use the cell and feedback relating to that including any relevant detail
*/
/datum/component/cell/proc/simple_power_use(datum/source, use_amount, mob/user, check_only)
SIGNAL_HANDLER

Expand All @@ -94,18 +106,18 @@ component_cell_out_of_charge/component_cell_removed proc using loc where necessa

if(!inserted_cell)
if(user)
to_chat(user, "<span class='danger'>There is no cell inside [equipment]</span>")
to_chat(user, span_danger("There is no cell inside [equipment]"))
return COMPONENT_NO_CELL

if(check_only && inserted_cell.charge < use_amount)
if(user)
to_chat(user, "<span class='danger'>The cell inside [equipment] does not have enough charge to perform this action!</span>")
to_chat(user, span_danger("The cell inside [equipment] does not have enough charge to perform this action!"))
return COMPONENT_NO_CHARGE

if(!inserted_cell.use(use_amount))
inserted_cell.update_appearance() //Updates the attached cell sprite - Why does this not happen in cell.use?
if(user)
to_chat(user, "<span class='danger'>The cell inside [equipment] does not have enough charge to perform this action!</span>")
to_chat(user, span_danger("The cell inside [equipment] does not have enough charge to perform this action!"))
return COMPONENT_NO_CHARGE

inserted_cell.update_appearance()
Expand All @@ -116,57 +128,57 @@ component_cell_out_of_charge/component_cell_removed proc using loc where necessa
SIGNAL_HANDLER

if(!inserted_cell)
examine_list += "<span class='danger'>It does not have a cell inserted!</span>"
else if(!inside_robot)
examine_list += "<span class='notice'>It has [inserted_cell] inserted. It has <b>[inserted_cell.percent()]%</b> charge left."
examine_list += span_danger("It does not have a cell inserted!")
else
examine_list += "<span class='notice'>It is drawing power from an external powersource, reading <b>[inserted_cell.percent()]%</b> charge.</span>"
examine_list += span_notice("It has [inserted_cell] inserted. It has <b>[inserted_cell.percent()]%</b> charge left. \
Ctrl+Shift+Click to remove the [inserted_cell].")

/// Handling of cell removal.
/datum/component/cell/proc/remove_cell(datum/source, mob/user)
SIGNAL_HANDLER
if(!equipment.can_interact(user))
return

if(inside_robot)
if(!cell_can_be_removed)
return

if(!cell_can_be_removed)
if(!isliving(user))
return

if(inserted_cell)
to_chat(user, "<span class='notice'>You remove [inserted_cell] from [equipment]!</span>")
to_chat(user, span_notice("You remove [inserted_cell] from [equipment]!"))
playsound(equipment, 'sound/weapons/magout.ogg', 40, TRUE)
inserted_cell.forceMove(get_turf(equipment))
INVOKE_ASYNC(user, /mob/living.proc/put_in_hands, inserted_cell)
INVOKE_ASYNC(user, TYPE_PROC_REF(/mob/living, put_in_hands), inserted_cell)
inserted_cell = null
if(on_cell_removed)
on_cell_removed.Invoke()
handle_cell_overlays(TRUE)
else
to_chat(user, "<span class='danger'>There is no cell inserted in [equipment]!</span>")
to_chat(user, span_danger("There is no cell inserted in [equipment]!"))

/// Handling of cell insertion.
/datum/component/cell/proc/insert_cell(datum/source, obj/item/inserting_item, mob/living/user, params)
SIGNAL_HANDLER
if(!equipment.can_interact(user))
return

if(inside_robot) //More robot shitcode, if we allowed them to remove the cell, it would cause the universe to implode.
return

if(!istype(inserting_item, /obj/item/stock_parts/cell))
return

if(inserted_cell) //No quickswap compatibility
to_chat(user, "<span class='danger'>There is already a cell inserted in [equipment]!</span>")
to_chat(user, span_danger("There is already a cell inserted in [equipment]!"))
return

to_chat(user, "<span class='notice'>You insert [inserting_item] into [equipment]!</span>")
playsound(equipment, 'sound/weapons/magin.ogg', 40, TRUE)
to_chat(user, span_notice("You insert [inserting_item] into [equipment]!"))
playsound(equipment, 'sound/weapons/magin.ogg', 40, TRUE)
inserted_cell = inserting_item
inserting_item.forceMove(parent)
handle_cell_overlays(FALSE)

/datum/component/cell/proc/handle_cell_overlays(update_overlays)
if(!has_cell_overlays)
return
if(inserted_cell)
cell_overlay = mutable_appearance(equipment.icon, "[initial(equipment.icon_state)]_cell")
equipment.add_overlay(cell_overlay)
Expand Down

0 comments on commit 7c604bd

Please sign in to comment.