movin and grovin
This commit is contained in:
parent
0b4a1cdb92
commit
a472506f56
@ -353,6 +353,7 @@ function line(x1, y1, x2, y2, color) {
|
||||
curCtx.style = color;
|
||||
curCtx.moveTo(x1 + camera.x + difx, y1 + camera.y + dify);
|
||||
curCtx.lineTo(x2 + camera.x + difx, y2 + camera.y + dify);
|
||||
curCtx.stroke();
|
||||
}
|
||||
|
||||
function shape(x,y,relitivePoints,color) {
|
||||
|
@ -33,6 +33,7 @@ function update() {
|
||||
// playing
|
||||
case globalStates.playing:
|
||||
handlePlaying();
|
||||
player.update();
|
||||
break;
|
||||
// paused
|
||||
case globalStates.paused:
|
||||
@ -91,6 +92,7 @@ function absoluteDraw() {
|
||||
// playing
|
||||
case globalStates.playing:
|
||||
drawPlayingUI();
|
||||
player.draw();
|
||||
break;
|
||||
// paused
|
||||
case globalStates.paused:
|
||||
|
@ -1,56 +1,25 @@
|
||||
|
||||
|
||||
// a Class for legs
|
||||
class Leg{
|
||||
|
||||
|
||||
Leg(thighX, thighY, kneeX, kneeY, footX, footY){
|
||||
constructor(x, y, len, angle) {
|
||||
|
||||
// Thigh X,Y
|
||||
this.thighX = thighX;
|
||||
this.thighY = thighY;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
|
||||
// Knee X,Y
|
||||
this.kneeX = kneeX;
|
||||
this.kneeY = kneeY;
|
||||
|
||||
// Foot X,Y
|
||||
this.footX = footX;
|
||||
this.footY = footY
|
||||
this.len = len;
|
||||
this.len2 = this.len * this.len;
|
||||
this.angle = angle;
|
||||
|
||||
// Calculates distances
|
||||
this.thighToKnee = abs(math.hypot(thighX - kneeX, thighY - kneeY));
|
||||
this.kneeToFoot = abs(math.hypot(kneeX - footX, kneeY - footX));
|
||||
|
||||
}
|
||||
|
||||
setThigh(newX, newY){
|
||||
this.thighX = newX;
|
||||
this.thighY = newY;
|
||||
|
||||
// Recalculates distances
|
||||
this.thighToKnee = abs(math.hypot(newX - this.kneeX, newY - this.kneeY));
|
||||
}
|
||||
|
||||
setKnee(newX, newY){
|
||||
this.kneeX = newX;
|
||||
this.kneeY = newY;
|
||||
|
||||
// Recalculates distances
|
||||
this.thighToKnee = abs(math.hypot(this.thighX - newX, this.thighY - newY));
|
||||
this.kneeToFoot = abs(math.hypot(newX - this.footX, newY - this.footY));
|
||||
}
|
||||
|
||||
setFoot(newX, newY){
|
||||
this.footX = newX;
|
||||
this.footY = newY;
|
||||
|
||||
// Recalculates distances
|
||||
this.kneeToFoot = abs(math.hypot(this.kneeX - newX, this.kneeY - newY));
|
||||
this.x2 = this.x + len * Math.cos(angle);
|
||||
this.y2 = this.y + len * Math.sin(angle);
|
||||
}
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
Leg.prototype.draw = function() {
|
||||
line(this.x,this.y,this.x2,this.y2,"green");
|
||||
};
|
@ -3,131 +3,122 @@ class Player {
|
||||
constructor(x, y){
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.hipL = {x:x-5,y:y};
|
||||
this.hipR = {x:x+5,y:y};
|
||||
this.footL = {x:x-5,y:y+10};
|
||||
this.footR = {x:x+5,y:y+10};
|
||||
this.kneeL= {x:x-5,y:y+5};
|
||||
this.kneeR = {x:x+5,y:y+5};
|
||||
this.legSelected = "l";
|
||||
this.shouldMoveLeg = false;
|
||||
this.w = 10;
|
||||
this.h = 20;
|
||||
this.hipLeft = {x:this.x-5,y:this.y+10};
|
||||
this.hipRight = {x:this.x+5,y:this.y+10};
|
||||
this.leftLeg = new Leg(this.hipLeft.x,this.hipLeft.y,50,-Math.PI/4);
|
||||
this.rightLeg = new Leg(this.hipRight.x, this.hipRight.y, 50, Math.PI/2);
|
||||
this.legSelected = "R";
|
||||
this.shouldMoveLeg = true;
|
||||
}
|
||||
|
||||
getLeg(){
|
||||
if(this.legSelected === "l"){
|
||||
return [this.footL, this.kneeL, this.thighL];
|
||||
}
|
||||
return [this.footR, this.kneeR, this.thighR];
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Player.prototype.getActiveLeg = function(){
|
||||
if(this.legSelected === "L"){
|
||||
return this.leftLeg;
|
||||
}
|
||||
return this.rightLeg;
|
||||
}
|
||||
|
||||
Player.prototype.getLockedLeg = function(){
|
||||
if(this.legSelected === "R"){
|
||||
return this.leftLeg;
|
||||
}
|
||||
return this.rightLeg;
|
||||
}
|
||||
|
||||
// leg has been selected, move leg towards mouse
|
||||
Player.prototype.moveLeg = function(){
|
||||
|
||||
|
||||
// Stops if we shouldn't move leg
|
||||
if(!this.shouldMoveLeg){
|
||||
return
|
||||
return 0;
|
||||
}
|
||||
|
||||
var target = mousePosition();
|
||||
// gets active leg & target
|
||||
var curLeg = this.getActiveLeg();
|
||||
var target = mousePos;
|
||||
|
||||
//TODO set a proper constant
|
||||
if(Math.hypot(this.x - target.x, this.y - target.y) < constants.legs.size.maximumMovement){
|
||||
// move selected leg towards mouse
|
||||
|
||||
// Points to move towards
|
||||
var ix = target.x;
|
||||
var iy = target.y;
|
||||
var leg = this.getLeg()
|
||||
// var angle = turn(pointTo(curLeg,{x:curLeg.x2,y:curLeg.y2}),pointTo(curLeg,target),0.1);
|
||||
var angle = pointTo(curLeg,target);
|
||||
curLeg.x2 = curLeg.x + curLeg.len * Math.cos(angle);
|
||||
curLeg.y2 = curLeg.y + curLeg.len * Math.sin(angle);
|
||||
|
||||
// Check collision psuedo code need to figure out actual collison
|
||||
if(ix.collide()){
|
||||
ix = leg[0].x;
|
||||
if(dist(curLeg,target) > curLeg.len) {
|
||||
// move towards mouse
|
||||
this.x += Math.cos(angle) * dist(curLeg,target)/50;
|
||||
|
||||
this.y += Math.sin(angle) * dist(curLeg,target)/50;
|
||||
this.updateHips();
|
||||
}
|
||||
// if leg is right update it accordingly
|
||||
if(this.legSelected === "R") {
|
||||
// set angle to the locked foot to the locked hip
|
||||
oppLeg = this.getLockedLeg();
|
||||
angle = pointTo({x:oppLeg.x2,y:oppLeg.y2},this.hipRight);
|
||||
|
||||
// Check collision psuedo code need to figure out actual collison
|
||||
if(iy.collide()){
|
||||
iy = leg[0].y;
|
||||
// snap body to a position where the hip is attached to the leg
|
||||
this.x = (oppLeg.x2 + oppLeg.len * Math.cos(angle)) - 5;
|
||||
this.y = (oppLeg.y2 + oppLeg.len * Math.sin(angle)) - 10;
|
||||
|
||||
this.updateHips();
|
||||
|
||||
// make sure each leg ends at the hips
|
||||
oppLeg.x = this.hipRight.x;
|
||||
oppLeg.y = this.hipRight.y;
|
||||
|
||||
curLeg.x = this.hipLeft.x;
|
||||
curLeg.y = this.hipLeft.y;
|
||||
} else {
|
||||
// set angle to the locked foot to the locked hip
|
||||
oppLeg = this.getLockedLeg();
|
||||
angle = pointTo({x:oppLeg.x2,y:oppLeg.y2},this.hipLeft);
|
||||
|
||||
// snap body to a position where the hip is attached to the leg
|
||||
this.x = (oppLeg.x2 + oppLeg.len * Math.cos(angle)) + 5;
|
||||
this.y = (oppLeg.y2 + oppLeg.len * Math.sin(angle)) - 10;
|
||||
|
||||
this.updateHips();
|
||||
|
||||
// make sure each leg ends at the hips
|
||||
oppLeg.x = this.hipLeft.x;
|
||||
oppLeg.y = this.hipLeft.y;
|
||||
|
||||
curLeg.x = this.hipRight.x;
|
||||
curLeg.y = this.hipRight.y;
|
||||
}
|
||||
|
||||
|
||||
// total distances as a square
|
||||
var targetSqrDistance = ix * ix + iy * iy;
|
||||
|
||||
// gets lengths may need to be tweaked
|
||||
var thighKneeLength = abs(Math.hypot(leg[2].x - leg[1].x, leg[2].y - leg[1].y) * 2)
|
||||
var kneeFootLength = abs(Math.hypot(leg[1].x - leg[0].x, leg[1].y - leg[0].y) * 2)
|
||||
|
||||
|
||||
var thighKneeAngle = Math.max(-1, Math.min(1, (targetSqrDistance + thighKneeLength - kneeFootLength)
|
||||
/ (2 * (thighKneeLength / 2) * Math.sqrt(targetSqrDistance)
|
||||
)));
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
|
||||
if target within range of leg
|
||||
ik towards target in x
|
||||
if colliding undo move
|
||||
ik towards target in y
|
||||
if colliding undo move
|
||||
if out of target
|
||||
ik towards target in x
|
||||
if colliding undo move
|
||||
ik towards target in y
|
||||
if colliding undo move
|
||||
slowly move torso towards mouse
|
||||
planted leg ik towards torso
|
||||
if torso outside the planted leg range
|
||||
move torso back
|
||||
*/
|
||||
|
||||
|
||||
// Finds the distance between the old hip position and the future one
|
||||
requiredLegDistance = Math.hypot(currentHip.x - futureFoot.x, currentHip.y - futureFoot.y);
|
||||
|
||||
newFootX = futureFoot.x;
|
||||
newFootY = futureFoot.y;
|
||||
|
||||
|
||||
|
||||
// TODO implement collision checking
|
||||
|
||||
|
||||
|
||||
newHipX = currentHip.x + futureFoot.x - currentFoot.x;
|
||||
newHipY = currentHip.y + futureFoot.y - currentFoot.y;
|
||||
|
||||
// newKneeX =
|
||||
|
||||
|
||||
|
||||
|
||||
// returns new leg positions
|
||||
return [{x:newHipX, y:newHipY}, {x:newKneeX, y:newKneeY}, {x:newFootX, y:newFootY}];
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
Player.prototype.updateHips = function() {
|
||||
this.hipLeft = {x:this.x-5,y:this.y+10};
|
||||
this.hipRight = {x:this.x+5,y:this.y+10};
|
||||
}
|
||||
|
||||
|
||||
Player.prototype.draw = function() {
|
||||
|
||||
rect(this.x, this.y, this.w, this.h,"green");
|
||||
this.leftLeg.draw();
|
||||
this.rightLeg.draw();
|
||||
}
|
||||
|
||||
Player.prototype.update = function() {
|
||||
|
||||
this.moveLeg();
|
||||
var curLeg = this.getActiveLeg();
|
||||
if(collidingWithWorld({x:curLeg.x2,y:curLeg.y2,w:4,h:4})||mousePress[0]){
|
||||
if(this.legSelected === "R"){
|
||||
this.legSelected = "L";
|
||||
} else {
|
||||
this.legSelected = "R";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
var player = new Player(300,200);
|
@ -32,14 +32,14 @@
|
||||
<!-- <script src="assets/js/world/build.js"></script> -->
|
||||
<script src="assets/js/world/level.js"></script>
|
||||
|
||||
<!-- <script src="assets/js/player/player.js"></script>
|
||||
<script src="assets/js/player/leg.js"></script>
|
||||
<script src="assets/js/player/lifeFunctions.js"></script> -->
|
||||
<script src="assets/js/player/player.js"></script>
|
||||
<!-- <script src="assets/js/player/lifeFunctions.js"></script> -->
|
||||
|
||||
<script src="assets/js/playing/playing.js"></script>
|
||||
|
||||
<!-- <script src="assets/js/titleScreen/titleScreen.js"></script>
|
||||
<script src="assets/js/UI/ui.js"></script> -->
|
||||
<script src="assets/js/titleScreen/titleScreen.js"></script>
|
||||
<script src="assets/js/UI/ui.js"></script>
|
||||
|
||||
<!-- Webpage -->
|
||||
<script src="assets/js/injection/cssinjector.js"></script>
|
||||
|
Reference in New Issue
Block a user