Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

よくわかんないけど最近つくってるゲームで RxJava つかってみてる

よくわかんないけど最近つくってるゲームで RxJava つかってみてる

社内RxJava勉強会で発表した時の資料です。

Tomohiro Moro

March 18, 2015
Tweet

More Decks by Tomohiro Moro

Other Decks in Programming

Transcript

  1. ೖྗΠϕϯτ • touchDown -> δϟϯϓ४උ • tap -> δϟϯϓ •

    fling -> ࠨӈҠಈ • panStop -> δϟϯϓΩϟϯηϧ
  2. // Input PublishSubject<GameMaster.Action> subject = PublishSubject.create(); ! @Override public boolean

    touchDown(float x, float y, int pointer, int button) { subject.onNext(GameMaster.Action.CHARACTER_PREPARE_JUM return false; } ! @Override public boolean tap(float x, float y, int count, int button) { subject.onNext(GameMaster.Action.CHARACTER_JUMP); return false; } ! … ! public Observable<GameMaster.Action> getActions() {return subject;}
  3. // GameMaster public enum Action { CHARACTER_MOVE_LEFT, CHARACTER_MOVE_RIG ! public

    GameMaster(Observable<Action> input) { this.input = input; this.character = new Character(input); … ! // Character public enum Action { MOVE_LEFT, MOVE_RIGHT, JUMP, … } public Character(Observable<GameMaster.Action> input) { this.input = input; } ! public Observable<Action> getActions() { return input.map(new ActionConverter()); } !
  4. // Character private class ActionConverter implements Func1<GameMaster.Action, Action> { !

    @Override public Action call(GameMaster.Action action) { Action result = null; switch (action) { case CHARACTER_MOVE_LEFT: result = Action.MOVE_LEFT; break; case CHARACTER_MOVE_RIGHT: result = Action.MOVE_RIGHT; break; case CHARACTER_PREPARE_JUMP: result = Action.PREPARE_JUMP; break; … } return result; }
  5. Model Instance • LibGDX (3D) Ͱඳը͢ΔϞσϧΛ දݱ͢ΔΫϥε • xyz࠲ඪ ճస

    εέʔϧ ͳͲΛࢦఆ͢Δ • ύϥϝʔλΛม͑ΔΞχϝʔγϣϯΛ࣮ߦ͢Δ
  6. ҠಈͱΞχϝʔγϣϯ • δϟϯϓ४උ -> ্͔Βԡ͞ΕͨΑ͏ʹͭͿΕΔ • δϟϯϓ -> ͭͿΕΛ໭্ͭͭ͠ԼҠಈ •

    ࠨӈҠಈ -> ࠨӈʹඳըҐஔΛҠಈ • δϟϯϓΩϟϯηϧ -> ͭͿΕΛ໭͢
  7. // CharacterModelInstance public CharacterModelInstance(Model model, Character character) { super(model); …

    character.getActions().subscribe(new Action1<Character.Action>() { @Override public void call(Character.Action action) { switch (action) { case MOVE_LEFT: transform.translate(GroundBlockModel.SIZE, 0.0f, 0.0f); break; case MOVE_RIGHT: transform.translate(-GroundBlockModel.SIZE, 0.0f, 0.0f); break; case PREPARE_JUMP: animationController.animate("shrink", 0.0f); break; …
  8. // CameraMan public CameraMan(final OrthographicCamera camera, Character character) { this.camera

    = camera; this.character = character; character.getActions().subscribe(new Action1<Character.Action>() { @Override public void call(Character.Action action) { switch (action) { case MOVE_LEFT: Tween.to(camera, CameraTween.POSITION_X, cameraMoveDuration) .targetRelative(-GroundBlockModel.SIZE) .ease(TweenEquations.easeOutExpo) .start(tweenManager); break; …
  9. 0.12 0.32 0.14 0.21 update update update update 0.12 0.44

    0.58 0.79 Observable<Float> timeline = updates.scan((t1, t2) -> return t1 + t2);
  10. // GameMaster private PublishSubject<Action> actions = PublishSubject.create(); public GameMaster(Observable<Action> input)

    { this.input = input; this.character = new Character(input); this.groundGenerator = new GroundGenerator(); this.groundLineEmitter = new GroundLineEmitter(timeline, GROUND_LINE_SPAWN_INTERVAL); actions .mergeWith(input) .mergeWith(groundLineEmitter.getEmits()) .subscribe(new ActionProcessor());
  11. Ground Generator Ground Ground Layouter Ground Line #MPDL #MPDL #MPDL

    #MPDL *OTUBODF #MPDL *OTUBODF #MPDL *OTUBODF getNewLines() putLine(line) Layout next()