{"id":771,"date":"2012-10-24T15:19:18","date_gmt":"2012-10-24T14:19:18","guid":{"rendered":"http:\/\/www.aymericlamboley.fr\/blog\/?p=771"},"modified":"2014-11-01T14:49:36","modified_gmt":"2014-11-01T13:49:36","slug":"citrus-engines-structure-updated-camera-movement-example","status":"publish","type":"post","link":"http:\/\/www.aymericlamboley.fr\/blog\/citrus-engines-structure-updated-camera-movement-example\/","title":{"rendered":"Citrus Engine&#8217;s structure updated, camera movement example"},"content":{"rendered":"<p>This is a quick blog post to share a small tutorial on how you can handle a camera in the <a href=\"http:\/\/citrusengine.com\/\" target=\"_blank\">Citrus Engine<\/a> and having feedbacks for the new CE&#8217;s structure.<\/p>\n<p><!--more--><\/p>\n<p><strong>Project dependencies<\/strong><br \/>\nLet&#8217;s start for a very interessant &#038; complex subject : refactor &#038; separate an engine which has many libraries dependencies. Now that the Citrus Engine handles 3D and 3D physics thanks to Away3D &#038; AwayPhysics it becomes too heavy. There is no problem with performances but a simple hello world is far too heavy. At the moment, any project needs to have Starling, Box2D, Nape, Away3D &#038; AwayPhysics included. Even if you just need, for example, Starling &#038; Box2D. We have to find a solution. We can&#8217;t just remove libraries that we don&#8217;t need manually and follow compiler instructions to remove them from the engine too. We have to find the best solution for final users &#038; contributors.<br \/>\nIt would be great to have swc as well for each dependency, e.g. ce.swc, ce_starling.swc &#038; ce_starling_box2d.swc or ce.swc, cd_away3d.swc &#038; ce_away3d_nape.swc. It means lots of swcs but it would be really easy to pick up those needed.<\/p>\n<p>That was the point. And I&#8217;m glad to say that we find the best solution for everyone! And it&#8217;s up on <a href=\"https:\/\/github.com\/alamboley\/Citrus-Engine\" target=\"_blank\">GitHub<\/a>! Also I would like to thanks Johann Barbie who helped me a lot on this. Now everything in the engine is separated, it means simply that in the view package we never care about which physics engine we use. Concerning the physics debug view, we have a class for each physics engine and then we use some wrapper to handle them in Starling or Away3D.<\/p>\n<p><strong>What changed?<\/strong><br \/>\nI try to do my best to not break backward compatibility. If you use a modern IDE (if not, you should!!), just update the package import. It should works fine.<br \/>\n<em>If you use Starling, you Main class must extends StarlingCitrusEngine<\/em>. And that&#8217;s it!<br \/>\nAll examples have been updated, and the final SWF size is really smaller!<\/p>\n<p><strong>Ant<\/strong><br \/>\nNow I&#8217;m working with Ant to provide a swc for each build which only contain CE&#8217;s code. My main problem is I don&#8217;t success to remove external libraries on the final build. I would like to include them to compile my swc, but not include them in the final swc. Any advice would be very much appreciated!<\/p>\n<p><strong>Box2D AS3<\/strong><br \/>\nSome people request me to move on Box2D AS3 version instead of the Alchemy version. It is a good idea if you don&#8217;t want to depend of the new Adobe tax. However this AS3 version is harder to handle since there isn&#8217;t a simple event system to manage collision like the Alchemy one. If you are familiar with the AS3 version and would like to provide your help, please contact me.<\/p>\n<p><strong>Camera demo<\/strong><br \/>\n<a href=\"http:\/\/www.aymericlamboley.fr\/blog\/wp-content\/uploads\/2012\/10\/index.html\" target=\"_blank\">The demo<\/a>.<br \/>\nUse the mouse wheel to zoom\/dezoom. Press R to rotate the content.<\/p>\n<p>The source code is very simple :<\/p>\n<pre lang=\"actionscript3\" line=\"1\">package cameramovement {\r\n\r\n\timport com.citrusengine.core.CitrusEngine;\r\n\timport com.citrusengine.core.State;\r\n\timport com.citrusengine.math.MathUtils;\r\n\timport com.citrusengine.math.MathVector;\r\n\timport com.citrusengine.objects.platformer.box2d.Hero;\r\n\timport com.citrusengine.objects.platformer.box2d.Platform;\r\n\timport com.citrusengine.physics.box2d.Box2D;\r\n\r\n\timport flash.events.MouseEvent;\r\n\timport flash.geom.Point;\r\n\timport flash.geom.Rectangle;\r\n\timport flash.ui.Keyboard;\r\n\r\n\t\/**\r\n\t * @author Aymeric\r\n\t *\/\r\n\tpublic class CameraMovement extends State {\r\n\r\n\t\tpublic function CameraMovement() {\r\n\t\t\tsuper();\r\n\t\t}\r\n\r\n\t\toverride public function initialize():void {\r\n\t\t\t\r\n\t\t\tsuper.initialize();\r\n\r\n\t\t\tvar box2d:Box2D = new Box2D(\"box2D\", {visible:true});\r\n\t\t\tbox2d.group = 0; \/\/ box2d debug view will be behind graphics, default is 1 : can't work on Starling, since the debug view is on the display list\r\n\t\t\tadd(box2d);\r\n\r\n\t\t\tvar hero:Hero = new Hero(\"hero\", {x:100, view:\"PatchSpriteArt.swf\", width:60, height:120, offsetY:15});\r\n\t\t\tadd(hero);\r\n\r\n\t\t\tadd(new Platform(\"platBot\", {x:stage.stageWidth \/ 2, y:stage.stageHeight, width:3000}));\r\n\t\t\tadd(new Platform(\"cloud\", {x:450, y:250, width:200, oneWay:true}));\r\n\r\n\t\t\tview.setupCamera(hero, new MathVector(stage.stageWidth \/ 2, stage.stageHeight \/ 2), new Rectangle(0, 0, 1550, 450), new MathVector(.25, .05));\r\n\r\n\t\t\tstage.addEventListener(MouseEvent.MOUSE_WHEEL, _mouseWheel);\r\n\t\t}\r\n\r\n\t\toverride public function destroy():void {\r\n\r\n\t\t\tstage.removeEventListener(MouseEvent.MOUSE_DOWN, _mouseWheel);\r\n\r\n\t\t\tsuper.destroy();\r\n\t\t}\r\n\r\n\t\toverride public function update(timeDelta:Number):void {\r\n\r\n\t\t\tsuper.update(timeDelta);\r\n\t\t\t\r\n\t\t\tif (CitrusEngine.getInstance().input.isDown(Keyboard.R)) {\r\n\t\t\t\t\r\n\t\t\t\t\/\/ if you use Starling, just move the pivot point!\r\n\t\t\t\t\r\n\t\t\t\tMathUtils.RotateAroundInternalPoint(this, new Point(stage.stageWidth \/ 2, stage.stageHeight \/ 2), 1);\r\n\t\t\t\t\r\n\t\t\t\tview.cameraOffset = new MathVector(stage.stageWidth \/ 2, stage.stageHeight \/ 2);\r\n\t\t\t\tview.cameraBounds = new Rectangle(0, 0, 1550, 450);\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t\tprivate function _mouseWheel(mEvt:MouseEvent):void {\r\n\t\t\t\r\n\t\t\tscaleX = mEvt.delta > 0 ? scaleX + 0.03 : scaleX - 0.03;\r\n\t\t\tscaleY = scaleX;\r\n\t\t\t\r\n\t\t\tview.cameraOffset = new MathVector(stage.stageWidth \/ 2 \/ scaleX, stage.stageHeight \/ 2 \/ scaleY);\r\n\t\t\tview.cameraBounds = new Rectangle(0, 0, 1550 * scaleX, 450 * scaleY);\r\n\t\t}\r\n\t}\r\n}<\/pre>\n<p>In a physics world, you will never rotate the world! You just have to rotate its view, then manage the gravity like you want \ud83d\ude09<\/p>\n<p>November should be an awesome month for the Citrus Engine, so stay tuned!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a quick blog post to share a small tutorial on how you can handle a camera in the Citrus Engine and having feedbacks for the new CE&#8217;s structure.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[4,51,33,11,6],"tags":[15,50,34,26,74],"_links":{"self":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/771"}],"collection":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/comments?post=771"}],"version-history":[{"count":4,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/771\/revisions"}],"predecessor-version":[{"id":1267,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/771\/revisions\/1267"}],"wp:attachment":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/media?parent=771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/categories?post=771"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/tags?post=771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}