CSC3221_Programming_For_Games_Shapes / Project2 / Square.cpp
Square.cpp
Raw
#include "Square.h"
#include "Collision.h"
#include <iostream>

/*Default constructor*/
Square::Square(void) : Shape() {
	this->length = 0;
}

/*Set constructor with values*/
Square::Square(const float x, const float y, const float length) : Shape(x, y, length, length) {
	this->length = length;
}

/*Copy constructor*/
Square::Square(const Square& shape) {
	position.shapeLeft = shape.position.shapeLeft;
	position.shapeTop = shape.position.shapeTop;
	position.shapeRight = shape.position.shapeRight;
	position.shapeBottom = shape.position.shapeBottom;
	length = shape.length;
}

/*Destructor*/
Square::~Square(void) {
}

/*Collision detection method in the Collision class*/
bool Square::isDetectCollision(const Shape &shape) const {
	try {
		if (typeid(shape) == typeid(Square)) {
			return Collision::isDetectCollision(dynamic_cast<const Square&>(shape), *this);
		}
		else if (typeid(shape) == typeid(Circle)) {
			return Collision::isDetectCollision(dynamic_cast<const Circle&>(shape), *this);
		}
		else {
			throw "Error: the shape is not defined";
		}
	}
	catch (char const* &exception) {
		std::cout << exception;
	}
	return false;
}

/*Overload to print out the output of the squares*/
std::ostream& Square::result(std::ostream& output) const {
	output << *this;
	return output;
}

/*Overloads assignment operator*/
Square& Square::operator=(const Square &square) {
	if (this != &square) {
		position.shapeLeft = square.position.shapeLeft;
		position.shapeTop = square.position.shapeTop;
		position.shapeRight = square.position.shapeRight;
		position.shapeBottom = square.position.shapeBottom;
		length = square.length;
	}
	return *this;
}

/*Overloads ostream operator*/
std::ostream& operator<<(std::ostream& output, const Square& square) {
	output << "SQUARE" << std::endl <<
		"Position[Top: " << square.position.shapeTop << " , Right: " << square.position.shapeRight << " , Bottom: " << square.position.shapeBottom << " , Left: " << square.position.shapeLeft << "]" << std::endl <<
		"Length[" << square.length << "]" << std::endl <<
		"Shape Origin[X: " << square.getOriginPositionX() << " , Y: " << square.getOriginPositionY() << "]" << std::endl;
	return output;
}