CanadianMachines / MachineLib / Cylinder.cpp
Cylinder.cpp
Raw
/**
 * @file Cylinder.cpp
 * @author sriram
 */

#include "pch.h"
#include "Cylinder.h"
#include "StruckInstrument.h"

/// Cylinder base image
const std::wstring CylinderBaseImage = L"/images/cylinder-base.png";

/// Cylinder body image
const std::wstring CylinderBodyImage = L"/images/cylinder.png";

/// Cylinder ram image
const std::wstring CylinderRamImage = L"/images/cylinder-ram.png";

/// The maximum pixels to move the cylinder ram when enabled
const int CylinderMaxExtent = 44;

/// Angle from bottom center of the cylinder to the tubing connector
const double ConnectorAngle = -0.17;

/// Distance from bottom center of the cylinder to the tubing connector
const double ConnectorDistance = 24.5;


/**
 * constructor
 * @param resourcesDir
 */
Cylinder::Cylinder(std::wstring resourcesDir)
{
	mMount.SetImage(resourcesDir + CylinderBaseImage);
	mMount.Rectangle(-mMount.GetImageWidth() / 2, 0);

	mCylinder.SetImage(resourcesDir + CylinderBodyImage);
	mCylinder.Rectangle(-mCylinder.GetImageWidth() / 2, 0);

	mRam.SetImage(resourcesDir + CylinderRamImage);
	mRam.Rectangle(-mCylinder.GetImageWidth() / 2, 0);

	mSink.SetComponent(this);
}

/**
 * draws cylinder
 * @param graphics
 * @param x
 * @param y
 */
void Cylinder::Draw(std::shared_ptr<wxGraphicsContext> graphics, double x, double y)
{
	mMount.DrawPolygon(graphics,x+GetComponentLocation().x,y+GetComponentLocation().y);

	bool toExtend = mPressure>0;
	auto extendX = mMaxExtent*toExtend*(CylinderMaxExtent*sin(2*M_PI*mRam.GetRotation()));
	auto extendY = mMaxExtent*toExtend*abs(CylinderMaxExtent*cos(2*M_PI*mRam.GetRotation()));
	mRam.DrawPolygon(graphics, x + GetComponentLocation().x+extendX, y + GetComponentLocation().y+extendY);

	mCylinder.DrawPolygon(graphics,x+GetComponentLocation().x,y+GetComponentLocation().y);
}


void Cylinder::SetRotation(double rotation)
{
	mCylinder.SetRotation(rotation);
	mMount.SetRotation(rotation);
	mRam.SetRotation(rotation);
}

/**
 * send pressure across and play sound
 * @param pressure
 */
void Cylinder::SendPressure(double pressure)
{
	if(mPressure==0 && pressure>0)
	{
		mInstrument->Hit();
		//cylinder hits the instrument
		mInstrument->PlaySound();
	}
	mPressure=pressure;
}

/**
 * getter for position
 * @return
 */
wxPoint Cylinder::GetPosition()
{
	auto rotation  = GetRotation();
	double x = Component::GetPosition().x+ConnectorDistance*cos(2*M_PI*(rotation+ConnectorAngle));
	double y = Component::GetPosition().y+ConnectorDistance*sin(2*M_PI*(rotation+ConnectorAngle));
	return wxPoint(x,y);
}