forked from Mirrors/openclonk
clean up bullet trail and fix offset to bullet
parent
1fc7fea628
commit
095a589842
|
@ -79,7 +79,7 @@ public func DoExplode()
|
|||
shrapnel->SetVelocity(Random(359), RandomX(100, 140));
|
||||
shrapnel->SetRDir(-30 + Random(61));
|
||||
shrapnel->Launch(GetController());
|
||||
CreateObjectAbove(BulletTrail)->Set(2, 30, shrapnel);
|
||||
CreateObject(BulletTrail)->Set(shrapnel, 2, 30);
|
||||
}
|
||||
if (GBackLiquid())
|
||||
Sound("Fire::BlastLiquid2");
|
||||
|
|
|
@ -41,8 +41,8 @@ public func Launch(object shooter, int angle, int dist, int speed, int offset_x,
|
|||
SquishVertices(true);
|
||||
|
||||
// Neat trail.
|
||||
CreateObjectAbove(BulletTrail, 0, 0)->Set(2, 200, this);
|
||||
|
||||
CreateObject(BulletTrail)->Set(this, 2, 200);
|
||||
|
||||
// Sound.
|
||||
Sound("Objects::Weapons::Musket::BulletShot?");
|
||||
}
|
||||
|
|
|
@ -1,140 +1,156 @@
|
|||
/*
|
||||
/**
|
||||
Bullet trail
|
||||
Author: Newton
|
||||
|
||||
Draws a trail to any object. That object should fly straight or so fast
|
||||
that it looks like it because the trail itself is only drawn straight.
|
||||
Any projectile can use the trail by calling (after creating it):
|
||||
|
||||
Set(int width, int length, object projectile)
|
||||
Set(object bullet, int width, int length)
|
||||
|
||||
while width is the width of the trail, length the length of the trail and
|
||||
projectile itself. The trail will be removed when the projectile is gone.
|
||||
The color is always a light-grey. However each frame, ~TrailColor(int time)
|
||||
is called in the projectile which can return the color modulation.
|
||||
|
||||
@author Newton, Maikel
|
||||
*/
|
||||
|
||||
local fRemove, iSpeed, pShot, w, l, r, x, y;
|
||||
local for_bullet;
|
||||
local width, length;
|
||||
local speed, rotation;
|
||||
local orig_x, orig_y;
|
||||
|
||||
public func Set(int iWidth, int iLength, object pSht)
|
||||
public func Set(object bullet, int wdt, int lgt)
|
||||
{
|
||||
pShot = pSht;
|
||||
// Store the bullet for which this trail is made.
|
||||
for_bullet = bullet;
|
||||
|
||||
w = 1000*iWidth/ActMap.Travel.Wdt;
|
||||
l = 1000*iLength/ActMap.Travel.Hgt;
|
||||
// Change plane to be just behind bullet.
|
||||
this.Plane = for_bullet.Plane - 1;
|
||||
|
||||
var iXDir = pShot->GetXDir(1000);
|
||||
var iYDir = pShot->GetYDir(1000);
|
||||
|
||||
iSpeed = Sqrt(iXDir*iXDir/1000+iYDir*iYDir/1000);
|
||||
// Store trail properties.
|
||||
width = 1000 * wdt / ActMap.Travel.Wdt;
|
||||
length = 1000 * lgt / ActMap.Travel.Hgt;
|
||||
var xdir = for_bullet->GetXDir(1000);
|
||||
var ydir = for_bullet->GetYDir(1000);
|
||||
speed = Distance(0, 0, xdir, ydir) / Sqrt(1000);
|
||||
rotation = Angle(0, 0, xdir, ydir);
|
||||
orig_x = for_bullet->GetX();
|
||||
orig_y = for_bullet->GetY();
|
||||
|
||||
// Copy bullet's motion.
|
||||
SetAction("Travel");
|
||||
SetXDir(iXDir,1000);
|
||||
SetYDir(iYDir,1000);
|
||||
|
||||
r = Angle(0,0,iXDir,iYDir);
|
||||
x = GetX();
|
||||
y = GetY();
|
||||
|
||||
SetPosition(pShot->GetX(),pShot->GetY());
|
||||
Traveling();
|
||||
SetComDir(COMD_None);
|
||||
SetPosition(orig_x, orig_y);
|
||||
SetXDir(xdir, 1000);
|
||||
SetYDir(ydir, 1000);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Timer */
|
||||
|
||||
private func Traveling()
|
||||
private func UpdateTrail()
|
||||
{
|
||||
// The shot is gone -> remove
|
||||
if(!fRemove)
|
||||
if(!pShot)
|
||||
Remove();
|
||||
|
||||
// on the borders
|
||||
if(GetX() <= 0 && GetXDir() < 0
|
||||
|| GetX() >= LandscapeWidth() && GetXDir() > 0
|
||||
|| GetY() <= 0 && GetYDir() < 0
|
||||
|| GetY() >= LandscapeHeight() && GetYDir() > 0)
|
||||
Remove();
|
||||
// Remove trail if the bullet is gone or the trail is out of the landscape borders.
|
||||
if (!for_bullet)
|
||||
return Remove();
|
||||
if ((GetX() <= 0 && GetXDir() < 0) || (GetX() >= LandscapeWidth() && GetXDir() > 0) || (GetY() <= 0 && GetYDir() < 0) || (GetY() >= LandscapeHeight() && GetYDir() > 0))
|
||||
return Remove();
|
||||
|
||||
if(pShot)
|
||||
SetPosition(pShot->GetX(),pShot->GetY());
|
||||
|
||||
// Display
|
||||
// Display.
|
||||
DrawTransform();
|
||||
if(pShot)
|
||||
if(pShot->~TrailColor(GetActTime()) != nil)
|
||||
SetClrModulation(pShot->~TrailColor(GetActTime()));
|
||||
// Adjust trail color as specified in bullet.
|
||||
if (for_bullet)
|
||||
{
|
||||
var trail_color = for_bullet->~TrailColor(GetActTime());
|
||||
if (trail_color != nil)
|
||||
SetClrModulation(for_bullet->~TrailColor(GetActTime()));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if(l == 0) RemoveObject();
|
||||
public func RemoveTrail()
|
||||
{
|
||||
// Reduce trail length by its speed every frame.
|
||||
length = Max(0, length - speed);
|
||||
// Display.
|
||||
DrawTransform();
|
||||
// Remove trail if its length is zero.
|
||||
if (length <= 0)
|
||||
return RemoveObject();
|
||||
return;
|
||||
}
|
||||
|
||||
public func Hit()
|
||||
{
|
||||
Remove();
|
||||
// Remove trail if it hit something.
|
||||
return Remove();
|
||||
}
|
||||
|
||||
public func Remove() {
|
||||
SetXDir();
|
||||
SetYDir();
|
||||
l = Min(l,1000*Distance(x,y,GetX(),GetY())/ActMap.Travel.Hgt);
|
||||
fRemove = true;
|
||||
pShot = nil;
|
||||
private func Remove()
|
||||
{
|
||||
SetXDir(0);
|
||||
SetYDir(0);
|
||||
return SetAction("Remove");
|
||||
}
|
||||
|
||||
public func DrawTransform() {
|
||||
public func DrawTransform()
|
||||
{
|
||||
// Determine length, width and position of the trail.
|
||||
var distance = Distance(orig_x, orig_y, GetX(), GetY());
|
||||
var relative_length = 1000 * distance / ActMap.Travel.Hgt;
|
||||
|
||||
var distance = Distance(x,y,GetX(),GetY());
|
||||
var relative_length = 1000*distance/ActMap.Travel.Hgt;
|
||||
var h = Min(length, relative_length);
|
||||
|
||||
// skip because nothing has to be transformed
|
||||
if(!fRemove && l < relative_length) return;
|
||||
var fsin = -Sin(rotation, 1000);
|
||||
var fcos = Cos(rotation, 1000);
|
||||
|
||||
// stretch >-<
|
||||
if(fRemove) l = Max(0,l-iSpeed);
|
||||
|
||||
// stretch <->
|
||||
var h = Min(l,relative_length);
|
||||
|
||||
var fsin = -Sin(r, 1000), fcos = Cos(r, 1000);
|
||||
|
||||
var xoff = -(ActMap.Travel.Wdt*w/1000)/2;
|
||||
var xoff = -ActMap.Travel.Wdt * width / 2 + 750;
|
||||
var yoff = 0;
|
||||
|
||||
var width = +fcos*w/1000, height = +fcos*h/1000;
|
||||
var xskew = +fsin*h/1000, yskew = -fsin*w/1000;
|
||||
|
||||
var xadjust = +fcos*xoff + fsin*yoff;
|
||||
var yadjust = -fsin*xoff + fcos*yoff;
|
||||
|
||||
// set matrix values
|
||||
SetObjDrawTransform (
|
||||
width, xskew, xadjust,
|
||||
yskew, height, yadjust
|
||||
);
|
||||
// Set object draw transformation.
|
||||
var draw_width = +fcos * width / 1000;
|
||||
var draw_height = +fcos * h / 1000;
|
||||
var xskew = +fsin * h / 1000;
|
||||
var yskew = -fsin * width / 1000;
|
||||
var xadjust = (+fcos * xoff + fsin * yoff) / 1000;
|
||||
var yadjust = (-fsin * xoff + fcos * yoff) / 1000;
|
||||
return SetObjDrawTransform(draw_width, xskew, xadjust, yskew, draw_height, yadjust);
|
||||
}
|
||||
|
||||
public func SaveScenarioObject() { return false; }
|
||||
|
||||
|
||||
/*-- Properties --*/
|
||||
|
||||
local Plane = 300;
|
||||
|
||||
local ActMap = {
|
||||
|
||||
Travel = {
|
||||
Prototype = Action,
|
||||
Name = "Travel",
|
||||
Procedure = DFA_FLOAT,
|
||||
Speed = 100000,
|
||||
Accel = 16,
|
||||
Decel = 16,
|
||||
NextAction = "Travel",
|
||||
Length = 1,
|
||||
Delay = 1,
|
||||
X = 0,
|
||||
Y = 0,
|
||||
Wdt = 5,
|
||||
Hgt = 25,
|
||||
OffX = 0,
|
||||
OffY = 2,
|
||||
StartCall = "Traveling"
|
||||
},
|
||||
Travel = {
|
||||
Prototype = Action,
|
||||
Name = "Travel",
|
||||
Procedure = DFA_FLOAT,
|
||||
NextAction = "Travel",
|
||||
Length = 1,
|
||||
Delay = 1,
|
||||
X = 0,
|
||||
Y = 0,
|
||||
Wdt = 5,
|
||||
Hgt = 25,
|
||||
OffX = 0,
|
||||
OffY = 2,
|
||||
StartCall = "UpdateTrail"
|
||||
},
|
||||
Remove = {
|
||||
Prototype = Action,
|
||||
Name = "Remove",
|
||||
Procedure = DFA_FLOAT,
|
||||
NextAction = "Remove",
|
||||
Length = 1,
|
||||
Delay = 1,
|
||||
X = 0,
|
||||
Y = 0,
|
||||
Wdt = 5,
|
||||
Hgt = 25,
|
||||
OffX = 0,
|
||||
OffY = 2,
|
||||
StartCall = "RemoveTrail"
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue