2D scene system with cameras
Monkey Archive Forums/Monkey Projects/2D scene system with cameras
| ||
| I haven't had an opportunity to touch Monkey code properly for a long time! But today I got some fun bit of code working in my lunch break. Controls -Hold 1,2 or 3 to select the different cameras/objects -While 1,2 or 3 are held: -- c rotates selected cameras -- o rotates selected objects -- up,down,left,right moves selected objects -- page-up/page-down zooms selected cameras http://www.skn3.com/junk/skn3engine/demos/cameras/index.html The scene is setup like so: object1 = Skn3EntityRect.Create("object1", 32, 32, 32, 32, 0, 0, 255)
AddChild(object1)
'add child object
object2 = Skn3EntityRect.Create("object2", 64, 0, 16, 16, 0, 255, 255)
object1.AddChild(object2)
'add child-child object
object3 = Skn3EntityRect.Create("object3", 32, 0, 8, 8, 255, 0, 255)
object2.AddChild(object3)
'add a camera to the scene and set its target to teh child object
camera1 = AddCamera(Skn3Camera.Create("camera1", 20, 20, 320, 440))
camera1.BackgroundTo(128, 128, 128)
camera1.BorderTo(255, 0, 0)
camera1.CrosshairTo(255, 0, 0, 0.3)
camera1.SetTarget(object3)
camera2 = AddCamera(Skn3Camera.Create("camera2", 360, 20, 260, 160))
camera2.BackgroundTo(128, 128, 128)
camera2.BorderTo(255, 0, 0)
camera2.CrosshairTo(255, 0, 0, 0.3)
camera2.SetTarget(object2)
camera3 = AddCamera(Skn3Camera.Create("camera3", 360, 200, 260, 260))
camera3.BackgroundTo(128, 128, 128)
camera3.BorderTo(255, 0, 0)
camera3.CrosshairTo(255, 0, 0, 0.3)
camera3.SetTarget(object1)Just thought Id post it here because I was happy I got it working! |
| ||
| Very nice! |
| ||
| interesting |
| ||
| Today I got some image caching stuff going, a console started (try pressing escape) and a system to automate mouse/touch input from screen/world into camera viewports. http://www.skn3.com/junk/skn3engine/demos/cameras02/MonkeyGame.html So Im working towards quite a powerful scene system which automates a lot of stuff. Here is the scene code for teh above demo: Class SceneGame Extends Skn3Scene
Field camera1:Skn3Camera
Field camera2:Skn3Camera
Field camera3:Skn3Camera
Field object1:Skn3EntityRect
Field object2:Skn3EntityRect
Field object3:Skn3EntityRect
Field image:Skn3Image
'constructor/destructor
Function Create:SceneGame(id:String)
' --- create the scene ---
'create new scene
Local scene:= New SceneGame
'setup
scene.OnSetupEntity(id)
scene.OnSetupScene()
'perform create
scene.OnCreate()
'return it
Return scene
End
'inherited events
Method OnCreate:Void()
' --- scene has been created ---
'set size of the scene
SizeTo(DeviceWidth(), DeviceHeight())
'add parent object
object1 = Skn3EntityRect.Create("object1", 32, 32, 32, 32, 0, 0, 255)
AddChild(object1)
'add child object
object2 = Skn3EntityRect.Create("object2", 64, 0, 16, 16, 0, 255, 255)
object1.AddChild(object2)
'add child-child object
object3 = Skn3EntityRect.Create("object3", 32, 0, 8, 8, 255, 0, 255)
object2.AddChild(object3)
'add cameras to the scene
camera1 = AddCamera(Skn3Camera.Create("camera1", 20, 20, 320, 440))
camera1.BackgroundTo(128, 128, 128)
camera1.BorderTo(255, 0, 0)
camera1.CrosshairTo(255, 0, 0, 0.3)
camera1.ScaleTo(0.5)
camera2 = AddCamera(Skn3Camera.Create("camera2", 360, 20, 260, 160))
camera2.BackgroundTo(128, 128, 128)
camera2.BorderTo(255, 0, 0)
camera2.CrosshairTo(255, 0, 0, 0.3)
camera2.ScaleTo(0.1)
camera3 = AddCamera(Skn3Camera.Create("camera3", 360, 200, 260, 260))
camera3.BackgroundTo(128, 128, 128)
camera3.BorderTo(255, 0, 0)
camera3.CrosshairTo(255, 0, 0, 0.3)
camera3.ScaleTo(0.2)
'load an image
image = Skn3Image.Create("scrap/monkey.jpg")
End
Method OnUpdate:Void()
' --- scene is updating ---
'position object2 at mouse coords depending on what camera we are hovering over, the true param indicates that we are position at scene world coords
If Skn3Runtime.MouseActive() object2.MoveTo(Skn3Runtime.MouseX(), Skn3Runtime.MouseY(), True)
'make stuff happen to each camera
camera1.RotateBy(30)
camera2.RotateBy(-15)
camera3.RotateBy(5)
camera3.ScaleTo(1 + ( (Cos(Skn3Runtime.Ms() * 0.1) + 1) * 0.5))
End
Method OnRender:Void()
'draw monkey image
SetAlpha(0.5)
SetColor(255, 255, 255)
image.Draw(0.0, 0.0, DeviceWidth(), DeviceHeight())
'draw the lines connecting the objects
Local pos1:Float[2]
Local pos2:Float[2]
Local pos3:Float[2]
object1.GetWorldPosition(pos1)
object2.GetWorldPosition(pos2)
object3.GetWorldPosition(pos3)
SetAlpha(1.0)
SetColor(45, 45, 45)
DrawLine(pos1[0], pos1[1], pos2[0], pos2[1])
DrawLine(pos2[0], pos2[1], pos3[0], pos3[1])
End
End |
| ||
| Added a generic tilemap renderer plus made more progress with lots of stuff. The blow examples sets up the grid like so: Move around with up,down,left,right http://www.skn3.com/junk/skn3engine/demos/grid01/MonkeyGame.html |