Contributed by Scott Anderson
LPC is an object-oriented C-like language that shares elements of LISP and Smalltalk. LPC is a special-purpose language typically used to create on-line communities like MUDs or chat rooms. This code was written for the MudOS LP driver.
// x and y origin points
int originX = 0, originY = 0;
// create routine for a shape
varargs void create(int x, int y)
{
originX = x; originY = y;
}
// move the shape to a new origin
void moveTo(int newX, int newY)
{
originX = newX;
originY = newY;
}
// move the shape relative to the current origin
void moveRelative(int deltaX, int deltaY)
{
originX += deltaX;
originY += deltaY;
}
|
// Inherit the shape object.
// Note that the MudOS driver needs an absolute path;
// typically the directory would be aliased with a
// #define statement in a .h file.
inherit "/wiz/malraux/shapes/shape";
// width and height variables.
int width = 0, height = 0;
// create the rectangle
varargs void create(int x, int y, int w, int h)
{
// Call the inherited create method.
// For multiple inheritance, the specific superclass
// would be specified, like "shape::create()"
::create(x, y);
width = w;
height = h;
}
// set the width of the rectangle
void setWidth(int newW)
{
width = newW;
}
// set the height of the rectangle
void setHeight(int newH)
{
height = newH;
}
// draw the rectangle
void draw()
{
printf("Drawing rectangle at: (%d, %d), width: %d, height: %d\n", originX, originY, width, height);
}
|
// Inherit the shape object.
// Note that the MudOS driver needs an absolute path;
// typically the directory would be aliased with a
// #define statement in a .h file.
inherit "/wiz/malraux/shapes/shape";
// radius variable
int radius = 0;
// create the circle
varargs void create(int x, int y, int r)
{
// Call the inherited create method.
// For multiple inheritance, the specific superclass
// would be specified, like "shape::create()"
::create(x, y);
radius = r;
}
// set the radius of the circle
void setRadius(int newR)
{
radius = newR;
}
// draw the circle
void draw()
{
printf("Drawing circle at: (%d, %d), radius: %d\n", originX, originY, radius);
}
|
// Run the test code when this object is loaded
void create()
{
// define an array of objects
object *aShapes;
object r;
int i;
// fill the array with some shapes
aShapes = ({ clone_object("/wiz/malraux/shapes/rectangle", 10, 20, 5, 6),
clone_object("/wiz/malraux/shapes/circle", 15, 25, 8) });
// display the polymorphism of the shapes
for (i = 0; i < sizeof(aShapes); i++)
{
aShapes[i]->draw();
aShapes[i]->moveRelative(100, 100);
aShapes[i]->draw();
}
// call a rectangle-specific function
r = clone_object("/wiz/malraux/shapes/rectangle", 0, 0, 15, 15);
r->setWidth(30);
r->draw();
}
|