forked from Mirrors/openclonk
added System.ocg/EnergyBar.c which provides AddEnergyBar() to show an energy bar over any object, adjusted Tutorial04 to use that
parent
ddeb338dc9
commit
8970913fc7
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
adds an energy bar above the object.
|
||||
The energy bar uses either target.HitPoints & GetDamage() or target->GetMaxEnergy() & target->GetEnergy().
|
||||
*/
|
||||
global func AddEnergyBar()
|
||||
{
|
||||
var e = AddEffect("ShowEnergyBar", this, 1, 0, nil, nil);
|
||||
if (e)
|
||||
return e.bar;
|
||||
}
|
||||
|
||||
global func FxShowEnergyBarStart(target, effect, temp)
|
||||
{
|
||||
if (temp) return;
|
||||
var attachpoint = { x = 0, y = target->GetDefOffset(1) - 5};
|
||||
var current, max;
|
||||
if (target->GetCategory() & C4D_Living)
|
||||
{
|
||||
max = target->~GetMaxEnergy();
|
||||
current = target->GetEnergy();
|
||||
}
|
||||
else
|
||||
{
|
||||
max = target.HitPoints;
|
||||
current = max - target->GetDamage();
|
||||
}
|
||||
|
||||
if (current == nil || max == nil)
|
||||
return -1;
|
||||
|
||||
effect.bar = target->CreateProgressBar(GUI_ShadedSimpleProgressBar, max, current, 0, target->GetOwner(), attachpoint, nil, { width = 28, height = 6, color = RGB(200, 1, 1) });
|
||||
effect.bar->SetPlane(750);
|
||||
// update once
|
||||
effect.Interval = 1;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
global func FxShowEnergyBarTimer(target, effect, time)
|
||||
{
|
||||
var value;
|
||||
if (target->GetCategory() & C4D_Living) value = target->GetEnergy();
|
||||
else value = target.HitPoints - target->GetDamage();
|
||||
|
||||
effect.bar->SetValue(value);
|
||||
effect.Interval = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
global func FxShowEnergyBarDamage(target, effect, dmg, cause)
|
||||
{
|
||||
effect.Interval = 1;
|
||||
return dmg;
|
||||
}
|
||||
|
||||
global func FxShowEnergyBarStop(target, effect, reason, temp)
|
||||
{
|
||||
if (temp) return;
|
||||
if (effect.bar)
|
||||
effect.bar->Close();
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
[DefCore]
|
||||
id=EnergyBar
|
||||
Category=C4D_StaticBack
|
||||
Width=32
|
||||
Height=32
|
||||
Offset=-16,-16
|
||||
Vertices=1
|
||||
VertexX=0
|
||||
VertexY=8
|
Binary file not shown.
Before Width: | Height: | Size: 1.1 KiB |
|
@ -1,65 +0,0 @@
|
|||
/*--
|
||||
Energy bars
|
||||
Author: Maikel
|
||||
|
||||
Displays an energy bar above an object, uses Library_Bars.
|
||||
--*/
|
||||
|
||||
|
||||
#include Library_Bars
|
||||
|
||||
private func EnergyBarWidth() { return 24; }
|
||||
private func EnergyBarHeight() { return 6; }
|
||||
|
||||
protected func Initialize()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
public func SetTarget(object target)
|
||||
{
|
||||
// Attach energy bar to the object.
|
||||
SetAction("Attach", target);
|
||||
// Create energy bar.
|
||||
SetBarLayers(2, 0);
|
||||
SetBarOffset(0, - target->GetDefHeight() / 2, 0);
|
||||
SetBarDimensions(EnergyBarWidth(), EnergyBarHeight(), 0);
|
||||
SetClrModulation(RGB(200, 0, 0), 3);
|
||||
UpdateEnergy();
|
||||
return;
|
||||
}
|
||||
|
||||
protected func UpdateEnergy()
|
||||
{
|
||||
var target = GetActionTarget();
|
||||
var energy = target->GetEnergy();
|
||||
if (!energy)
|
||||
return RemoveObject();
|
||||
var phys = target->GetMaxEnergy();
|
||||
var promille;
|
||||
if (phys == 0)
|
||||
promille = 0;
|
||||
else
|
||||
promille = 1000 * energy / phys;
|
||||
SetBarProgress(promille, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
protected func AttachTargetLost() { RemoveObject(); }
|
||||
|
||||
local Name = "$Name$";
|
||||
local ActMap = {
|
||||
Attach = {
|
||||
Prototype = Action,
|
||||
Name = "Attach",
|
||||
Procedure = DFA_ATTACH,
|
||||
Length = 1,
|
||||
Delay = 1,
|
||||
X = 0,
|
||||
Y = 0,
|
||||
Wdt = 32,
|
||||
Hgt = 32,
|
||||
NextAction = "Attach",
|
||||
StartCall = "UpdateEnergy",
|
||||
},
|
||||
};
|
|
@ -1 +0,0 @@
|
|||
Name=Energy bar
|
|
@ -1 +0,0 @@
|
|||
Name=Energy bar
|
|
@ -111,7 +111,7 @@ private func InitializeScriptPlayer(int plr)
|
|||
spearman1->CreateContents(Javelin);
|
||||
spearman1->AI_GuardArea(800, 400, 400, 250);
|
||||
AddEffect("IntContentRemoval", spearman1, 100, 0);
|
||||
CreateObject(EnergyBar)->SetTarget(spearman1);
|
||||
spearman1->AddEnergyBar();
|
||||
|
||||
// Third section: Two opponents in a tower.
|
||||
// Lower part: a weak spearman.
|
||||
|
@ -121,7 +121,7 @@ private func InitializeScriptPlayer(int plr)
|
|||
spearman2->CreateContents(Javelin);
|
||||
spearman2->AI_GuardArea(1350, 200, 500, 400);
|
||||
AddEffect("IntContentRemoval", spearman2, 100, 0);
|
||||
CreateObject(EnergyBar)->SetTarget(spearman2);
|
||||
spearman2->AddEnergyBar();
|
||||
// Upper part: a normal bowman.
|
||||
var bowman = CreateObject(Clonk, 1732, 352, plr);
|
||||
bowman->MakeCrewMember(plr);
|
||||
|
@ -129,7 +129,7 @@ private func InitializeScriptPlayer(int plr)
|
|||
bowman->CreateContents(Bow)->CreateContents(Arrow);
|
||||
bowman->AI_GuardArea(1350, 200, 500, 400);
|
||||
AddEffect("IntContentRemoval", bowman, 100, 0);
|
||||
CreateObject(EnergyBar)->SetTarget(bowman);
|
||||
bowman->AddEnergyBar();
|
||||
|
||||
// Fourth section: Opponent with sword and shield.
|
||||
var swordman = CreateObject(Clonk, 2250, 360, plr);
|
||||
|
@ -139,7 +139,7 @@ private func InitializeScriptPlayer(int plr)
|
|||
swordman->CreateContents(Sword);
|
||||
swordman->AI_GuardArea(2050, 300, 300, 100);
|
||||
AddEffect("IntContentRemoval", swordman, 100, 0);
|
||||
CreateObject(EnergyBar)->SetTarget(swordman);
|
||||
swordman->AddEnergyBar();
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue