using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Collections.Generic; using System.Diagnostics; using LynxEngine; //---------------------------------------------- // 說明: //---------------------------------------------- public class CBGLayer { public float ObjectWidth, ObjectHeight; public uint ObjectInterval; public float PosY; public uint NumObjects; public float ScrollingSpeed; public CDynamicObj[] Objects = null; public float[] Pos = null;
public CBGLayer() { }
public void CreateObjects( CScene scene, int n, String name, String filename, CRenderableObj.DEPTHLAYERTYPE dl) { Objects = new CDynamicObj[n]; Pos = new float[n]; for (uint i=0; i < Objects.GetLength(0); i++) { Objects[i] = scene.CreateDynamicEntity(); Objects[i].CreateSprite( name, filename, ObjectWidth, ObjectHeight); Objects[i].UpdateTransform(); Objects[i].SetDepthLayer(dl); scene.AddDynamicObj(Objects[i]); } } } //---------------------------------------------- // 說明: //---------------------------------------------- class CPlayer : CDynamicObj { public enum STATE { RUN = 0, JUMP, ATTACK1 };
public class CState { public CSound Sound = null; public CAnimation Animation = null; }
public STATE State; public CState [] States = null;
public CPlayer(IntPtr ptr) : base(ptr) { }
public void SetState(STATE state) { States[(uint)State].Sound.Stop();
SetCurrentAnimation( States[(uint)state].Animation); States[(uint)state].Sound.Play(); State = state; } } //---------------------------------------------- // 說明: //---------------------------------------------- public class CScrollingGamePage : CUIPage { bool GameConsoleIsRunning = false; CBGLayer[] BGLayers = new CBGLayer[4]; CPlayer Player; CSound BGM; CVector2 [] BackgroundUV = new CVector2 [2]; CScene Scene;
new public void OnCreate() { Scene = GlobalVar.SceneSystem.AddScene(); Scene.SetName("Scrolling Game Scene") ;
CCamera Camera = Scene.CreateCamera(); Camera.Create(); Scene.SetCurrentCamera(Camera); CCameraContainer CameraContainer = (CCameraContainer)Camera.GetContainer();
Scene:SetRenderMode(CScene.RENDERMODE.RENDER_SIMPLE) Scene:SetSortMode(CScene.SORTMODE.SORT_BY_DEPTH_LAYER) Scene:Setup() end ------------------------------------------------ -- 說明: ------------------------------------------------ CScrollingGamePage.OnInit = function () CEngine:DeleteLastUIPage() Player.SetState(Player.STATE.RUN)
for i=0,3 do for j=0, BGLayer[i].NumObjects-1 do BGLayer[i][j].Pos = j*BGLayer[i].ObjectInterval end end
CSound_Cast(BGM):SetVolume(0.6) CSound_Cast(BGM):Play() end ------------------------------------------------ -- 說明: ------------------------------------------------ MouseDevice = CMouse_Cast(CInputSystem:FindDevice(L"System Mouse")) ------------------------------------------------ -- 說明: ------------------------------------------------ CScrollingGamePage.AnimationLoop = function () if (MouseDevice:ButtonStatus(RBUTTON) == TRUE and Player.State == Player.STATE.RUN) then Player.SetState(Player.STATE.JUMP) end
if (MouseDevice:ButtonStatus(LBUTTON) == TRUE and Player.State == Player.STATE.RUN) then Player.SetState(Player.STATE.ATTACK1) end
if (Player.State ~= Player.STATE.RUN) then if (CDynamicObj_Cast( Player.DynObj):IsCurrentAnimationStopped() == TRUE) then Player.SetState(Player.STATE.RUN) end end end ------------------------------------------------ -- 說明: ------------------------------------------------ CScrollingGamePage.OnLoop = function (step) MouseDevice:Poll()
if (CGameConsoleSystem:IsRunning() == TRUE) then GameConsoleIsRunning = true CSound_Cast(Player.States[Player.State].Sound):Pause() else CScrollingGamePage:AnimationLoop()
if (GameConsoleIsRunning == true) then if (CSound_Cast( Player.States[Player.State].Sound):IsPlaying() == FALSE) then CSound_Cast(Player.States[Player.State].Sound):Play() end GameConsoleIsRunning = false end
if (Player.State == Player.STATE.RUN or Player.State == Player.STATE.JUMP) then BackgroundUV.u0 = BackgroundUV.u0 + (step * 0.0003) BackgroundUV.u1 = BackgroundUV.u1 + (step * 0.0003) Scene:SetBackgroundTextureUVOffset( BackgroundUV.u0, BackgroundUV.v0, BackgroundUV.u1, BackgroundUV.v1)
for i=0,3 do for j=0, BGLayer[i].NumObjects-1 do BGLayer[i][j].Pos = BGLayer[i][j].Pos - BGLayer[i].ScrollingSpeed*step if (BGLayer[i][j].Pos < -BGLayer[i].ObjectWidth) then BGLayer[i][j].Pos = BGLayer[i][j].Pos + BGLayer[i].NumObjects*BGLayer[i].ObjectInterval end DynObj = CDynamicObj_Cast(BGLayer[i][j].DynObj) DynObj:SetPosition( CVector3(BGLayer[i][j].Pos, BGLayer[i].PosY, 0)) DynObj:UpdateTransform() end end end end end ------------------------------------------------ -- 說明: ------------------------------------------------ CScrollingGamePage.OnRender = function () CEngine:Render() end ------------------------------------------------ -- 說明: ------------------------------------------------ CScrollingGamePage.OnQuit = function () CUIPage:OnQuit(); end