{"id":16253531,"url":"https://github.com/petarov/craftymouseface","last_synced_at":"2025-10-25T04:30:17.290Z","repository":{"id":5761863,"uuid":"6974859","full_name":"petarov/CraftyMouseFace","owner":"petarov","description":"Crafty component that helps adjusting sprite facing.","archived":true,"fork":false,"pushed_at":"2014-07-13T12:54:03.000Z","size":199,"stargazers_count":1,"open_issues_count":1,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-22T01:40:41.807Z","etag":null,"topics":["crafty-js","craftyjs","mouse-position"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/petarov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-12-03T01:22:10.000Z","updated_at":"2024-11-20T03:37:49.000Z","dependencies_parsed_at":"2022-09-21T04:51:20.949Z","dependency_job_id":null,"html_url":"https://github.com/petarov/CraftyMouseFace","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petarov%2FCraftyMouseFace","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petarov%2FCraftyMouseFace/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petarov%2FCraftyMouseFace/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petarov%2FCraftyMouseFace/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/petarov","download_url":"https://codeload.github.com/petarov/CraftyMouseFace/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238075362,"owners_count":19412317,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["crafty-js","craftyjs","mouse-position"],"created_at":"2024-10-10T15:17:43.736Z","updated_at":"2025-10-25T04:30:16.891Z","avatar_url":"https://github.com/petarov.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"CraftyMouseFace\n===============\n\n[Crafty](http://craftyjs.com/) component that monitors mouse movement and calculates angular position relative to entity position.\n\n# Description\n\nThis component does the following:\n\n  1. It finds the angle between given Sprite and mouse position and triggers a Crafty [event](http://craftyjs.com/api/Crafty-trigger.html) which holds information about current mouse position and calculated angle in radians and degrees.\n  2. Determines sprite facing direction.\n  3. Triggers Crafty [events](http://craftyjs.com/api/Crafty-trigger.html) when a mouse buttons is **pressed down** or **released up** anywhere on the game screen.\n\n![alt text](http://i28.tinypic.com/2llj7e8.png \"2D coordinates\")\n\n# How To Use\n\n## Demos\n\nDemos are located in the [demos](demos) folder. There are currently 2 demos:\n  * [Demo1](demos/demo1.js): Move sprite around the screen and shoot. Sprite faces mouse cursor position when moving.\n  * [Demo2](demos/demo2.js): Move sprite around the screen and shoot. Sprite rotates to face the mouse cursor.\n\nTo run the demos first install the required bower dependencies via:\n\n    bower install\n\nYou may use Python3 to start an http web server on http://localhost:8000 and test the demos:\n\n    python -m http.server\n    Serving HTTP on 0.0.0.0 port 8000 ...\n\nDemo2 is using a handy component called [CraftyEntityBoxOverlays](https://github.com/towbi/CraftyEntityBoxOverlays) to display entity collision and rotation boxes.\n\n## Examples\n\nCreate 2D Sprite entity with the *MouseFace* component enabled.\n\n```javascript\n    var entity = Crafty.e(\"2D, DOM, player, CharAnims, Multiway, MouseFace\")\n    .attr({\n        x: 400, y: 256, z: zbase + 1,\n        moving: false\n    })\n```\n\nSet a boolean flag when player is moving the sprite.\n\n```javascript\n    .CharAnims()\n    .bind(\"Moved\", function(from) {\n        this.moving = true;\n    })\n```\n\nNow, adjust the animation depending on the position of player's sprite relative to the mouse position.\n\n```javascript\n    .bind(\"EnterFrame\", function() {\n            // Display animation in the direction of moving\n        if (this.moving) {\n            var anim = null;\n            switch(this.getDirection()) {\n            case this._directions.left:\n                anim = 'walk_left';\n                break;\n            case this._directions.right:\n                anim = 'walk_right';\n                break;\n            case this._directions.up:\n                anim = 'walk_up';\n                break;\n            case this._directions.down:\n                anim = 'walk_down';\n                break;              \n            }\n                \n            if (anim) {\n                if (!this.isPlaying(anim))\n                this.animate(anim, -1); \n            }    \n            \n            this.moving = false;\n        } else {\n            this.pauseAnimation();\n        } \n    })\n      \n```\n\nSpawn a bullet when left mouse button is released. We're using the **getAngle()** call which will fetch the already calculated direction angle. We can then use the direction angle to adjust the vector of entity movement.\n\n```javascript\n    .bind(\"MouseUp\", function(data) {\n        if (data.mouseButton == Crafty.mouseButtons.LEFT) {\n            // shoot - create bullet\n            Crafty.e(\"2D, DOM, Color\")\n            .attr({\n                x: this.x + 16, y: this.y + 24, z: zbase + 1,\n                w: 3, h: 3,\n                speed: 5,\n                angle: this.getAngle()\n            })\n            .color(\"#FA5656\")\n            .bind(\"EnterFrame\", function(frame) {\n                this.x += Math.cos(this.angle) * this.speed;\n                this.y += Math.sin(this.angle) * this.speed;\n              // destroy ...\n            });\n        }\n    });\n```\n\nFor more examples please check the [demos](demos/) folder.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpetarov%2Fcraftymouseface","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpetarov%2Fcraftymouseface","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpetarov%2Fcraftymouseface/lists"}