diff --git a/WinterHoliday/SnowMan.pde b/WinterHoliday/SnowMan.pde index 38b4f5a..af3bf6e 100644 --- a/WinterHoliday/SnowMan.pde +++ b/WinterHoliday/SnowMan.pde @@ -12,6 +12,12 @@ public class SnowMan { int offsetMid = offsetBot - (radiusBot); int offsetTop = offsetMid - (2*radiusTop); + float snowballSize = 0; + float snowballX; + float snowballY; + float snowballThrowX; + float snowballThrowY; + public SnowMan() { this.scale = height / 800.0; // scale 1 for height = 800 this.x = width/2; // centered @@ -124,14 +130,77 @@ public class SnowMan { float armY = offsetMid - (0.3 * radiusMid); float leftArmX = x - (0.9 * radiusMid); float rightArmX = x + (0.9 * radiusMid); - float armLength = 1.0 * radiusMid; + float armLength = 1.2 * radiusMid; float armWidth = 0.1 * radiusMid; stroke(#4C322B); strokeWeight(armWidth); strokeCap(ROUND); - line(leftArmX, armY ,leftArmX - armLength, offsetMid - (radiusMid)); - line(rightArmX, armY ,rightArmX + armLength, offsetMid - (radiusMid)); + + if( abs(mouseX - leftArmX) == 0 || abs(mouseX - rightArmX) == 0 ){ + return; + } + + // Left arm angle and lengths + float thetaL = atan( abs(mouseY - armY) / abs(mouseX - leftArmX) ); + float leftHandX = armLength * cos(thetaL); + float leftHandY = armLength * sin(thetaL); + + // If mouseX is farther to the right, add to x position + // If mouseY is farther below, add to the y position + leftHandX = ( mouseX > leftArmX ) ? leftHandX : -leftHandX; + leftHandY = ( mouseY > armY ) ? leftHandY : -leftHandY; + + // Same for right arm + float thetaR = atan( abs(mouseY - armY) / abs(mouseX - rightArmX) ); + float rightHandX = armLength * cos(thetaR); + float rightHandY = armLength * sin(thetaR); + rightHandX = ( mouseX > rightArmX ) ? rightHandX : -rightHandX; + rightHandY = ( mouseY > armY ) ? rightHandY : -rightHandY; + + // Draw arms + line(leftArmX, armY , leftArmX + leftHandX, armY + leftHandY); + line(rightArmX, armY , rightArmX + rightHandX, armY + rightHandY); + + if( mousePressed ){ + // Build an increasingly large snowball when pressing the mouse + + snowballSize++; + + strokeWeight(3); + stroke(0); + snowballThrowX = mouseX; + snowballThrowY = mouseY; + snowballX = leftArmX + leftHandX; + snowballY = armY + leftHandY; + circle(snowballX, snowballY, snowballSize); + } else { + if( snowballSize > 0 ){ + // We have a snowball to throw! + + strokeWeight(3); + stroke(0); + circle( snowballX, snowballY, snowballSize); + + if( snowballX < 0 || snowballX > width || snowballY < 0 || snowballY > height ){ + // Snowball is off the screen, reset + + snowballSize = 0; + } else { + // Snowball on the screen so move it + + if( abs(snowballThrowX - leftArmX) == 0 ){ + return; + } + + float thetaS = atan( abs(snowballThrowY - armY) / abs(snowballThrowX - leftArmX) ); + float snowballIncX = ( snowballThrowX > leftArmX ) ? 10 * cos(thetaS) : -10 * cos(thetaS); + float snowballIncY = ( snowballThrowY > armY ) ? 10 * sin(thetaS) : -10 * sin(thetaS); + snowballX += snowballIncX; + snowballY += snowballIncY; + } + } + } pop(); }