implement max length for pipe and show it in inventory HUD

liquid_container
Maikel de Vries 2016-03-22 22:09:02 +01:00
parent dc1e828cfb
commit 7693276a48
2 changed files with 115 additions and 35 deletions

View File

@ -1,18 +1,8 @@
/*-- Pipe line
/**
Pipe line
Author: ST-DDT
--*/
local Name = "$Name$";
local ActMap = {
Connect = {
Prototype = Action,
Name = "Connect",
Procedure = DFA_CONNECT,
NextAction = "Connect"
}
};
@author ST-DDT
*/
private func Initialize()
{
@ -23,31 +13,31 @@ private func Initialize()
return;
}
// Reddish colour
// Reddish colour.
public func SetDrain()
{
SetProperty("LineColors", [RGB(110, 80, 80), RGB(110, 80, 80)]);
}
// Greenish colour
// Greenish colour.
public func SetSource()
{
SetProperty("LineColors", [RGB(80, 110, 80), RGB(80, 110, 80)]);
}
/** Returns true if this object is a functioning pipe. */
// Returns true if this object is a functioning pipe.
public func IsPipeLine()
{
return GetAction() == "Connect";
}
/** Returns whether this pipe is connected to an object. */
// Returns whether this pipe is connected to an object.
public func IsConnectedTo(object obj)
{
return GetActionTarget(0) == obj || GetActionTarget(1) == obj;
}
/** Returns the object which is connected to obj through this pipe. */
// Returns the object which is connected to obj through this pipe.
public func GetConnectedObject(object obj)
{
if (GetActionTarget(0) == obj)
@ -67,17 +57,43 @@ private func OnLineBreak(bool no_msg)
if (!line_end ||line_end->GetID() != Pipe)
line_end = GetActionTarget(1);
if (line_end)
line_end->~ResetPicture();
line_end->~OnPipeLineRemoval();
return;
}
private func OnLineChange()
{
// Notify action targets about line change.
var act1 = GetActionTarget(0);
var act2 = GetActionTarget(1);
if (act1) act1->~OnPipeLengthChange(this);
if (act2) act2->~OnPipeLengthChange(this);
// Break line if it is too long.
if (GetPipeLength() > this.PipeMaxLength)
{
OnLineBreak();
RemoveObject();
}
return;
}
// Returns the length between all the vertices.
public func GetPipeLength()
{
var current_length = 0;
for (var index = 0; index < GetVertexNum() - 1; index++)
current_length += Distance(GetVertex(index, VTX_X), GetVertex(index, VTX_Y), GetVertex(index + 1, VTX_X), GetVertex(index + 1, VTX_Y));
return current_length;
}
private func Destruction()
{
var line_end = GetActionTarget(0);
if (!line_end || line_end->GetID() != Pipe)
line_end = GetActionTarget(1);
if (line_end)
line_end->~ResetPicture();
line_end->~OnPipeLineRemoval();
return;
}
@ -96,4 +112,18 @@ public func SaveScenarioObject(props)
if (!inherited(props, ...)) return false;
SaveScenarioObjectAction(props);
return true;
}
}
/*-- Properties --*/
local Name = "$Name$";
local PipeMaxLength = 1200;
local ActMap = {
Connect = {
Prototype = Action,
Name = "Connect",
Procedure = DFA_CONNECT,
NextAction = "Connect"
}
};

View File

@ -1,12 +1,8 @@
/*-- Pipe
/**
Pipe
Author: ST-DDT
--*/
local Name = "$Name$";
local Description = "$Description$";
local Collectible = 1;
local PipeState = nil;
@author ST-DDT
*/
protected func Hit()
{
@ -17,7 +13,7 @@ public func IsToolProduct() { return true; }
/*-- Line connection --*/
/** Will connect power line to building at the clonk's position. */
// Will connect power line to building at the clonk's position.
protected func ControlUse(object clonk, int x, int y)
{
// Is this already connected to a liquid pump?
@ -34,7 +30,7 @@ protected func ControlUse(object clonk, int x, int y)
}
// already two pipes connected
if(liquid_pump->GetSource() && liquid_pump->GetDrain())
if (liquid_pump->GetSource() && liquid_pump->GetDrain())
{
clonk->Message("$MsgHasPipes$");
return true;
@ -80,6 +76,51 @@ public func ResetPicture()
return true;
}
public func OnPipeLineRemoval()
{
ResetPicture();
OnPipeLengthChange();
return;
}
public func OnPipeLengthChange()
{
// Update usage bar for a possible carrier (the clonk).
var carrier = Contained();
if (carrier)
carrier->~OnInventoryChange();
return;
}
// Display the line length bar over the pipe icon.
public func GetInventoryIconOverlay()
{
var pipe = FindObject(Find_ID(PipeLine), Find_Func("IsConnectedTo", this));
if (!pipe)
return;
var percentage = 100 * pipe->GetPipeLength() / pipe.PipeMaxLength;
var red = percentage * 255 / 100;
var green = 255 - red;
// Overlay a usage bar.
var overlay =
{
Bottom = "0.75em",
Margin = ["0.1em", "0.25em"],
BackgroundColor = RGB(0, 0, 0),
margin =
{
Margin = "0.05em",
bar =
{
BackgroundColor = RGB(red, green, 0),
Right = Format("%d%%", percentage),
}
}
};
return overlay;
}
public func CanBeStackedWith(object other)
{
// Do not stack source/drain/unused pipes
@ -94,10 +135,11 @@ local ApertureOffsetY = 3;
public func CycleApertureOffset()
{
// Cycle in three steps of three px each through X and Y
// covering a 3x3 grid on points -3,0,+3
// Cycle in three steps of three px each through X and Y.
// Covering a 3x3 grid on points -3, 0, +3.
ApertureOffsetX = (ApertureOffsetX + 6) % 9 - 3;
if (!ApertureOffsetX) ApertureOffsetY = (ApertureOffsetY + 6) % 9 - 3;
if (!ApertureOffsetX)
ApertureOffsetY = (ApertureOffsetY + 6) % 9 - 3;
return true;
}
@ -107,3 +149,11 @@ public func IsDroppedOnDeath(object clonk)
{
return !!FindObject(Find_Func("IsConnectedTo",this));
}
/*-- Properties --*/
local Name = "$Name$";
local Description = "$Description$";
local Collectible = 1;
local PipeState = nil;