// Obstacle.C -- written 16 Mar 1996 by Max Hailperin // // Implementation of all the various kinds of Obstacles in the Photon game. // // When an Obstacle of some kind is created, it is drawn, and the result // of the drawing operation gotten as an integer which is the id of the // so-called "canvas item" (i.e, the visible form of the obstacle). This // is saved so that it can be deleted in the Obstacle's destructor. (In // the case of targets, it can also be used to move the target to a new // location when it has been hit, in lieu of actually destroying the target // and creating a new one in the new location.) // // Work wasn't quite complete on this module when your predecessor at the // game company quit his job. (Even where "complete" is taken to mean even a // succesful clone of your competitor's game -- let alone the bigger, better // game you hope to turn it into.) #include "Obstacle.h" #include "Photon.h" #include "Ball.h" #include #include #include #include #include LinearObstacle::LinearObstacle(Photon *p, int x, int y, float x0, float y0, float x1, float y1) : Obstacle(p, x, y) { char s[128]; ostrstream strm(s, 128); strm << "create line " << p->mapX(x0) << ' ' << p->mapY(y0) << ' ' << p->mapX(x1) << ' ' << p->mapY(y1) << ends; p->draw(s); Tcl_GetInt(p->interpreter(), p->interpreter()->result, &id); } LinearObstacle::~LinearObstacle(){ game->deleteCanvasItem(id); } int Wall::beHitBy(Ball *b){ b->direction( - b->direction()); return 0; } Corner::Corner(Photon *p, int x, int y, float x0, float y0, float x1, float y1) : Obstacle(p, x, y) { char s[128]; ostrstream strm(s, 128); strm << "create line " << p->mapX(x0) << ' ' << p->mapY(y0) << ' ' << p->mapX(x0) << ' ' << p->mapY(y1) << ' ' << p->mapX(x1) << ' ' << p->mapY(y1) << ends; p->draw(s); Tcl_GetInt(p->interpreter(), p->interpreter()->result, &id); } Corner::~Corner(){ game->deleteCanvasItem(id); } int Corner::beHitBy(Ball *){ assert(0); // can't happen! return 0; } int Mirror::beHitBy(Ball *b){ b->vertical( ! b->vertical()); b->direction( mult * b->direction()); return 0; } Target::Target(Photon *p, int x, int y) : Obstacle(p, x, y){ char s[128]; ostrstream strm(s, 128); // Targets' diameters are 50% of the cell size, because of the 0.25s below strm << "create oval " << p->mapX(x-0.25f) << ' ' << p->mapY(y-0.25f) << ' ' << p->mapX(x+0.25f) << ' ' << p->mapY(y+0.25f) << " -fill red" << ends; p->draw(s); Tcl_GetInt(p->interpreter(), p->interpreter()->result, &id); } int Target::beHitBy(Ball *){ // Fill this in. } Target::~Target(){ game->deleteCanvasItem(id); }