Useful script snippets

Started by Wyz, Fri 13/08/2010 19:45:46

Previous topic - Next topic

Wyz

I don't know if there already exists a place to post useful pieces of script , if not then I'm creating it now. :)

A wrote a function that can turn any dynamic sprite into a graph:
Code: ags

void Graph(this DynamicSprite *, float value1, float value2, bool smooth)
{
  int y1 = (this.Height - 1) - FloatToInt(IntToFloat(this.Height - 1) * value1);
  int y2 = (this.Height - 1) - FloatToInt(IntToFloat(this.Height - 1) * value2);
  int y3 = ((y1 > y2) - (y1 < y2)) * smooth;
  
  this.Crop(1, 0, this.Width - 1, this.Height);
  this.ChangeCanvasSize(this.Width + 1, this.Height, 0, 0);
  
  DrawingSurface *surface = this.GetDrawingSurface();
  surface.DrawingColor = 15;
  surface.DrawLine(surface.Width - 1, y1, surface.Width - 1, y2 + y3);
  
  surface.Release();
}


It can be used like this:
(assuming that sprite is a dynamic sprite)
Code: ags

float last;

function room_RepExec()
{
  float current = IntToFloat(mouse.x) / IntToFloat(Room.Width);

  sprite.Graph(current, last, true);
  last = current;
}


You could also play with these:
Code: ags

  sprite.Graph(current, last, false);
  sprite.Graph(current, last, -1);

  sprite.Graph(current, 0.0, false);
  sprite.Graph(current, 1.0, false);

  sprite.Graph((current / 2.0) + 0.5, 0.5 - (last / 2.0), false);
Life is like an adventure without the pixel hunts.

Pumaman

Thanks, what people usually do is create a script module with handy bits of script in, which then makes it easy for other people to use.

Wyz

That's a good idea, I'll do so when I've collected a number of them so I can group them as something useful :D

In the meanwhile I'll post here so people can already use them.

Drawing extension: Brush/spray paint line

Code: ags

function BrushLine(this DrawingSurface *, int x1, int y1, int x2, int y2, int slot, float spread)
{
  x1 -= Game.SpriteWidth[slot] / 2;
  y1 -= Game.SpriteHeight[slot];
  x2 -= Game.SpriteWidth[slot] / 2;
  y2 -= Game.SpriteHeight[slot];
  
  float dx = IntToFloat(x2 - x1);
  float dy = IntToFloat(y2 - y1);
  
  float interval = Maths.Sqrt(dx*dx + dy*dy);
  interval = interval / (IntToFloat(Game.SpriteWidth[slot] + Game.SpriteHeight[slot]) / (spread * 2.0));
  
  if (interval < 1.0)
    interval = 1.0;
  
  float i = 0.0;
  float xoffset = dx / interval;
  float yoffset = dy / interval;
  float xx = IntToFloat(x1);
  float yy = IntToFloat(y1);
  
  while (i < interval)
  {
    this.DrawImage(FloatToInt(xx), FloatToInt(yy), slot, false);
    xx += xoffset;
    yy += yoffset;
    i++;
  }
}


Use like this:
Code: ags

DrawingSurface *surface; // assumed that it contains a valid surface

surface.BrushLine(10, 10, 100, 100, 33, 4.0);


The final parameter indicates the ratio of the sprite size and the core of the brush. 4.0 will usually do, but if your line has holes in them, increase this number.
Life is like an adventure without the pixel hunts.

monkey0506

Code: ags
bool IsModeEnabled(this Mouse*, CursorMode mode) {
  if (mode == this.Mode) return true;
  CursorMode prevMode = this.Mode;
  this.Mode = mode;
  if (this.Mode == prevMode) return false;
  this.Mode = prevMode;
  return true;
}

SMF spam blocked by CleanTalk