From e617dca052e8ee4c6762c476ff089ead6d5dcb5b Mon Sep 17 00:00:00 2001 From: oscarliang7 Date: Mon, 20 Nov 2023 15:13:08 +0800 Subject: [PATCH] Release Natty Engine 2.0 --- .idea/inspectionProfiles/Project_Default.xml | 17 ++ .idea/misc.xml | 5 + nattyengine/build.gradle | 1 + nattyengine/src/main/AndroidManifest.xml | 3 +- .../java/com/nativegame/nattyengine/Game.java | 47 +-- .../nattyengine/camera/BaseCamera.java | 238 +++++++++++++++ .../nativegame/nattyengine/camera/Camera.java | 56 ++++ .../nattyengine/camera/FixedCamera.java | 33 +++ .../nattyengine/camera/SmoothCamera.java | 90 ++++++ .../modifier/CameraPositionModifier.java | 66 +++++ .../modifier/CameraPositionXModifier.java | 63 ++++ .../modifier/CameraPositionYModifier.java | 63 ++++ .../camera/modifier/CameraZoomModifier.java | 63 ++++ .../modifier/DoubleValueCameraModifier.java} | 24 +- .../modifier/SingleValueCameraModifier.java | 30 ++ .../nattyengine/engine/Disposable.java | 13 - .../nativegame/nattyengine/engine/Engine.java | 278 ++++++++++++------ .../nattyengine/engine/EngineListener.java | 22 ++ .../nattyengine/engine/camera/Camera.java | 143 --------- .../engine/camera/CameraListener.java | 11 - .../engine/collision/Collidable.java | 21 -- .../engine/collision/CollisionType.java | 11 - .../engine/collision/algorithm/Collision.java | 57 ---- .../algorithm/CollisionAlgorithm.java | 101 ------- .../engine/collision/algorithm/QuadTree.java | 76 ----- .../collision/algorithm/QuadTreeNode.java | 134 --------- .../engine/collision/hitbox/BaseHitBox.java | 43 --- .../engine/collision/hitbox/HitBox.java | 18 -- .../collision/hitbox/RectangleHitBox.java | 41 --- .../engine/collision/hitbox/SpriteHitBox.java | 33 --- .../nattyengine/engine/loop/BaseLoop.java | 8 +- .../nattyengine/engine/loop/DrawLoop.java | 2 +- .../nattyengine/engine/loop/Loop.java | 2 - .../nattyengine/engine/loop/UpdateLoop.java | 2 +- .../{engine => entity}/Drawable.java | 8 +- .../nativegame/nattyengine/entity/Entity.java | 51 +++- .../nattyengine/entity/Manipulable.java | 57 ++++ .../nattyengine/entity/Releasable.java | 13 + .../nattyengine/entity/Reusable.java | 19 ++ .../nattyengine/entity/ScreenEntity.java | 47 +-- .../{engine => entity}/Updatable.java | 8 +- .../entity/counter/EntityCounter.java | 81 +++++ .../entity/counter/FPSCounter.java | 56 ++++ .../entity/counter/UPSCounter.java | 50 ++++ .../{sprite => }/modifier/AlphaModifier.java | 20 +- .../modifier/DoubleValueShapeModifier.java | 64 ++++ .../{sprite => }/modifier/FadeInModifier.java | 4 +- .../modifier/FadeOutModifier.java | 4 +- .../{sprite => }/modifier/PathModifier.java | 30 +- .../modifier/PositionModifier.java | 26 +- .../modifier/PositionXModifier.java | 20 +- .../modifier/PositionYModifier.java | 20 +- .../modifier/RotationModifier.java | 20 +- .../modifier/ScaleInModifier.java | 4 +- .../{sprite => }/modifier/ScaleModifier.java | 26 +- .../modifier/ScaleOutModifier.java | 4 +- .../modifier/SingleValueShapeModifier.java | 60 ++++ .../entity/particle/GenericParticle.java | 197 +++++++++++++ .../GenericParticleSystem.java} | 224 ++++++++------ .../nattyengine/entity/particle/Particle.java | 43 +++ .../entity/particle/ParticleSystem.java | 92 ++++++ .../ParticleSystemGroup.java | 23 +- .../entity/particle/SpriteParticleSystem.java | 20 ++ .../AccelerationXParticleInitializer.java | 28 ++ .../AccelerationYParticleInitializer.java | 28 ++ .../ColorParticleInitializer.java} | 19 +- .../DoubleValueParticleInitializer.java | 45 +++ .../DurationParticleInitialize.java | 4 +- .../initializer/ParticleInitializer.java | 4 +- .../RotationParticleInitializer.java | 28 ++ .../RotationSpeedParticleInitializer.java | 28 ++ .../SingleValueParticleInitializer.java | 40 +++ .../SpeedWithAngleParticleInitializer.java | 32 ++ .../SpeedXParticleInitializer.java | 28 ++ .../SpeedYParticleInitializer.java | 28 ++ .../modifier/AlphaParticleModifier.java | 28 ++ .../modifier/DoubleValueParticleModifier.java | 51 ++++ .../modifier/ParticleModifier.java | 4 +- .../modifier/RotationParticleModifier.java | 28 ++ .../modifier/ScaleParticleModifier.java | 28 ++ .../SingleValueParticleModifier.java} | 18 +- .../particle/shape/CircleParticleSystem.java | 22 ++ .../particle/shape/OvalParticleSystem.java | 22 ++ .../shape/RectangleParticleSystem.java | 26 ++ .../entity/particles/Particle.java | 112 ------- .../AccelerationXParticleInitializer.java | 34 --- .../AccelerationYParticleInitializer.java | 34 --- .../RotationParticleInitializer.java | 34 --- .../RotationSpeedParticleInitializer.java | 34 --- .../initializer/SpeedParticleInitializer.java | 43 --- .../SpeedXParticleInitializer.java | 34 --- .../SpeedYParticleInitializer.java | 34 --- .../modifier/AlphaParticleModifier.java | 40 --- .../modifier/RotationParticleModifier.java | 40 --- .../nattyengine/entity/primitive/Grid.java | 106 ------- .../nattyengine/entity/shape/Circle.java | 52 ---- .../nattyengine/entity/shape/Rectangle.java | 81 ----- .../entity/shape/RectangleShape.java | 147 +++++---- .../nattyengine/entity/shape/Shape.java | 201 +++++++++++-- .../entity/shape/geometry/Circle.java | 83 ++++++ .../entity/shape/{ => geometry}/Oval.java | 40 ++- .../entity/shape/geometry/Rectangle.java | 35 +++ .../entity/shape/primitive/Grid.java | 80 +++++ .../entity/{ => shape}/primitive/Line.java | 30 +- .../Color.java => shape/primitive/Plane.java} | 29 +- .../entity/{ => shape}/primitive/Point.java | 20 +- ...AnimatedSprite.java => AnimateSprite.java} | 147 ++++++--- .../entity/sprite/ButtonSprite.java | 120 ++++++++ .../{TileSprite.java => FrameSprite.java} | 34 +-- .../nattyengine/entity/sprite/Sprite.java | 144 +-------- .../entity/sprite/animation/Animation.java | 62 ---- .../modifier/SingleValueSpriteModifier.java | 34 --- .../nattyengine/entity/text/Text.java | 128 +++++++- .../nattyengine/entity/timer/Timer.java | 117 ++++---- .../nattyengine/{engine => }/event/Event.java | 2 +- .../{engine => }/event/EventListener.java | 2 +- .../input/touch/BaseTouchController.java | 16 +- .../input/touch/BoundTouchEventListener.java | 25 ++ .../input/touch/SingleTouchController.java | 6 +- .../input/touch/TouchEventListener.java | 6 + .../nattyengine/scene/BaseScene.java | 118 ++++++++ .../nattyengine/scene/MultiScene.java | 91 ++++++ .../nativegame/nattyengine/scene/Scene.java | 29 ++ .../nattyengine/scene/SceneController.java | 120 ++++++++ .../nattyengine/scene/SingleScene.java | 17 ++ .../nattyengine/ui/GameActivity.java | 34 ++- .../nattyengine/ui/GameFragment.java | 8 +- .../nativegame/nattyengine/ui/GameView.java | 4 +- .../{ClipUtils.java => BitmapClipUtils.java} | 4 +- .../nattyengine/util/debug/Debugger.java | 82 ++++++ .../util/exception/EngineException.java | 29 ++ .../exception/EngineRuntimeException.java | 29 ++ .../nattyengine/util/math/RandomUtils.java | 1 - .../util/math/ResolutionUtils.java | 32 ++ .../nattyengine/util/math/TransformUtils.java | 44 +++ .../modifier/BaseModifier.java | 20 +- .../modifier/BaseValueModifier.java | 7 +- .../modifier/DoubleValueModifier.java | 11 +- .../modifier/DurationModifier.java | 2 +- .../{entity => util}/modifier/Modifier.java | 10 +- .../modifier/SingleValueModifier.java | 7 +- .../modifier/tween/AccelerateTweener.java | 2 +- .../modifier/tween/AnticipateTweener.java | 2 +- .../modifier/tween/BounceTweener.java | 2 +- .../modifier/tween/DecelerateTweener.java | 2 +- .../modifier/tween/LinearTweener.java | 2 +- .../modifier/tween/OvershootTweener.java | 2 +- .../modifier/tween/Tweener.java | 2 +- .../pool/{ObjectPool.java => BasePool.java} | 44 +-- .../util/pool/FixedObjectPool.java | 48 +++ .../nattyengine/util/pool/GrowObjectPool.java | 31 ++ .../nattyengine/util/pool/Pool.java | 8 +- .../pool/SafeFixedObjectPool.java} | 23 +- 153 files changed, 4254 insertions(+), 2444 deletions(-) create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/camera/BaseCamera.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/camera/Camera.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/camera/FixedCamera.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/camera/SmoothCamera.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/camera/modifier/CameraPositionModifier.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/camera/modifier/CameraPositionXModifier.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/camera/modifier/CameraPositionYModifier.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/camera/modifier/CameraZoomModifier.java rename nattyengine/src/main/java/com/nativegame/nattyengine/{entity/sprite/modifier/DoubleValueSpriteModifier.java => camera/modifier/DoubleValueCameraModifier.java} (61%) create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/camera/modifier/SingleValueCameraModifier.java delete mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/engine/Disposable.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/engine/EngineListener.java delete mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/engine/camera/Camera.java delete mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/engine/camera/CameraListener.java delete mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/Collidable.java delete mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/CollisionType.java delete mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/algorithm/Collision.java delete mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/algorithm/CollisionAlgorithm.java delete mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/algorithm/QuadTree.java delete mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/algorithm/QuadTreeNode.java delete mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/hitbox/BaseHitBox.java delete mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/hitbox/HitBox.java delete mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/hitbox/RectangleHitBox.java delete mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/hitbox/SpriteHitBox.java rename nattyengine/src/main/java/com/nativegame/nattyengine/{engine => entity}/Drawable.java (65%) create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/Manipulable.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/Releasable.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/Reusable.java rename nattyengine/src/main/java/com/nativegame/nattyengine/{engine => entity}/Updatable.java (72%) create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/counter/EntityCounter.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/counter/FPSCounter.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/counter/UPSCounter.java rename nattyengine/src/main/java/com/nativegame/nattyengine/entity/{sprite => }/modifier/AlphaModifier.java (74%) create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/DoubleValueShapeModifier.java rename nattyengine/src/main/java/com/nativegame/nattyengine/entity/{sprite => }/modifier/FadeInModifier.java (85%) rename nattyengine/src/main/java/com/nativegame/nattyengine/entity/{sprite => }/modifier/FadeOutModifier.java (86%) rename nattyengine/src/main/java/com/nativegame/nattyengine/entity/{sprite => }/modifier/PathModifier.java (65%) rename nattyengine/src/main/java/com/nativegame/nattyengine/entity/{sprite => }/modifier/PositionModifier.java (74%) rename nattyengine/src/main/java/com/nativegame/nattyengine/entity/{sprite => }/modifier/PositionXModifier.java (75%) rename nattyengine/src/main/java/com/nativegame/nattyengine/entity/{sprite => }/modifier/PositionYModifier.java (75%) rename nattyengine/src/main/java/com/nativegame/nattyengine/entity/{sprite => }/modifier/RotationModifier.java (68%) rename nattyengine/src/main/java/com/nativegame/nattyengine/entity/{sprite => }/modifier/ScaleInModifier.java (85%) rename nattyengine/src/main/java/com/nativegame/nattyengine/entity/{sprite => }/modifier/ScaleModifier.java (79%) rename nattyengine/src/main/java/com/nativegame/nattyengine/entity/{sprite => }/modifier/ScaleOutModifier.java (85%) create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/SingleValueShapeModifier.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/GenericParticle.java rename nattyengine/src/main/java/com/nativegame/nattyengine/entity/{particles/ParticleSystem.java => particle/GenericParticleSystem.java} (72%) create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/Particle.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/ParticleSystem.java rename nattyengine/src/main/java/com/nativegame/nattyengine/entity/{particles => particle}/ParticleSystemGroup.java (94%) create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/SpriteParticleSystem.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/AccelerationXParticleInitializer.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/AccelerationYParticleInitializer.java rename nattyengine/src/main/java/com/nativegame/nattyengine/{engine/collision/hitbox/CircleHitBox.java => entity/particle/initializer/ColorParticleInitializer.java} (51%) create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/DoubleValueParticleInitializer.java rename nattyengine/src/main/java/com/nativegame/nattyengine/entity/{particles => particle}/initializer/DurationParticleInitialize.java (86%) rename nattyengine/src/main/java/com/nativegame/nattyengine/entity/{particles => particle}/initializer/ParticleInitializer.java (51%) create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/RotationParticleInitializer.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/RotationSpeedParticleInitializer.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/SingleValueParticleInitializer.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/SpeedWithAngleParticleInitializer.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/SpeedXParticleInitializer.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/SpeedYParticleInitializer.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/modifier/AlphaParticleModifier.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/modifier/DoubleValueParticleModifier.java rename nattyengine/src/main/java/com/nativegame/nattyengine/entity/{particles => particle}/modifier/ParticleModifier.java (55%) create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/modifier/RotationParticleModifier.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/modifier/ScaleParticleModifier.java rename nattyengine/src/main/java/com/nativegame/nattyengine/entity/{particles/modifier/ScaleParticleModifier.java => particle/modifier/SingleValueParticleModifier.java} (60%) create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/shape/CircleParticleSystem.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/shape/OvalParticleSystem.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/shape/RectangleParticleSystem.java delete mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/Particle.java delete mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/initializer/AccelerationXParticleInitializer.java delete mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/initializer/AccelerationYParticleInitializer.java delete mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/initializer/RotationParticleInitializer.java delete mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/initializer/RotationSpeedParticleInitializer.java delete mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/initializer/SpeedParticleInitializer.java delete mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/initializer/SpeedXParticleInitializer.java delete mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/initializer/SpeedYParticleInitializer.java delete mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/modifier/AlphaParticleModifier.java delete mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/modifier/RotationParticleModifier.java delete mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/primitive/Grid.java delete mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/Circle.java delete mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/Rectangle.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/geometry/Circle.java rename nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/{ => geometry}/Oval.java (57%) create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/geometry/Rectangle.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/primitive/Grid.java rename nattyengine/src/main/java/com/nativegame/nattyengine/entity/{ => shape}/primitive/Line.java (62%) rename nattyengine/src/main/java/com/nativegame/nattyengine/entity/{primitive/Color.java => shape/primitive/Plane.java} (66%) rename nattyengine/src/main/java/com/nativegame/nattyengine/entity/{ => shape}/primitive/Point.java (55%) rename nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/{animation/AnimatedSprite.java => AnimateSprite.java} (56%) create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/ButtonSprite.java rename nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/{TileSprite.java => FrameSprite.java} (59%) delete mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/animation/Animation.java delete mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/SingleValueSpriteModifier.java rename nattyengine/src/main/java/com/nativegame/nattyengine/{engine => }/event/Event.java (61%) rename nattyengine/src/main/java/com/nativegame/nattyengine/{engine => }/event/EventListener.java (70%) create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/input/touch/BoundTouchEventListener.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/scene/BaseScene.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/scene/MultiScene.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/scene/Scene.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/scene/SceneController.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/scene/SingleScene.java rename nattyengine/src/main/java/com/nativegame/nattyengine/util/bitmap/{ClipUtils.java => BitmapClipUtils.java} (98%) create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/util/debug/Debugger.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/util/exception/EngineException.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/util/exception/EngineRuntimeException.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/util/math/ResolutionUtils.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/util/math/TransformUtils.java rename nattyengine/src/main/java/com/nativegame/nattyengine/{entity => util}/modifier/BaseModifier.java (86%) rename nattyengine/src/main/java/com/nativegame/nattyengine/{entity => util}/modifier/BaseValueModifier.java (91%) rename nattyengine/src/main/java/com/nativegame/nattyengine/{entity => util}/modifier/DoubleValueModifier.java (88%) rename nattyengine/src/main/java/com/nativegame/nattyengine/{entity => util}/modifier/DurationModifier.java (95%) rename nattyengine/src/main/java/com/nativegame/nattyengine/{entity => util}/modifier/Modifier.java (73%) rename nattyengine/src/main/java/com/nativegame/nattyengine/{entity => util}/modifier/SingleValueModifier.java (89%) rename nattyengine/src/main/java/com/nativegame/nattyengine/{entity => util}/modifier/tween/AccelerateTweener.java (95%) rename nattyengine/src/main/java/com/nativegame/nattyengine/{entity => util}/modifier/tween/AnticipateTweener.java (95%) rename nattyengine/src/main/java/com/nativegame/nattyengine/{entity => util}/modifier/tween/BounceTweener.java (95%) rename nattyengine/src/main/java/com/nativegame/nattyengine/{entity => util}/modifier/tween/DecelerateTweener.java (95%) rename nattyengine/src/main/java/com/nativegame/nattyengine/{entity => util}/modifier/tween/LinearTweener.java (95%) rename nattyengine/src/main/java/com/nativegame/nattyengine/{entity => util}/modifier/tween/OvershootTweener.java (95%) rename nattyengine/src/main/java/com/nativegame/nattyengine/{entity => util}/modifier/tween/Tweener.java (68%) rename nattyengine/src/main/java/com/nativegame/nattyengine/util/pool/{ObjectPool.java => BasePool.java} (58%) create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/util/pool/FixedObjectPool.java create mode 100644 nattyengine/src/main/java/com/nativegame/nattyengine/util/pool/GrowObjectPool.java rename nattyengine/src/main/java/com/nativegame/nattyengine/{engine/collision/hitbox/OvalHitBox.java => util/pool/SafeFixedObjectPool.java} (50%) diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml index 4708f0c..5b2739d 100644 --- a/.idea/inspectionProfiles/Project_Default.xml +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -1,12 +1,29 @@ diff --git a/nattyengine/build.gradle b/nattyengine/build.gradle index 2e5419e..5cb9524 100644 --- a/nattyengine/build.gradle +++ b/nattyengine/build.gradle @@ -26,4 +26,5 @@ android { dependencies { implementation 'androidx.appcompat:appcompat:1.6.1' + implementation 'org.jbox2d:jbox2d-library:2.2.1.1' } \ No newline at end of file diff --git a/nattyengine/src/main/AndroidManifest.xml b/nattyengine/src/main/AndroidManifest.xml index 4d63116..542c6b4 100644 --- a/nattyengine/src/main/AndroidManifest.xml +++ b/nattyengine/src/main/AndroidManifest.xml @@ -1,5 +1,4 @@ - + \ No newline at end of file diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/Game.java b/nattyengine/src/main/java/com/nativegame/nattyengine/Game.java index b1d995a..9bf89e8 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/Game.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/Game.java @@ -1,8 +1,6 @@ package com.nativegame.nattyengine; import com.nativegame.nattyengine.engine.Engine; -import com.nativegame.nattyengine.input.sensor.AccelerationController; -import com.nativegame.nattyengine.input.sensor.OrientationController; import com.nativegame.nattyengine.ui.GameActivity; import com.nativegame.nattyengine.ui.GameView; @@ -36,15 +34,18 @@ public class Game { - protected final GameActivity mActivity; - protected final Engine mEngine; + private final GameActivity mActivity; + private final GameView mGameView; + private final Engine mEngine; //-------------------------------------------------------- // Constructors //-------------------------------------------------------- - public Game(GameActivity activity, GameView gameView) { + public Game(GameActivity activity, GameView gameView, Engine engine) { mActivity = activity; - mEngine = new Engine(gameView); + mGameView = gameView; + mEngine = engine; + mEngine.setGameView(gameView); } //======================================================== @@ -55,6 +56,10 @@ public GameActivity getActivity() { return mActivity; } + public GameView getGameView() { + return mGameView; + } + public Engine getEngine() { return mEngine; } @@ -64,14 +69,16 @@ public Engine getEngine() { // Methods //-------------------------------------------------------- public final void start() { - mEngine.startGame(); - onStart(); + if (!mEngine.isRunning()) { + mEngine.startGame(); + onStart(); + } } public final void stop() { if (mEngine.isRunning()) { mEngine.stopGame(); - mEngine.disposeGame(); + mEngine.releaseGame(); onStop(); } } @@ -101,28 +108,6 @@ protected void onPause() { protected void onResume() { } - - public void enableAccelerationSensor() { - mEngine.setAccelerationController(new AccelerationController(mActivity)); - } - - public void disableAccelerationSensor() { - if (mEngine.getAccelerationController() != null) { - mEngine.getAccelerationController().stop(); - } - mEngine.setAccelerationController(null); - } - - public void enableOrientationSensor() { - mEngine.setOrientationController(new OrientationController(mActivity)); - } - - public void disableOrientationSensor() { - if (mEngine.getOrientationController() != null) { - mEngine.getOrientationController().stop(); - } - mEngine.setOrientationController(null); - } //======================================================== } diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/camera/BaseCamera.java b/nattyengine/src/main/java/com/nativegame/nattyengine/camera/BaseCamera.java new file mode 100644 index 0000000..049ed2d --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/camera/BaseCamera.java @@ -0,0 +1,238 @@ +package com.nativegame.nattyengine.camera; + +import com.nativegame.nattyengine.util.exception.EngineRuntimeException; +import com.nativegame.nattyengine.util.math.ResolutionUtils; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public abstract class BaseCamera implements Camera { + + private final int mCameraWidth; + private final int mCameraHeight; + private final int mWorldWidth; + private final int mWorldHeight; + private final int mProjectWorldWidth; + private final int mProjectWorldHeight; + private final float mPixelFactor; + + private float mX; + private float mY; + private float mZoom = 1.0f; + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + protected BaseCamera(int cameraWidth, int cameraHeight) { + this(cameraWidth, cameraHeight, cameraWidth, cameraHeight); + } + + protected BaseCamera(int cameraWidth, int cameraHeight, + int worldWidth, int worldHeight) { + this(cameraWidth, cameraHeight, worldWidth, worldHeight, + ResolutionUtils.getResolutionWidth(cameraWidth, cameraHeight, worldWidth, worldHeight), + ResolutionUtils.getResolutionHeight(cameraWidth, cameraHeight, worldWidth, worldHeight)); + } + + protected BaseCamera(int cameraWidth, int cameraHeight, + int worldWidth, int worldHeight, + int projectWorldWidth, int projectWorldHeight) { + mPixelFactor = projectWorldWidth * 1f / worldWidth; + mCameraWidth = (int) (cameraWidth / mPixelFactor); + mCameraHeight = (int) (cameraHeight / mPixelFactor); + mWorldWidth = worldWidth; + mWorldHeight = worldHeight; + mProjectWorldWidth = projectWorldWidth; + mProjectWorldHeight = projectWorldHeight; + } + //======================================================== + + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + public int getCameraWidth() { + return (int) (mCameraWidth / mZoom); + } + + @Override + public int getCameraHeight() { + return (int) (mCameraHeight / mZoom); + } + + @Override + public int getWorldWidth() { + return mWorldWidth; + } + + @Override + public int getWorldHeight() { + return mWorldHeight; + } + + @Override + public float getPixelFactor() { + return mPixelFactor; + } + + @Override + public float getX() { + return getCenterX() - (mCameraWidth / 2f) / mZoom; + } + + @Override + public void setX(float x) { + mX = x; + if (mX < 0) { + mX = 0; + } + if (mX > mWorldWidth - mCameraWidth) { + mX = mWorldWidth - mCameraWidth; + } + } + + @Override + public float getY() { + return getCenterY() - (mCameraHeight / 2f) / mZoom; + } + + @Override + public void setY(float y) { + mY = y; + if (mY < 0) { + mY = 0; + } + if (mY > mWorldHeight - mCameraHeight) { + mY = mWorldHeight - mCameraHeight; + } + } + + @Override + public float getCenterX() { + return mX + mCameraWidth / 2f; + } + + @Override + public void setCenterX(float centerX) { + setX(centerX - mCameraWidth / 2f); + } + + @Override + public float getCenterY() { + return mY + mCameraHeight / 2f; + } + + @Override + public void setCenterY(float centerY) { + setY(centerY - mCameraHeight / 2f); + } + + @Override + public float getZoom() { + return mZoom; + } + + @Override + public void setZoom(float zoom) { + mZoom = zoom; + } + + @Override + public float getWorldToScreenZoom(CoordinateType type) { + switch (type) { + case WORLD: + return mZoom; + case CAMERA: + return 1; + default: + throw new EngineRuntimeException("CoordinateType not found!"); + } + } + + @Override + public float getWorldToScreenX(float worldX, CoordinateType type) { + switch (type) { + case WORLD: + return getProjectX(worldX) - getScreenMarginX(); + case CAMERA: + return getScreenWidth() / 2f - (mCameraWidth / 2f - worldX) * mPixelFactor; + default: + throw new EngineRuntimeException("CoordinateType not found!"); + } + } + + @Override + public float getWorldToScreenY(float worldY, CoordinateType type) { + switch (type) { + case WORLD: + return getProjectY(worldY) - getScreenMarginY(); + case CAMERA: + return getScreenHeight() / 2f - (mCameraHeight / 2f - worldY) * mPixelFactor; + default: + throw new EngineRuntimeException("CoordinateType not found!"); + } + } + + @Override + public float getScreenToWorldX(float screenX, CoordinateType type) { + switch (type) { + case WORLD: + return getWorldX(screenX + getScreenMarginX()); + case CAMERA: + return mCameraWidth / 2f - (getScreenWidth() / 2f - screenX) / mPixelFactor; + default: + throw new EngineRuntimeException("CoordinateType not found!"); + } + } + + @Override + public float getScreenToWorldY(float screenY, CoordinateType type) { + switch (type) { + case WORLD: + return getWorldY(screenY + getScreenMarginY()); + case CAMERA: + return mCameraHeight / 2f - (getScreenHeight() / 2f - screenY) / mPixelFactor; + default: + throw new EngineRuntimeException("CoordinateType not found!"); + } + } + //======================================================== + + //-------------------------------------------------------- + // Methods + //-------------------------------------------------------- + private float getProjectX(float worldX) { + return mProjectWorldWidth / 2f - (mWorldWidth / 2f - worldX) * mPixelFactor * mZoom; + } + + private float getProjectY(float worldY) { + return mProjectWorldHeight / 2f - (mWorldHeight / 2f - worldY) * mPixelFactor * mZoom; + } + + private float getWorldX(float projectX) { + return mWorldWidth / 2f - (mProjectWorldWidth / 2f - projectX) / mPixelFactor * mZoom; + } + + private float getWorldY(float projectY) { + return mWorldHeight / 2f - (mProjectWorldHeight / 2f - projectY) / mPixelFactor * mZoom; + } + + private float getScreenWidth() { + return mCameraWidth * mPixelFactor; + } + + private float getScreenHeight() { + return mCameraHeight * mPixelFactor; + } + + private float getScreenMarginX() { + return mX * mPixelFactor; + } + + private float getScreenMarginY() { + return mY * mPixelFactor; + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/camera/Camera.java b/nattyengine/src/main/java/com/nativegame/nattyengine/camera/Camera.java new file mode 100644 index 0000000..96cf04b --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/camera/Camera.java @@ -0,0 +1,56 @@ +package com.nativegame.nattyengine.camera; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public interface Camera { + + public enum CoordinateType { + WORLD, + CAMERA + } + + int getCameraWidth(); + + int getCameraHeight(); + + int getWorldWidth(); + + int getWorldHeight(); + + float getPixelFactor(); + + float getX(); + + void setX(float x); + + float getY(); + + void setY(float y); + + float getCenterX(); + + void setCenterX(float centerX); + + float getCenterY(); + + void setCenterY(float centerY); + + float getZoom(); + + void setZoom(float zoom); + + float getWorldToScreenZoom(CoordinateType type); + + float getWorldToScreenX(float worldX, CoordinateType type); + + float getWorldToScreenY(float worldY, CoordinateType type); + + float getScreenToWorldX(float screenX, CoordinateType type); + + float getScreenToWorldY(float screenY, CoordinateType type); + + void update(long elapsedMillis); + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/camera/FixedCamera.java b/nattyengine/src/main/java/com/nativegame/nattyengine/camera/FixedCamera.java new file mode 100644 index 0000000..c63a1f8 --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/camera/FixedCamera.java @@ -0,0 +1,33 @@ +package com.nativegame.nattyengine.camera; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public class FixedCamera extends BaseCamera { + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + public FixedCamera(int cameraWidth, int cameraHeight) { + super(cameraWidth, cameraHeight); + } + + public FixedCamera(int cameraWidth, int cameraHeight, int worldWidth, int worldHeight) { + super(cameraWidth, cameraHeight, worldWidth, worldHeight); + } + + public FixedCamera(int cameraWidth, int cameraHeight, int worldWidth, int worldHeight, int projectWorldWidth, int projectWorldHeight) { + super(cameraWidth, cameraHeight, worldWidth, worldHeight, projectWorldWidth, projectWorldHeight); + } + //======================================================== + + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + public void update(long elapsedMillis) { + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/camera/SmoothCamera.java b/nattyengine/src/main/java/com/nativegame/nattyengine/camera/SmoothCamera.java new file mode 100644 index 0000000..68f6ac1 --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/camera/SmoothCamera.java @@ -0,0 +1,90 @@ +package com.nativegame.nattyengine.camera; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public class SmoothCamera extends BaseCamera { + + private float mSpeedX; + private float mSpeedY; + private float mTargetX; + private float mTargetY; + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + public SmoothCamera(int cameraWidth, int cameraHeight) { + super(cameraWidth, cameraHeight); + } + + public SmoothCamera(int cameraWidth, int cameraHeight, int worldWidth, int worldHeight) { + super(cameraWidth, cameraHeight, worldWidth, worldHeight); + } + + public SmoothCamera(int cameraWidth, int cameraHeight, int worldWidth, int worldHeight, int projectWorldWidth, int projectWorldHeight) { + super(cameraWidth, cameraHeight, worldWidth, worldHeight, projectWorldWidth, projectWorldHeight); + } + //======================================================== + + //-------------------------------------------------------- + // Getter and Setter + //-------------------------------------------------------- + public float getSpeedX() { + return mSpeedX; + } + + public void setSpeedX(float speedX) { + mSpeedX = speedX; + } + + public float getSpeedY() { + return mSpeedY; + } + + public void setSpeedY(float speedY) { + mSpeedY = speedY; + } + + public float getTargetX() { + return mTargetX; + } + + public void setTargetX(float targetX) { + mTargetX = targetX; + } + + public float getTargetY() { + return mTargetY; + } + + public void setTargetY(float targetY) { + mTargetY = targetY; + } + //======================================================== + + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + public void update(long elapsedMillis) { + float offsetX = mSpeedX * elapsedMillis; + float offsetY = mSpeedY * elapsedMillis; + if (getCenterX() != mTargetX) { + if (getCenterX() < mTargetX) { + setCenterX(getCenterX() + Math.min(offsetX, mTargetX - getCenterX())); + } else { + setCenterX(getCenterX() - Math.min(offsetX, getCenterX() - mTargetX)); + } + } + if (getCenterY() != mTargetY) { + if (getCenterY() < mTargetY) { + setCenterY(getCenterY() + Math.min(offsetY, mTargetY - getCenterY())); + } else { + setCenterY(getCenterY() - Math.min(offsetY, getCenterY() - mTargetY)); + } + } + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/camera/modifier/CameraPositionModifier.java b/nattyengine/src/main/java/com/nativegame/nattyengine/camera/modifier/CameraPositionModifier.java new file mode 100644 index 0000000..5be4c5a --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/camera/modifier/CameraPositionModifier.java @@ -0,0 +1,66 @@ +package com.nativegame.nattyengine.camera.modifier; + +import com.nativegame.nattyengine.camera.Camera; +import com.nativegame.nattyengine.util.modifier.tween.Tweener; + +public class CameraPositionModifier extends DoubleValueCameraModifier { + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + public CameraPositionModifier(long duration) { + super(0, 0, 0, 0, duration); + } + + public CameraPositionModifier(long duration, long startDelay) { + super(0, 0, 0, 0, duration, startDelay); + } + + public CameraPositionModifier(long duration, Tweener tweener) { + super(0, 0, 0, 0, duration, tweener); + } + + public CameraPositionModifier(long duration, long startDelay, Tweener tweener) { + super(0, 0, 0, 0, duration, startDelay, tweener); + } + + public CameraPositionModifier(float startValueX, float endValueX, float startValueY, float endValueY, long duration) { + super(startValueX, endValueX, startValueY, endValueY, duration); + } + + public CameraPositionModifier(float startValueX, float endValueX, float startValueY, float endValueY, long duration, long startDelay) { + super(startValueX, endValueX, startValueY, endValueY, duration, startDelay); + } + + public CameraPositionModifier(float startValueX, float endValueX, float startValueY, float endValueY, long duration, Tweener tweener) { + super(startValueX, endValueX, startValueY, endValueY, duration, tweener); + } + + public CameraPositionModifier(float startValueX, float endValueX, float startValueY, float endValueY, long duration, long startDelay, Tweener tweener) { + super(startValueX, endValueX, startValueY, endValueY, duration, startDelay, tweener); + } + //======================================================== + + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + protected void onInitValue(Camera camera, float x, float y) { + camera.setX(x); + camera.setY(y); + } + + @Override + protected void onUpdateValue(Camera camera, float x, float y) { + camera.setX(x); + camera.setY(y); + } + + @Override + protected void onEndValue(Camera camera, float x, float y) { + camera.setX(x); + camera.setY(y); + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/camera/modifier/CameraPositionXModifier.java b/nattyengine/src/main/java/com/nativegame/nattyengine/camera/modifier/CameraPositionXModifier.java new file mode 100644 index 0000000..4f09159 --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/camera/modifier/CameraPositionXModifier.java @@ -0,0 +1,63 @@ +package com.nativegame.nattyengine.camera.modifier; + +import com.nativegame.nattyengine.camera.Camera; +import com.nativegame.nattyengine.util.modifier.tween.Tweener; + +public class CameraPositionXModifier extends SingleValueCameraModifier { + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + public CameraPositionXModifier(long duration) { + super(0, 0, duration); + } + + public CameraPositionXModifier(long duration, long startDelay) { + super(0, 0, duration, startDelay); + } + + public CameraPositionXModifier(long duration, Tweener tweener) { + super(0, 0, duration, tweener); + } + + public CameraPositionXModifier(long duration, long startDelay, Tweener tweener) { + super(0, 0, duration, startDelay, tweener); + } + + public CameraPositionXModifier(float startValue, float endValue, long duration) { + super(startValue, endValue, duration); + } + + public CameraPositionXModifier(float startValue, float endValue, long duration, long startDelay) { + super(startValue, endValue, duration, startDelay); + } + + public CameraPositionXModifier(float startValue, float endValue, long duration, Tweener tweener) { + super(startValue, endValue, duration, tweener); + } + + public CameraPositionXModifier(float startValue, float endValue, long duration, long startDelay, Tweener tweener) { + super(startValue, endValue, duration, startDelay, tweener); + } + //======================================================== + + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + protected void onInitValue(Camera camera, float x) { + camera.setX(x); + } + + @Override + protected void onUpdateValue(Camera camera, float x) { + camera.setX(x); + } + + @Override + protected void onEndValue(Camera camera, float x) { + camera.setX(x); + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/camera/modifier/CameraPositionYModifier.java b/nattyengine/src/main/java/com/nativegame/nattyengine/camera/modifier/CameraPositionYModifier.java new file mode 100644 index 0000000..500c54c --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/camera/modifier/CameraPositionYModifier.java @@ -0,0 +1,63 @@ +package com.nativegame.nattyengine.camera.modifier; + +import com.nativegame.nattyengine.camera.Camera; +import com.nativegame.nattyengine.util.modifier.tween.Tweener; + +public class CameraPositionYModifier extends SingleValueCameraModifier { + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + public CameraPositionYModifier(long duration) { + super(0, 0, duration); + } + + public CameraPositionYModifier(long duration, long startDelay) { + super(0, 0, duration, startDelay); + } + + public CameraPositionYModifier(long duration, Tweener tweener) { + super(0, 0, duration, tweener); + } + + public CameraPositionYModifier(long duration, long startDelay, Tweener tweener) { + super(0, 0, duration, startDelay, tweener); + } + + public CameraPositionYModifier(float startValue, float endValue, long duration) { + super(startValue, endValue, duration); + } + + public CameraPositionYModifier(float startValue, float endValue, long duration, long startDelay) { + super(startValue, endValue, duration, startDelay); + } + + public CameraPositionYModifier(float startValue, float endValue, long duration, Tweener tweener) { + super(startValue, endValue, duration, tweener); + } + + public CameraPositionYModifier(float startValue, float endValue, long duration, long startDelay, Tweener tweener) { + super(startValue, endValue, duration, startDelay, tweener); + } + //======================================================== + + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + protected void onInitValue(Camera camera, float y) { + camera.setY(y); + } + + @Override + protected void onUpdateValue(Camera camera, float y) { + camera.setY(y); + } + + @Override + protected void onEndValue(Camera camera, float y) { + camera.setY(y); + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/camera/modifier/CameraZoomModifier.java b/nattyengine/src/main/java/com/nativegame/nattyengine/camera/modifier/CameraZoomModifier.java new file mode 100644 index 0000000..5c33786 --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/camera/modifier/CameraZoomModifier.java @@ -0,0 +1,63 @@ +package com.nativegame.nattyengine.camera.modifier; + +import com.nativegame.nattyengine.camera.Camera; +import com.nativegame.nattyengine.util.modifier.tween.Tweener; + +public class CameraZoomModifier extends SingleValueCameraModifier { + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + public CameraZoomModifier(long duration) { + super(0, 0, duration); + } + + public CameraZoomModifier(long duration, long startDelay) { + super(0, 0, duration, startDelay); + } + + public CameraZoomModifier(long duration, Tweener tweener) { + super(0, 0, duration, tweener); + } + + public CameraZoomModifier(long duration, long startDelay, Tweener tweener) { + super(0, 0, duration, startDelay, tweener); + } + + public CameraZoomModifier(float startValue, float endValue, long duration) { + super(startValue, endValue, duration); + } + + public CameraZoomModifier(float startValue, float endValue, long duration, long startDelay) { + super(startValue, endValue, duration, startDelay); + } + + public CameraZoomModifier(float startValue, float endValue, long duration, Tweener tweener) { + super(startValue, endValue, duration, tweener); + } + + public CameraZoomModifier(float startValue, float endValue, long duration, long startDelay, Tweener tweener) { + super(startValue, endValue, duration, startDelay, tweener); + } + //======================================================== + + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + protected void onInitValue(Camera camera, float zoom) { + camera.setZoom(zoom); + } + + @Override + protected void onUpdateValue(Camera camera, float zoom) { + camera.setZoom(zoom); + } + + @Override + protected void onEndValue(Camera camera, float zoom) { + camera.setZoom(zoom); + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/DoubleValueSpriteModifier.java b/nattyengine/src/main/java/com/nativegame/nattyengine/camera/modifier/DoubleValueCameraModifier.java similarity index 61% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/DoubleValueSpriteModifier.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/camera/modifier/DoubleValueCameraModifier.java index d243d09..ce99dd3 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/DoubleValueSpriteModifier.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/camera/modifier/DoubleValueCameraModifier.java @@ -1,35 +1,31 @@ -package com.nativegame.nattyengine.entity.sprite.modifier; +package com.nativegame.nattyengine.camera.modifier; -import com.nativegame.nattyengine.entity.modifier.DoubleValueModifier; -import com.nativegame.nattyengine.entity.modifier.tween.LinearTweener; -import com.nativegame.nattyengine.entity.modifier.tween.Tweener; -import com.nativegame.nattyengine.entity.sprite.Sprite; +import com.nativegame.nattyengine.camera.Camera; +import com.nativegame.nattyengine.util.modifier.DoubleValueModifier; +import com.nativegame.nattyengine.util.modifier.tween.LinearTweener; +import com.nativegame.nattyengine.util.modifier.tween.Tweener; -/** - * Created by Oscar Liang on 2022/12/11 - */ - -public abstract class DoubleValueSpriteModifier extends DoubleValueModifier { +public abstract class DoubleValueCameraModifier extends DoubleValueModifier { //-------------------------------------------------------- // Constructors //-------------------------------------------------------- - protected DoubleValueSpriteModifier(float startValueX, float endValueX, float startValueY, float endValueY, + protected DoubleValueCameraModifier(float startValueX, float endValueX, float startValueY, float endValueY, long duration) { super(startValueX, endValueX, startValueY, endValueY, duration, 0, LinearTweener.getInstance()); } - protected DoubleValueSpriteModifier(float startValueX, float endValueX, float startValueY, float endValueY, + protected DoubleValueCameraModifier(float startValueX, float endValueX, float startValueY, float endValueY, long duration, long startDelay) { super(startValueX, endValueX, startValueY, endValueY, duration, startDelay, LinearTweener.getInstance()); } - protected DoubleValueSpriteModifier(float startValueX, float endValueX, float startValueY, float endValueY, + protected DoubleValueCameraModifier(float startValueX, float endValueX, float startValueY, float endValueY, long duration, Tweener tweener) { super(startValueX, endValueX, startValueY, endValueY, duration, 0, tweener); } - protected DoubleValueSpriteModifier(float startValueX, float endValueX, float startValueY, float endValueY, + protected DoubleValueCameraModifier(float startValueX, float endValueX, float startValueY, float endValueY, long duration, long startDelay, Tweener tweener) { super(startValueX, endValueX, startValueY, endValueY, duration, startDelay, tweener); } diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/camera/modifier/SingleValueCameraModifier.java b/nattyengine/src/main/java/com/nativegame/nattyengine/camera/modifier/SingleValueCameraModifier.java new file mode 100644 index 0000000..a025535 --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/camera/modifier/SingleValueCameraModifier.java @@ -0,0 +1,30 @@ +package com.nativegame.nattyengine.camera.modifier; + +import com.nativegame.nattyengine.camera.Camera; +import com.nativegame.nattyengine.util.modifier.SingleValueModifier; +import com.nativegame.nattyengine.util.modifier.tween.LinearTweener; +import com.nativegame.nattyengine.util.modifier.tween.Tweener; + +public abstract class SingleValueCameraModifier extends SingleValueModifier { + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + protected SingleValueCameraModifier(float startValue, float endValue, long duration) { + super(startValue, endValue, duration, 0, LinearTweener.getInstance()); + } + + protected SingleValueCameraModifier(float startValue, float endValue, long duration, long startDelay) { + super(startValue, endValue, duration, startDelay, LinearTweener.getInstance()); + } + + protected SingleValueCameraModifier(float startValue, float endValue, long duration, Tweener tweener) { + super(startValue, endValue, duration, 0, tweener); + } + + protected SingleValueCameraModifier(float startValue, float endValue, long duration, long startDelay, Tweener tweener) { + super(startValue, endValue, duration, startDelay, tweener); + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/Disposable.java b/nattyengine/src/main/java/com/nativegame/nattyengine/engine/Disposable.java deleted file mode 100644 index c9e3e96..0000000 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/Disposable.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.nativegame.nattyengine.engine; - -/** - * Created by Oscar Liang on 2022/12/11 - */ - -public interface Disposable { - - void dispose(); - - boolean isDisposed(); - -} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/Engine.java b/nattyengine/src/main/java/com/nativegame/nattyengine/engine/Engine.java index 3bd5051..3236efe 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/Engine.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/engine/Engine.java @@ -2,21 +2,25 @@ import android.graphics.Canvas; -import com.nativegame.nattyengine.engine.camera.Camera; -import com.nativegame.nattyengine.engine.camera.CameraListener; -import com.nativegame.nattyengine.engine.collision.Collidable; -import com.nativegame.nattyengine.engine.collision.algorithm.QuadTree; -import com.nativegame.nattyengine.engine.event.Event; -import com.nativegame.nattyengine.engine.event.EventListener; +import com.nativegame.nattyengine.camera.Camera; import com.nativegame.nattyengine.engine.loop.DrawLoop; import com.nativegame.nattyengine.engine.loop.UpdateLoop; +import com.nativegame.nattyengine.entity.Drawable; +import com.nativegame.nattyengine.entity.Releasable; +import com.nativegame.nattyengine.entity.Updatable; +import com.nativegame.nattyengine.event.Event; +import com.nativegame.nattyengine.event.EventListener; import com.nativegame.nattyengine.input.sensor.AccelerationController; import com.nativegame.nattyengine.input.sensor.OrientationController; -import com.nativegame.nattyengine.input.touch.SingleTouchController; +import com.nativegame.nattyengine.input.touch.BoundTouchEventListener; import com.nativegame.nattyengine.input.touch.TouchController; import com.nativegame.nattyengine.input.touch.TouchEvent; import com.nativegame.nattyengine.input.touch.TouchEventListener; +import com.nativegame.nattyengine.scene.Scene; +import com.nativegame.nattyengine.scene.SceneController; import com.nativegame.nattyengine.ui.GameView; +import com.nativegame.nattyengine.util.debug.Debugger; +import com.nativegame.nattyengine.util.exception.EngineRuntimeException; import java.util.ArrayList; import java.util.Collections; @@ -29,34 +33,47 @@ public class Engine implements UpdateLoop.UpdateListener, DrawLoop.DrawListener, GameView.OnDrawListener { - private final GameView mGameView; - + private GameView mGameView; + private Camera mCamera; private UpdateLoop mUpdateLoop; private DrawLoop mDrawLoop; - private Camera mCamera; private TouchController mTouchController; private AccelerationController mAccelerationController; private OrientationController mOrientationController; + private boolean mIsDebugMode = false; - private final QuadTree mQuadTree = new QuadTree(); + private final SceneController mSceneController = new SceneController(); + private final LayerComparator mLayerComparator = new LayerComparator(); + private final Debugger mDebugger = new Debugger(); private final List mUpdatables = new ArrayList<>(); private final List mDrawables = new ArrayList<>(); private final List mUpdatablesToAdd = new ArrayList<>(); private final List mUpdatablesToRemove = new ArrayList<>(); - private final LayerComparator mLayerComparator = new LayerComparator(); + private final List mListeners = new ArrayList<>(); //-------------------------------------------------------- // Constructors //-------------------------------------------------------- + public Engine() { + } + public Engine(GameView gameView) { - mGameView = gameView; - mGameView.setListener(this); + setGameView(gameView); } //======================================================== //-------------------------------------------------------- // Getter and Setter //-------------------------------------------------------- + public GameView getGameView() { + return mGameView; + } + + public void setGameView(GameView gameView) { + mGameView = gameView; + mGameView.setListener(this); + } + public Camera getCamera() { return mCamera; } @@ -65,6 +82,10 @@ public void setCamera(Camera camera) { mCamera = camera; } + public SceneController getSceneController() { + return mSceneController; + } + public TouchController getTouchController() { return mTouchController; } @@ -89,6 +110,22 @@ public void setOrientationController(OrientationController orientationController mOrientationController = orientationController; } + public boolean isDebugMode() { + return mIsDebugMode; + } + + public void setDebugMode(boolean debugMode) { + mIsDebugMode = debugMode; + } + + public Debugger getDebugger() { + return mDebugger; + } + + public void setDebugger(Debugger debugger) { + mDebugger.set(debugger); + } + public boolean isRunning() { return mUpdateLoop != null && mUpdateLoop.isRunning(); } @@ -104,28 +141,36 @@ public boolean isPaused() { @Override public void update(long elapsedMillis) { processInput(); - int size = mUpdatables.size(); - for (int i = 0; i < size; i++) { - Updatable u = mUpdatables.get(i); - if (u.isActive() && u.isRunning()) { - u.update(elapsedMillis); + synchronized (mUpdatables) { + int updatableCount = mUpdatables.size(); + for (int i = 0; i < updatableCount; i++) { + Updatable u = mUpdatables.get(i); + if (u.isActive() && u.isRunning()) { + u.update(elapsedMillis); + } } } - focusCamera(); - checkCollision(); + mCamera.update(elapsedMillis); + int listenerCount = mListeners.size(); + for (int i = 0; i < listenerCount; i++) { + EngineListener listener = mListeners.get(i); + listener.onEngineUpdate(elapsedMillis); + } synchronized (mDrawables) { while (!mUpdatablesToRemove.isEmpty()) { - removeFromEngine(mUpdatablesToRemove.remove(0)); + removeUpdatable(mUpdatablesToRemove.remove(0)); } while (!mUpdatablesToAdd.isEmpty()) { - addToEngine(mUpdatablesToAdd.remove(0)); + addUpdatable(mUpdatablesToAdd.remove(0)); } } } @Override public void draw() { - mGameView.draw(); + if (mGameView != null) { + mGameView.draw(); + } } @Override @@ -133,14 +178,19 @@ public void onDraw(Canvas canvas) { synchronized (mDrawables) { // Sort drawables by layer Collections.sort(mDrawables, mLayerComparator); - int size = mDrawables.size(); - for (int i = 0; i < size; i++) { + int drawableCount = mDrawables.size(); + for (int i = 0; i < drawableCount; i++) { Drawable d = mDrawables.get(i); - if (d.isVisible() && !d.isCulling(mCamera)) { + if (d.isVisible() && !d.isCulling(canvas, mCamera)) { d.draw(canvas, mCamera); } } } + int listenerCount = mListeners.size(); + for (int i = 0; i < listenerCount; i++) { + EngineListener listener = mListeners.get(i); + listener.onEngineDraw(canvas, mCamera); + } } //======================================================== @@ -148,24 +198,14 @@ public void onDraw(Canvas canvas) { // Methods //-------------------------------------------------------- public void startGame() { - // Stop the engine if it is running - stopGame(); - - // Init the default Camera + if (mGameView == null) { + throw new EngineRuntimeException("GameView not found!"); + } if (mCamera == null) { - mCamera = new Camera(mGameView.getWidth(), mGameView.getHeight(), - mGameView.getWidth(), mGameView.getHeight()); + throw new EngineRuntimeException("Camera not found!"); } - // Init the screen area - mCamera.init(mGameView.getWidth(), mGameView.getHeight()); - // Init the collision area to world width and height - mQuadTree.init(mCamera.getWorldWidth(), mCamera.getWorldHeight()); - - // Init the default TouchController - if (mTouchController == null) { - mTouchController = new SingleTouchController(mGameView); - } + mSceneController.start(); // Start the input controller if (mTouchController != null) { @@ -188,6 +228,7 @@ public void startGame() { } public void stopGame() { + mSceneController.stop(); if (mUpdateLoop != null) { mUpdateLoop.stopLoop(); mUpdateLoop = null; @@ -198,16 +239,20 @@ public void stopGame() { } if (mTouchController != null) { mTouchController.stop(); + mTouchController = null; } if (mAccelerationController != null) { mAccelerationController.stop(); + mAccelerationController = null; } if (mOrientationController != null) { mOrientationController.stop(); + mOrientationController = null; } } public void pauseGame() { + mSceneController.pause(); if (mUpdateLoop != null) { mUpdateLoop.pauseLoop(); } @@ -226,6 +271,7 @@ public void pauseGame() { } public void resumeGame() { + mSceneController.resume(); if (mUpdateLoop != null) { mUpdateLoop.resumeLoop(); } @@ -243,104 +289,110 @@ public void resumeGame() { } } - public void disposeGame() { - int size = mUpdatables.size(); - for (int i = 0; i < size; i++) { - Updatable u = mUpdatables.get(i); - if (u instanceof Disposable) { - Disposable d = (Disposable) u; - if (!d.isDisposed()) { - d.dispose(); + public void releaseGame() { + synchronized (mUpdatables) { + int updatableCount = mUpdatables.size(); + for (int i = updatableCount - 1; i >= 0; i--) { + Updatable u = mUpdatables.get(i); + u.removeFromGame(); + if (u instanceof Releasable) { + Releasable r = (Releasable) u; + if (!r.isRelease()) { + r.release(); + } } } } } private void processInput() { + if (mTouchController == null) { + return; + } List touchEvents = mTouchController.getTouchEvents(); if (touchEvents.isEmpty()) { return; } - // We notify all the TouchEventListener - int size = mUpdatables.size(); - for (int i = 0; i < size; i++) { + // We notify all the listener + int updatableCount = mUpdatables.size(); + for (int i = 0; i < updatableCount; i++) { Updatable u = mUpdatables.get(i); + + // Check is TouchEventListener if (u instanceof TouchEventListener) { TouchEventListener listener = ((TouchEventListener) u); // Consume TouchEvent - int sizeEvent = touchEvents.size(); - for (int j = 0; j < sizeEvent; j++) { + int touchEventCount = touchEvents.size(); + for (int j = 0; j < touchEventCount; j++) { TouchEvent event = touchEvents.get(j); // Transform screen coordinate to world coordinate listener.onTouchEvent(event.getType(), - mCamera.getScreenToWorldX(event.getTouchX()), - mCamera.getScreenToWorldY(event.getTouchY())); + mCamera.getScreenToWorldX(event.getTouchX(), listener.getCoordinateType()), + mCamera.getScreenToWorldY(event.getTouchY(), listener.getCoordinateType())); } } - } - } - private void focusCamera() { - // Check is camera has focus - if (!mCamera.isFocus()) { - return; - } - float offsetX = mCamera.getOffsetX(); - float offsetY = mCamera.getOffsetY(); - - int size = mUpdatables.size(); - for (int i = 0; i < size; i++) { - Updatable u = mUpdatables.get(i); - if (u instanceof CameraListener) { - CameraListener listener = ((CameraListener) u); - listener.setCameraOffset(offsetX, offsetY); + // Check is BoundTouchEventListener + if (u instanceof BoundTouchEventListener) { + BoundTouchEventListener listener = ((BoundTouchEventListener) u); + // Consume TouchEvent + int touchEventCount = touchEvents.size(); + for (int j = 0; j < touchEventCount; j++) { + TouchEvent event = touchEvents.get(j); + // Transform screen coordinate to world coordinate + float touchX = mCamera.getScreenToWorldX(event.getTouchX(), listener.getCoordinateType()); + float touchY = mCamera.getScreenToWorldY(event.getTouchY(), listener.getCoordinateType()); + // Check is in bounds + if (touchX > listener.getX() && touchX < listener.getEndX() + && touchY > listener.getY() && touchY < listener.getEndY()) { + listener.onAreaTouchEvent(event.getType(), touchX - listener.getX(), + touchY - listener.getY()); + } + } } } } - private void checkCollision() { - mQuadTree.checkCollision(this); - } - - private void addToEngine(Updatable updatable) { + private void addUpdatable(Updatable updatable) { mUpdatables.add(updatable); if (updatable instanceof Drawable) { mDrawables.add((Drawable) updatable); } - if (updatable instanceof Collidable) { - Collidable c = (Collidable) updatable; - if (c.getCollisionHitBox() != null) { - mQuadTree.addCollidable(c); - } + int listenerCount = mListeners.size(); + for (int i = 0; i < listenerCount; i++) { + EngineListener listener = mListeners.get(i); + listener.onAddToEngine(updatable); } } - private void removeFromEngine(Updatable updatable) { + private void removeUpdatable(Updatable updatable) { mUpdatables.remove(updatable); if (updatable instanceof Drawable) { mDrawables.remove((Drawable) updatable); } - if (updatable instanceof Collidable) { - mQuadTree.removeCollidable((Collidable) updatable); + int listenerCount = mListeners.size(); + for (int i = 0; i < listenerCount; i++) { + EngineListener listener = mListeners.get(i); + listener.onRemoveFromEngine(updatable); } } - public void addUpdatable(Updatable updatable) { + public void addToEngine(Updatable updatable) { if (updatable.isRunning()) { - throw new IllegalStateException("'" + updatable.getClass().getSimpleName() + "' is already in the engine!"); + throw new EngineRuntimeException("'" + updatable.getName() + "' is already in the engine!"); } updatable.setRunning(true); // Add to buffer if engine is running if (isRunning()) { mUpdatablesToAdd.add(updatable); } else { - addToEngine(updatable); + addUpdatable(updatable); } } - public void removeUpdatable(Updatable updatable) { + public void removeFromEngine(Updatable updatable) { if (!updatable.isRunning()) { - throw new IllegalStateException("'" + updatable.getClass().getSimpleName() + "' is not in the engine!"); + throw new EngineRuntimeException("'" + updatable.getName() + "' is not in the engine!"); } updatable.setRunning(false); // Add to buffer if engine is running @@ -351,15 +403,53 @@ public void removeUpdatable(Updatable updatable) { mUpdatablesToRemove.add(updatable); } } else { - removeFromEngine(updatable); + removeUpdatable(updatable); } } + public void addToScene(Updatable updatable) { + addToEngine(updatable); + Scene scene = mSceneController.getCurrentScene(); + if (scene == null) { + throw new EngineRuntimeException("Scene not found!"); + } + if (scene.getAllChild().contains(updatable)) { + throw new EngineRuntimeException("'" + updatable.getName() + + "' is already in the scene '" + scene.getName() + "'!"); + } + scene.addToScene(updatable); + } + + public void removeFromScene(Updatable updatable) { + removeFromEngine(updatable); + Scene scene = mSceneController.getCurrentScene(); + if (scene == null) { + throw new EngineRuntimeException("Scene not found!"); + } + if (!scene.getAllChild().contains(updatable)) { + throw new EngineRuntimeException("'" + updatable.getName() + + "' is not in the scene '" + scene.getName() + "'!"); + } + scene.removeFromScene(updatable); + } + + public void addListener(EngineListener listener) { + mListeners.add(listener); + } + + public void removeListener(EngineListener listener) { + mListeners.remove(listener); + } + + public void clearListener() { + mListeners.clear(); + } + public void dispatchEvent(Event event) { synchronized (mDrawables) { // We notify all the EventListener - int size = mUpdatables.size(); - for (int i = 0; i < size; i++) { + int updatableCount = mUpdatables.size(); + for (int i = 0; i < updatableCount; i++) { Updatable u = mUpdatables.get(i); if (u instanceof EventListener) { EventListener listener = ((EventListener) u); diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/EngineListener.java b/nattyengine/src/main/java/com/nativegame/nattyengine/engine/EngineListener.java new file mode 100644 index 0000000..5b36c45 --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/engine/EngineListener.java @@ -0,0 +1,22 @@ +package com.nativegame.nattyengine.engine; + +import android.graphics.Canvas; + +import com.nativegame.nattyengine.camera.Camera; +import com.nativegame.nattyengine.entity.Updatable; + +public interface EngineListener { + + default void onEngineUpdate(long elapsedMillis) { + } + + default void onEngineDraw(Canvas canvas, Camera camera) { + } + + default void onAddToEngine(Updatable updatable) { + } + + default void onRemoveFromEngine(Updatable updatable) { + } + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/camera/Camera.java b/nattyengine/src/main/java/com/nativegame/nattyengine/engine/camera/Camera.java deleted file mode 100644 index c7bc2b4..0000000 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/camera/Camera.java +++ /dev/null @@ -1,143 +0,0 @@ -package com.nativegame.nattyengine.engine.camera; - -/** - * Created by Oscar Liang on 2022/12/11 - */ - -public class Camera { - - private final int mCameraWidth; - private final int mCameraHeight; - private final int mWorldWidth; - private final int mWorldHeight; - - private final int mScreenCenterX; - private final int mScreenCenterY; - private final int mWorldCenterX; - private final int mWorldCenterY; - private final float mPixelFactor; - - private int mScreenWidth; - private int mScreenHeight; - private float mOffsetX; - private float mOffsetY; - private float mZoom = 1.0f; - - private boolean mIsFocus = false; - - //-------------------------------------------------------- - // Constructors - //-------------------------------------------------------- - public Camera(int cameraWidth, int cameraHeight, int worldWidth, int worldHeight) { - this(cameraWidth / 2, cameraHeight / 2, cameraWidth, cameraHeight, worldWidth, worldHeight); - } - - public Camera(int centerX, int centerY, int cameraWidth, int cameraHeight, int worldWidth, int worldHeight) { - mCameraWidth = cameraWidth; - mCameraHeight = cameraHeight; - mWorldWidth = worldWidth; - mWorldHeight = worldHeight; - - mScreenCenterX = centerX; - mScreenCenterY = centerY; - mWorldCenterX = worldWidth / 2; - mWorldCenterY = worldHeight / 2; - - mPixelFactor = cameraWidth * 1f / worldWidth; - } - //======================================================== - - //-------------------------------------------------------- - // Getter and Setter - //-------------------------------------------------------- - public int getScreenWidth() { - return mScreenWidth; - } - - public int getScreenHeight() { - return mScreenHeight; - } - - public float getCameraWidth() { - return mCameraWidth * mZoom; - } - - public float getCameraHeight() { - return mCameraHeight * mZoom; - } - - public int getWorldWidth() { - return mWorldWidth; - } - - public int getWorldHeight() { - return mWorldHeight; - } - - public float getPixelFactor() { - return mPixelFactor * mZoom; - } - - public float getOffsetX() { - return mOffsetX; - } - - public void setOffsetX(float offsetX) { - mOffsetX = offsetX; - } - - public float getOffsetY() { - return mOffsetY; - } - - public void setOffsetY(float offsetY) { - mOffsetY = offsetY; - } - - public float getZoom() { - return mZoom; - } - - public void setZoom(float zoom) { - mZoom = zoom; - } - - public boolean isFocus() { - return mIsFocus; - } - - public void setFocus(boolean focus) { - mIsFocus = focus; - } - //======================================================== - - //-------------------------------------------------------- - // Methods - //-------------------------------------------------------- - public void init(int screenWidth, int screenHeight) { - mScreenWidth = screenWidth; - mScreenHeight = screenHeight; - } - - public float getWorldToScreenX(float worldX) { - return mScreenCenterX - (mWorldCenterX - worldX) * getPixelFactor(); - } - - public float getWorldToScreenY(float worldY) { - return mScreenCenterY - (mWorldCenterY - worldY) * getPixelFactor(); - } - - public float getScreenToWorldX(float screenX) { - // Margin between screen and camera - float marginX = mScreenCenterX - getCameraWidth() / 2f; - return (screenX - marginX) / getPixelFactor(); - } - - public float getScreenToWorldY(float screenY) { - // Margin between screen and camera - float marginY = mScreenCenterY - getCameraHeight() / 2f; - return (screenY - marginY) / getPixelFactor(); - } - //======================================================== - -} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/camera/CameraListener.java b/nattyengine/src/main/java/com/nativegame/nattyengine/engine/camera/CameraListener.java deleted file mode 100644 index bfd54ed..0000000 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/camera/CameraListener.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.nativegame.nattyengine.engine.camera; - -/** - * Created by Oscar Liang on 2022/12/11 - */ - -public interface CameraListener { - - void setCameraOffset(float offsetX, float offsetY); - -} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/Collidable.java b/nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/Collidable.java deleted file mode 100644 index dc9d9be..0000000 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/Collidable.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.nativegame.nattyengine.engine.collision; - -import com.nativegame.nattyengine.engine.collision.hitbox.HitBox; - -/** - * Created by Oscar Liang on 2022/12/11 - */ - -public interface Collidable { - - HitBox getCollisionHitBox(); - - void setCollisionHitBox(HitBox hitBox); - - CollisionType getCollisionType(); - - void setCollisionType(CollisionType collisionType); - - void collide(Collidable collidable); - -} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/CollisionType.java b/nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/CollisionType.java deleted file mode 100644 index 840aee9..0000000 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/CollisionType.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.nativegame.nattyengine.engine.collision; - -/** - * Created by Oscar Liang on 2022/12/11 - */ - -public enum CollisionType { - NONE, - ACTIVE, - PASSIVE -} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/algorithm/Collision.java b/nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/algorithm/Collision.java deleted file mode 100644 index 12fa70a..0000000 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/algorithm/Collision.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.nativegame.nattyengine.engine.collision.algorithm; - -import com.nativegame.nattyengine.engine.collision.Collidable; - -import java.util.ArrayList; -import java.util.List; - -/** - * Created by Oscar Liang on 2022/12/11 - */ - -public class Collision { - - private static final List COLLISION_POOL = new ArrayList<>(); - - private Collidable mCollidableA; - private Collidable mCollidableB; - - //-------------------------------------------------------- - // Constructors - //-------------------------------------------------------- - private Collision(Collidable collidableA, Collidable collidableB) { - mCollidableA = collidableA; - mCollidableB = collidableB; - } - //======================================================== - - //-------------------------------------------------------- - // Static methods - //-------------------------------------------------------- - public static Collision initCollision(Collidable collidableA, Collidable collidableB) { - if (COLLISION_POOL.isEmpty()) { - return new Collision(collidableA, collidableB); - } - Collision c = COLLISION_POOL.remove(0); - c.mCollidableA = collidableA; - c.mCollidableB = collidableB; - return c; - } - - public static void returnCollision(Collision collision) { - collision.mCollidableA = null; - collision.mCollidableB = null; - COLLISION_POOL.add(collision); - } - //======================================================== - - //-------------------------------------------------------- - // Methods - //-------------------------------------------------------- - public boolean equals(Collision collision) { - return (mCollidableA == collision.mCollidableA && mCollidableB == collision.mCollidableB) - || (mCollidableA == collision.mCollidableB && mCollidableB == collision.mCollidableA); - } - //======================================================== - -} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/algorithm/CollisionAlgorithm.java b/nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/algorithm/CollisionAlgorithm.java deleted file mode 100644 index 2192abe..0000000 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/algorithm/CollisionAlgorithm.java +++ /dev/null @@ -1,101 +0,0 @@ -package com.nativegame.nattyengine.engine.collision.algorithm; - -import android.graphics.Bitmap; -import android.graphics.Color; -import android.graphics.Rect; - -import com.nativegame.nattyengine.engine.collision.Collidable; -import com.nativegame.nattyengine.engine.collision.CollisionType; - -/** - * Created by Oscar Liang on 2022/12/11 - */ - -public class CollisionAlgorithm { - - private static final Rect RECT = new Rect(); - - //-------------------------------------------------------- - // Constructors - //-------------------------------------------------------- - private CollisionAlgorithm() { - } - //======================================================== - - //-------------------------------------------------------- - // Static methods - //-------------------------------------------------------- - public static boolean isCollisionsDetected(Collidable collidableA, Collidable collidableB) { - // We skip the passive collision type - if (collidableA.getCollisionType() == CollisionType.PASSIVE - && collidableB.getCollisionType() == CollisionType.PASSIVE) { - return false; - } - - // Get the HitBox bounds - Rect boundsA = collidableA.getCollisionHitBox().getCollisionBounds(); - Rect boundsB = collidableB.getCollisionHitBox().getCollisionBounds(); - - // Check is two bounds intersect - if (Rect.intersects(boundsA, boundsB)) { - - // Get the collision bounds - Rect collisionBounds = getCollisionBounds(boundsA, boundsB); - for (int i = collisionBounds.left; i < collisionBounds.right; i += 10) { - for (int j = collisionBounds.top; j < collisionBounds.bottom; j += 10) { - - // Get the pixel color from bitmap - int collidableAPixel = getBitmapPixel(collidableA.getCollisionHitBox().getCollisionBitmap(), - i - boundsA.left, - j - boundsA.top); - int collidableBPixel = getBitmapPixel(collidableB.getCollisionHitBox().getCollisionBitmap(), - i - boundsB.left, - j - boundsB.top); - if (isFilled(collidableAPixel) && isFilled(collidableBPixel)) { - // Collision detected if both pixel colors are filled - return true; - } - } - } - } - - return false; - } - - private static Rect getCollisionBounds(Rect rectA, Rect rectB) { - int left = Math.max(rectA.left, rectB.left); - int top = Math.max(rectA.top, rectB.top); - int right = Math.min(rectA.right, rectB.right); - int bottom = Math.min(rectA.bottom, rectB.bottom); - RECT.set(left, top, right, bottom); - return RECT; - } - - private static int getBitmapPixel(Bitmap bitmap, int i, int j) { - // We make sure the value >= 0 - if (i < 0) { - i = 0; - } - if (j < 0) { - j = 0; - } - - // We make sure the value < bitmap width - int maxX = bitmap.getWidth() - 1; - if (i > maxX) { - i = maxX; - } - int maxY = bitmap.getHeight() - 1; - if (j > maxY) { - j = maxY; - } - - return bitmap.getPixel(i, j); - } - - private static boolean isFilled(int pixel) { - return pixel != Color.TRANSPARENT; - } - //======================================================== - -} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/algorithm/QuadTree.java b/nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/algorithm/QuadTree.java deleted file mode 100644 index 4ed7979..0000000 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/algorithm/QuadTree.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.nativegame.nattyengine.engine.collision.algorithm; - -import android.graphics.Rect; - -import com.nativegame.nattyengine.engine.Engine; -import com.nativegame.nattyengine.engine.collision.Collidable; - -import java.util.ArrayList; -import java.util.List; - -/** - * Created by Oscar Liang on 2022/12/11 - */ - -public class QuadTree { - - private static final int MAX_NODE = 12; - - private final QuadTreeNode mRoot; - - private final List mNodePool = new ArrayList<>(); - private final List mDetectedCollisions = new ArrayList<>(); - - //-------------------------------------------------------- - // Constructors - //-------------------------------------------------------- - public QuadTree() { - mRoot = new QuadTreeNode(this); - // We add them to the pool now - for (int i = 0; i < MAX_NODE; i++) { - mNodePool.add(new QuadTreeNode(this)); - } - } - //======================================================== - - //-------------------------------------------------------- - // Getter and Setter - //-------------------------------------------------------- - public int getPoolSize() { - return mNodePool.size(); - } - //======================================================== - - //-------------------------------------------------------- - // Methods - //-------------------------------------------------------- - public void init(int width, int height) { - mRoot.setArea(new Rect(0, 0, width, height)); - } - - public void checkCollision(Engine engine) { - // Clear the collisions from the previous loop - while (!mDetectedCollisions.isEmpty()) { - Collision.returnCollision(mDetectedCollisions.remove(0)); - } - mRoot.checkCollision(engine, mDetectedCollisions); - } - - public void addCollidable(Collidable collidable) { - mRoot.getCollidables().add(collidable); - } - - public void removeCollidable(Collidable collidable) { - mRoot.getCollidables().remove(collidable); - } - - public QuadTreeNode obtainNode() { - return mNodePool.remove(0); - } - - public void returnNode(QuadTreeNode treeNode) { - mNodePool.add(treeNode); - } - //======================================================== - -} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/algorithm/QuadTreeNode.java b/nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/algorithm/QuadTreeNode.java deleted file mode 100644 index e952857..0000000 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/algorithm/QuadTreeNode.java +++ /dev/null @@ -1,134 +0,0 @@ -package com.nativegame.nattyengine.engine.collision.algorithm; - -import android.graphics.Rect; - -import com.nativegame.nattyengine.engine.Engine; -import com.nativegame.nattyengine.engine.collision.Collidable; -import com.nativegame.nattyengine.engine.collision.CollisionType; - -import java.util.ArrayList; -import java.util.List; - -/** - * Created by Oscar Liang on 2022/12/11 - */ - -public class QuadTreeNode { - - private static final int MAX_OBJECTS_TO_CHECK = 8; - - private final QuadTree mParent; - - private final Rect mArea = new Rect(); - - private final List mCollidables = new ArrayList<>(); - private final List mChildren = new ArrayList<>(4); - - //-------------------------------------------------------- - // Constructors - //-------------------------------------------------------- - public QuadTreeNode(QuadTree quadTree) { - mParent = quadTree; - } - //======================================================== - - //-------------------------------------------------------- - // Getter and Setter - //-------------------------------------------------------- - public List getCollidables() { - return mCollidables; - } - - public void setArea(Rect area) { - mArea.set(area); - } - //======================================================== - - //-------------------------------------------------------- - // Methods - //-------------------------------------------------------- - public void checkCollision(Engine engine, List detectedCollisions) { - int size = mCollidables.size(); - if (size > MAX_OBJECTS_TO_CHECK && mParent.getPoolSize() >= 4) { - // Divide the area in 4 part - divideAndCheck(engine, detectedCollisions); - } else { - for (int i = 0; i < size; i++) { - Collidable collidableA = mCollidables.get(i); - for (int j = i + 1; j < size; j++) { - Collidable collidableB = mCollidables.get(j); - if (CollisionAlgorithm.isCollisionsDetected(collidableA, collidableB)) { - Collision c = Collision.initCollision(collidableA, collidableB); - if (!hasBeenDetected(c, detectedCollisions)) { - detectedCollisions.add(c); - collidableA.collide(collidableB); - collidableB.collide(collidableA); - } - } - } - } - } - } - - private void divideAndCheck(Engine engine, List detectedCollisions) { - mChildren.clear(); - // Add 4 children - for (int i = 0; i < 4; i++) { - mChildren.add(mParent.obtainNode()); - } - // Check children collision - for (int i = 0; i < 4; i++) { - QuadTreeNode node = mChildren.get(i); - node.setArea(getChildArea(i)); - node.initChildArea(mCollidables); - node.checkCollision(engine, detectedCollisions); - // Clear and return to the pool - node.getCollidables().clear(); - mParent.returnNode(node); - } - } - - private Rect getChildArea(int area) { - int startX = mArea.left; - int startY = mArea.top; - int width = mArea.width(); - int height = mArea.height(); - switch (area) { - case 0: - return new Rect(startX, startY, startX + width / 2, startY + height / 2); - case 1: - return new Rect(startX + width / 2, startY, startX + width, startY + height / 2); - case 2: - return new Rect(startX, startY + height / 2, startX + width / 2, startY + height); - case 3: - return new Rect(startX + width / 2, startY + height / 2, startX + width, startY + height); - default: - throw new IllegalArgumentException("Collision area not found!"); - } - } - - private void initChildArea(List collidables) { - mCollidables.clear(); - int size = collidables.size(); - for (int i = 0; i < size; i++) { - Collidable c = collidables.get(i); - if (c.getCollisionType() != CollisionType.NONE - && Rect.intersects(c.getCollisionHitBox().getCollisionBounds(), mArea)) { - mCollidables.add(c); - } - } - } - - private boolean hasBeenDetected(Collision collision, List detectedCollisions) { - int size = detectedCollisions.size(); - for (int i = 0; i < size; i++) { - Collision c = detectedCollisions.get(i); - if (c.equals(collision)) { - return true; - } - } - return false; - } - //======================================================== - -} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/hitbox/BaseHitBox.java b/nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/hitbox/BaseHitBox.java deleted file mode 100644 index 7e2fa2b..0000000 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/hitbox/BaseHitBox.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.nativegame.nattyengine.engine.collision.hitbox; - -import android.graphics.Rect; - -/** - * Created by Oscar Liang on 2022/12/11 - */ - -public abstract class BaseHitBox implements HitBox { - - private final int mHitBoxWidth; - private final int mHitBoxHeight; - - private final Rect mBounds = new Rect(-1, -1, -1, -1); - - //-------------------------------------------------------- - // Constructors - //-------------------------------------------------------- - protected BaseHitBox(int hitBoxWidth, int hitBoxHeight) { - mHitBoxWidth = hitBoxWidth; - mHitBoxHeight = hitBoxHeight; - } - //======================================================== - - //-------------------------------------------------------- - // Overriding methods - //-------------------------------------------------------- - @Override - public Rect getCollisionBounds() { - return mBounds; - } - - @Override - public void setCollisionBoundsPosition(int x, int y) { - // Align to center - mBounds.set(x - mHitBoxWidth / 2, - y - mHitBoxHeight / 2, - x + mHitBoxWidth / 2, - y + mHitBoxHeight / 2); - } - //======================================================== - -} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/hitbox/HitBox.java b/nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/hitbox/HitBox.java deleted file mode 100644 index ffe9500..0000000 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/hitbox/HitBox.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.nativegame.nattyengine.engine.collision.hitbox; - -import android.graphics.Bitmap; -import android.graphics.Rect; - -/** - * Created by Oscar Liang on 2022/12/11 - */ - -public interface HitBox { - - Bitmap getCollisionBitmap(); - - Rect getCollisionBounds(); - - void setCollisionBoundsPosition(int x, int y); - -} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/hitbox/RectangleHitBox.java b/nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/hitbox/RectangleHitBox.java deleted file mode 100644 index 330f9c5..0000000 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/hitbox/RectangleHitBox.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.nativegame.nattyengine.engine.collision.hitbox; - -import android.graphics.Bitmap; - -import com.nativegame.nattyengine.util.bitmap.BitmapUtils; - -/** - * Created by Oscar Liang on 2022/12/11 - */ - -public class RectangleHitBox extends BaseHitBox { - - private final Bitmap mHitBoxBitmap; - - //-------------------------------------------------------- - // Constructors - //-------------------------------------------------------- - public RectangleHitBox(int hitBoxWidth, int hitBoxHeight) { - this(hitBoxWidth, hitBoxHeight, 0, 0); - } - - public RectangleHitBox(int hitBoxWidth, int hitBoxHeight, int radius) { - this(hitBoxWidth, hitBoxHeight, radius, radius); - } - - public RectangleHitBox(int hitBoxWidth, int hitBoxHeight, int radiusX, int radiusY) { - super(hitBoxWidth, hitBoxHeight); - mHitBoxBitmap = BitmapUtils.createRectangleBitmap(hitBoxWidth, hitBoxHeight, radiusX, radiusY); - } - //======================================================== - - //-------------------------------------------------------- - // Overriding methods - //-------------------------------------------------------- - @Override - public Bitmap getCollisionBitmap() { - return mHitBoxBitmap; - } - //======================================================== - -} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/hitbox/SpriteHitBox.java b/nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/hitbox/SpriteHitBox.java deleted file mode 100644 index 1a648d2..0000000 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/hitbox/SpriteHitBox.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.nativegame.nattyengine.engine.collision.hitbox; - -import android.graphics.Bitmap; - -import com.nativegame.nattyengine.util.bitmap.BitmapUtils; - -/** - * Created by Oscar Liang on 2022/12/11 - */ - -public class SpriteHitBox extends BaseHitBox { - - private final Bitmap mHitBoxBitmap; - - //-------------------------------------------------------- - // Constructors - //-------------------------------------------------------- - public SpriteHitBox(Bitmap bitmap, int hitBoxWidth, int hitBoxHeight) { - super(hitBoxWidth, hitBoxHeight); - mHitBoxBitmap = BitmapUtils.createBitmap(bitmap, hitBoxWidth, hitBoxHeight); - } - //======================================================== - - //-------------------------------------------------------- - // Overriding methods - //-------------------------------------------------------- - @Override - public Bitmap getCollisionBitmap() { - return mHitBoxBitmap; - } - //======================================================== - -} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/loop/BaseLoop.java b/nattyengine/src/main/java/com/nativegame/nattyengine/engine/loop/BaseLoop.java index f5e5c6a..bfc7376 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/loop/BaseLoop.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/engine/loop/BaseLoop.java @@ -41,7 +41,7 @@ public void run() { } currentTimeMillis = System.currentTimeMillis(); } - updateLoop(elapsedTimeMillis); + onUpdateLoop(elapsedTimeMillis); previousTimeMillis = currentTimeMillis; } } @@ -85,4 +85,10 @@ public boolean isPaused() { } //======================================================== + //-------------------------------------------------------- + // Methods + //-------------------------------------------------------- + protected abstract void onUpdateLoop(long elapsedMillis); + //======================================================== + } diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/loop/DrawLoop.java b/nattyengine/src/main/java/com/nativegame/nattyengine/engine/loop/DrawLoop.java index 421d704..c5c686b 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/loop/DrawLoop.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/engine/loop/DrawLoop.java @@ -34,7 +34,7 @@ public void setListener(DrawListener listener) { // Overriding methods //-------------------------------------------------------- @Override - public void updateLoop(long elapsedMillis) { + public void onUpdateLoop(long elapsedMillis) { if (elapsedMillis < DELTA_TIME) { try { // We make sure each loop is fixed diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/loop/Loop.java b/nattyengine/src/main/java/com/nativegame/nattyengine/engine/loop/Loop.java index 3ec818c..45b0c03 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/loop/Loop.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/engine/loop/Loop.java @@ -14,8 +14,6 @@ public interface Loop { void resumeLoop(); - void updateLoop(long elapsedMillis); - boolean isRunning(); boolean isPaused(); diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/loop/UpdateLoop.java b/nattyengine/src/main/java/com/nativegame/nattyengine/engine/loop/UpdateLoop.java index 9661eda..a53afc2 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/loop/UpdateLoop.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/engine/loop/UpdateLoop.java @@ -32,7 +32,7 @@ public void setListener(UpdateListener listener) { // Overriding methods //-------------------------------------------------------- @Override - public void updateLoop(long elapsedMillis) { + public void onUpdateLoop(long elapsedMillis) { mListener.update(elapsedMillis); } //======================================================== diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/Drawable.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/Drawable.java similarity index 65% rename from nattyengine/src/main/java/com/nativegame/nattyengine/engine/Drawable.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/entity/Drawable.java index 07d48cd..f90d228 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/Drawable.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/Drawable.java @@ -1,8 +1,8 @@ -package com.nativegame.nattyengine.engine; +package com.nativegame.nattyengine.entity; import android.graphics.Canvas; -import com.nativegame.nattyengine.engine.camera.Camera; +import com.nativegame.nattyengine.camera.Camera; /** * Created by Oscar Liang on 2022/12/11 @@ -18,8 +18,8 @@ public interface Drawable { void setVisible(boolean visible); - void draw(Canvas canvas, Camera camera); + boolean isCulling(Canvas canvas, Camera camera); - boolean isCulling(Camera camera); + void draw(Canvas canvas, Camera camera); } diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/Entity.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/Entity.java index 6e3d497..e25ddf0 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/Entity.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/Entity.java @@ -1,21 +1,19 @@ package com.nativegame.nattyengine.entity; -import com.nativegame.nattyengine.engine.Disposable; import com.nativegame.nattyengine.engine.Engine; -import com.nativegame.nattyengine.engine.Updatable; -import com.nativegame.nattyengine.engine.event.Event; +import com.nativegame.nattyengine.event.Event; /** * Created by Oscar Liang on 2022/12/11 */ -public abstract class Entity implements Updatable, Disposable { +public abstract class Entity implements Updatable, Releasable { protected final Engine mEngine; private boolean mIsRunning = false; private boolean mIsActive = true; - private boolean mIsDisposed = false; + private boolean mIsRelease = false; //-------------------------------------------------------- // Constructors @@ -50,13 +48,25 @@ public void setActive(boolean active) { @Override public void addToGame() { - mEngine.addUpdatable(this); + mEngine.addToEngine(this); onStart(); } @Override public void removeFromGame() { - mEngine.removeUpdatable(this); + mEngine.removeFromEngine(this); + onRemove(); + } + + @Override + public void addToScene() { + mEngine.addToScene(this); + onStart(); + } + + @Override + public void removeFromScene() { + mEngine.removeFromScene(this); onRemove(); } @@ -73,32 +83,41 @@ public void reset() { } @Override - public void dispose() { - mIsDisposed = true; + public void release() { + mIsRelease = true; + onRelease(); + } + + @Override + public boolean isRelease() { + return mIsRelease; } @Override - public boolean isDisposed() { - return mIsDisposed; + public String getName() { + return getClass().getSimpleName(); } //======================================================== //-------------------------------------------------------- // Methods //-------------------------------------------------------- - public void onStart() { + protected void onStart() { + } + + protected void onRemove() { } - public void onRemove() { + protected void onRelease() { } - public void onPreUpdate(long elapsedMillis) { + protected void onPreUpdate(long elapsedMillis) { } - public void onUpdate(long elapsedMillis) { + protected void onUpdate(long elapsedMillis) { } - public void onPostUpdate(long elapsedMillis) { + protected void onPostUpdate(long elapsedMillis) { } public void dispatchEvent(Event event) { diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/Manipulable.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/Manipulable.java new file mode 100644 index 0000000..76a4c95 --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/Manipulable.java @@ -0,0 +1,57 @@ +package com.nativegame.nattyengine.entity; + +import com.nativegame.nattyengine.camera.Camera; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public interface Manipulable { + + Camera.CoordinateType getCoordinateType(); + + void setCoordinateType(Camera.CoordinateType type); + + float getX(); + + void setX(float x); + + float getY(); + + void setY(float y); + + float getScaleX(); + + void setScaleX(float scaleX); + + float getScaleY(); + + void setScaleY(float scaleY); + + void setScale(float scale); + + float getRotation(); + + void setRotation(float rotation); + + int getAlpha(); + + void setAlpha(int alpha); + + float getRotationPivotX(); + + void setRotationPivotX(float rotationPivotX); + + float getRotationPivotY(); + + void setRotationPivotY(float rotationPivotY); + + float getScalePivotX(); + + void setScalePivotX(float scalePivotX); + + float getScalePivotY(); + + void setScalePivotY(float scalePivotY); + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/Releasable.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/Releasable.java new file mode 100644 index 0000000..79dd776 --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/Releasable.java @@ -0,0 +1,13 @@ +package com.nativegame.nattyengine.entity; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public interface Releasable { + + void release(); + + boolean isRelease(); + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/Reusable.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/Reusable.java new file mode 100644 index 0000000..5ca60b3 --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/Reusable.java @@ -0,0 +1,19 @@ +package com.nativegame.nattyengine.entity; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public interface Reusable { + + RecycleListener getRecycleListener(); + + void setRecycleListener(RecycleListener listener); + + public interface RecycleListener { + + void recycle(T object); + + } + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/ScreenEntity.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/ScreenEntity.java index d0bf1a3..e5d9ad2 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/ScreenEntity.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/ScreenEntity.java @@ -2,52 +2,23 @@ import android.graphics.Canvas; -import com.nativegame.nattyengine.engine.Drawable; import com.nativegame.nattyengine.engine.Engine; -import com.nativegame.nattyengine.engine.camera.Camera; -import com.nativegame.nattyengine.engine.camera.CameraListener; +import com.nativegame.nattyengine.camera.Camera; /** * Created by Oscar Liang on 2022/12/11 */ -public abstract class ScreenEntity extends Entity implements Drawable, CameraListener { +public abstract class ScreenEntity extends Entity implements Drawable { - protected final float mPixelFactor; - - protected float mX; - protected float mY; protected int mLayer; private boolean mIsVisible = true; //-------------------------------------------------------- // Constructors //-------------------------------------------------------- - protected ScreenEntity(Engine engine, float x, float y) { + protected ScreenEntity(Engine engine) { super(engine); - mPixelFactor = engine.getCamera().getPixelFactor(); - mX = x; - mY = y; - } - //======================================================== - - //-------------------------------------------------------- - // Getter and Setter - //-------------------------------------------------------- - public float getX() { - return mX; - } - - public void setX(float x) { - mX = x; - } - - public float getY() { - return mY; - } - - public void setY(float y) { - mY = y; } //======================================================== @@ -86,23 +57,17 @@ public void reset() { super.reset(); mIsVisible = true; } - - @Override - public void setCameraOffset(float offsetX, float offsetY) { - mX += offsetX; - mY += offsetY; - } //======================================================== //-------------------------------------------------------- // Methods //-------------------------------------------------------- - public void onPreDraw(Canvas canvas, Camera camera) { + protected void onPreDraw(Canvas canvas, Camera camera) { } - public abstract void onDraw(Canvas canvas, Camera camera); + protected abstract void onDraw(Canvas canvas, Camera camera); - public void onPostDraw(Canvas canvas, Camera camera) { + protected void onPostDraw(Canvas canvas, Camera camera) { } //======================================================== diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/Updatable.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/Updatable.java similarity index 72% rename from nattyengine/src/main/java/com/nativegame/nattyengine/engine/Updatable.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/entity/Updatable.java index cc6ace9..f9c76c7 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/Updatable.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/Updatable.java @@ -1,4 +1,4 @@ -package com.nativegame.nattyengine.engine; +package com.nativegame.nattyengine.entity; /** * Created by Oscar Liang on 2022/12/11 @@ -18,8 +18,14 @@ public interface Updatable { void removeFromGame(); + void addToScene(); + + void removeFromScene(); + void update(long elapsedMillis); void reset(); + String getName(); + } diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/counter/EntityCounter.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/counter/EntityCounter.java new file mode 100644 index 0000000..150179e --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/counter/EntityCounter.java @@ -0,0 +1,81 @@ +package com.nativegame.nattyengine.entity.counter; + +import com.nativegame.nattyengine.camera.Camera; +import com.nativegame.nattyengine.engine.Engine; +import com.nativegame.nattyengine.engine.EngineListener; +import com.nativegame.nattyengine.entity.Updatable; +import com.nativegame.nattyengine.entity.text.Text; + +import java.util.HashMap; +import java.util.Map; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public class EntityCounter extends Text implements EngineListener { + + private int mEntityCount; + + private final StringBuilder mStringBuilder = new StringBuilder(); + private final Map mEntityCountMap = new HashMap<>(); + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + public EntityCounter(Engine engine, float relativeCameraX, float relativeCameraY, int width, int height) { + super(engine, relativeCameraX, relativeCameraY, width, height, ""); + engine.addListener(this); + setCoordinateType(Camera.CoordinateType.CAMERA); + setPaint(engine.getDebugger().getDebugTextPaint()); + setTextHorizontalAlign(TextHorizontalAlign.LEFT); + setTextVerticalAlign(TextVerticalAlign.TOP); + } + //======================================================== + + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + public void onAddToEngine(Updatable updatable) { + mEntityCount++; + String name = updatable.getName(); + if (!mEntityCountMap.containsKey(name)) { + mEntityCountMap.put(name, 1); + } else { + int currentCount = mEntityCountMap.get(name); + mEntityCountMap.put(name, currentCount + 1); + } + } + + @Override + public void onRemoveFromEngine(Updatable updatable) { + mEntityCount--; + String name = updatable.getName(); + if (mEntityCountMap.containsKey(name)) { + int currentCount = mEntityCountMap.get(name) - 1; + if (currentCount == 0) { + mEntityCountMap.remove(name); + } else { + mEntityCountMap.put(name, currentCount); + } + } + } + + @Override + protected void onUpdate(long elapsedMillis) { + mStringBuilder.delete(0, mStringBuilder.length()); + mStringBuilder.append("Total Entity: ") + .append(mEntityCount) + .append("\n"); + for (Map.Entry name : mEntityCountMap.entrySet()) { + mStringBuilder.append(name.getKey()) + .append(": ") + .append(name.getValue()) + .append("\n"); + } + setText(mStringBuilder.toString()); + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/counter/FPSCounter.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/counter/FPSCounter.java new file mode 100644 index 0000000..ccb1246 --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/counter/FPSCounter.java @@ -0,0 +1,56 @@ +package com.nativegame.nattyengine.entity.counter; + +import android.graphics.Canvas; + +import com.nativegame.nattyengine.camera.Camera; +import com.nativegame.nattyengine.engine.Engine; +import com.nativegame.nattyengine.entity.text.Text; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public class FPSCounter extends Text { + + private int mDrawCount; + private long mTotalTime; + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + public FPSCounter(Engine engine, float relativeCameraX, float relativeCameraY, int width, int height) { + super(engine, relativeCameraX, relativeCameraY, width, height, ""); + setCoordinateType(Camera.CoordinateType.CAMERA); + setPaint(engine.getDebugger().getDebugTextPaint()); + setTextHorizontalAlign(TextHorizontalAlign.LEFT); + setTextVerticalAlign(TextVerticalAlign.TOP); + } + //======================================================== + + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + protected void onStart() { + mDrawCount = 0; + mTotalTime = 0; + } + + @Override + protected void onUpdate(long elapsedMillis) { + mTotalTime += elapsedMillis; + if (mTotalTime >= 1000) { + float fps = mDrawCount * 1000f / mTotalTime; + setText("FPS: " + fps); + mDrawCount = 0; + mTotalTime = mTotalTime % 1000; + } + } + + @Override + protected void onPostDraw(Canvas canvas, Camera camera) { + mDrawCount++; + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/counter/UPSCounter.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/counter/UPSCounter.java new file mode 100644 index 0000000..8e28f06 --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/counter/UPSCounter.java @@ -0,0 +1,50 @@ +package com.nativegame.nattyengine.entity.counter; + +import com.nativegame.nattyengine.camera.Camera; +import com.nativegame.nattyengine.engine.Engine; +import com.nativegame.nattyengine.entity.text.Text; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public class UPSCounter extends Text { + + private int mUpdateCount; + private long mTotalTime; + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + public UPSCounter(Engine engine, float relativeCameraX, float relativeCameraY, int width, int height) { + super(engine, relativeCameraX, relativeCameraY, width, height, ""); + setCoordinateType(Camera.CoordinateType.CAMERA); + setPaint(engine.getDebugger().getDebugTextPaint()); + setTextHorizontalAlign(TextHorizontalAlign.LEFT); + setTextVerticalAlign(TextVerticalAlign.TOP); + } + //======================================================== + + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + protected void onStart() { + mUpdateCount = 0; + mTotalTime = 0; + } + + @Override + protected void onUpdate(long elapsedMillis) { + mUpdateCount++; + mTotalTime += elapsedMillis; + if (mTotalTime >= 1000) { + float ups = mUpdateCount * 1000f / mTotalTime; + setText("UPS: " + ups); + mUpdateCount = 0; + mTotalTime = mTotalTime % 1000; + } + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/AlphaModifier.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/AlphaModifier.java similarity index 74% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/AlphaModifier.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/AlphaModifier.java index 55ca966..552723f 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/AlphaModifier.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/AlphaModifier.java @@ -1,13 +1,13 @@ -package com.nativegame.nattyengine.entity.sprite.modifier; +package com.nativegame.nattyengine.entity.modifier; -import com.nativegame.nattyengine.entity.modifier.tween.Tweener; -import com.nativegame.nattyengine.entity.sprite.Sprite; +import com.nativegame.nattyengine.entity.shape.Shape; +import com.nativegame.nattyengine.util.modifier.tween.Tweener; /** * Created by Oscar Liang on 2022/12/11 */ -public class AlphaModifier extends SingleValueSpriteModifier { +public class AlphaModifier extends SingleValueShapeModifier { //-------------------------------------------------------- // Constructors @@ -49,18 +49,18 @@ public AlphaModifier(float startValue, float endValue, long duration, long start // Overriding methods //-------------------------------------------------------- @Override - protected void onInitValue(Sprite sprite, float alpha) { - sprite.setAlpha((int) alpha); + protected void onInitValue(Shape shape, float alpha) { + shape.setAlpha((int) alpha); } @Override - protected void onUpdateValue(Sprite sprite, float alpha) { - sprite.setAlpha((int) alpha); + protected void onUpdateValue(Shape shape, float alpha) { + shape.setAlpha((int) alpha); } @Override - protected void onEndValue(Sprite sprite, float alpha) { - sprite.setAlpha((int) alpha); + protected void onEndValue(Shape shape, float alpha) { + shape.setAlpha((int) alpha); } //======================================================== diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/DoubleValueShapeModifier.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/DoubleValueShapeModifier.java new file mode 100644 index 0000000..2feaabb --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/DoubleValueShapeModifier.java @@ -0,0 +1,64 @@ +package com.nativegame.nattyengine.entity.modifier; + +import com.nativegame.nattyengine.entity.shape.Shape; +import com.nativegame.nattyengine.util.modifier.DoubleValueModifier; +import com.nativegame.nattyengine.util.modifier.tween.LinearTweener; +import com.nativegame.nattyengine.util.modifier.tween.Tweener; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public abstract class DoubleValueShapeModifier extends DoubleValueModifier { + + private boolean mIsAutoRemove = false; + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + protected DoubleValueShapeModifier(float startValueX, float endValueX, float startValueY, float endValueY, + long duration) { + super(startValueX, endValueX, startValueY, endValueY, duration, 0, LinearTweener.getInstance()); + } + + protected DoubleValueShapeModifier(float startValueX, float endValueX, float startValueY, float endValueY, + long duration, long startDelay) { + super(startValueX, endValueX, startValueY, endValueY, duration, startDelay, LinearTweener.getInstance()); + } + + protected DoubleValueShapeModifier(float startValueX, float endValueX, float startValueY, float endValueY, + long duration, Tweener tweener) { + super(startValueX, endValueX, startValueY, endValueY, duration, 0, tweener); + } + + protected DoubleValueShapeModifier(float startValueX, float endValueX, float startValueY, float endValueY, + long duration, long startDelay, Tweener tweener) { + super(startValueX, endValueX, startValueY, endValueY, duration, startDelay, tweener); + } + //======================================================== + + //-------------------------------------------------------- + // Getter and Setter + //-------------------------------------------------------- + public boolean isAutoRemove() { + return mIsAutoRemove; + } + + public void setAutoRemove(boolean autoRemove) { + mIsAutoRemove = autoRemove; + } + //======================================================== + + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + protected void onEndModifier(Shape shape) { + super.onEndModifier(shape); + if (mIsAutoRemove) { + shape.removeFromGame(); + } + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/FadeInModifier.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/FadeInModifier.java similarity index 85% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/FadeInModifier.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/FadeInModifier.java index a6acdd7..587dd2d 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/FadeInModifier.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/FadeInModifier.java @@ -1,6 +1,6 @@ -package com.nativegame.nattyengine.entity.sprite.modifier; +package com.nativegame.nattyengine.entity.modifier; -import com.nativegame.nattyengine.entity.modifier.tween.Tweener; +import com.nativegame.nattyengine.util.modifier.tween.Tweener; /** * Created by Oscar Liang on 2022/12/11 diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/FadeOutModifier.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/FadeOutModifier.java similarity index 86% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/FadeOutModifier.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/FadeOutModifier.java index 2f7657b..3e8602b 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/FadeOutModifier.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/FadeOutModifier.java @@ -1,6 +1,6 @@ -package com.nativegame.nattyengine.entity.sprite.modifier; +package com.nativegame.nattyengine.entity.modifier; -import com.nativegame.nattyengine.entity.modifier.tween.Tweener; +import com.nativegame.nattyengine.util.modifier.tween.Tweener; /** * Created by Oscar Liang on 2022/12/11 diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/PathModifier.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/PathModifier.java similarity index 65% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/PathModifier.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/PathModifier.java index f4afee1..a3c75fe 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/PathModifier.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/PathModifier.java @@ -1,21 +1,21 @@ -package com.nativegame.nattyengine.entity.sprite.modifier; +package com.nativegame.nattyengine.entity.modifier; import android.graphics.Path; import android.graphics.PathMeasure; -import com.nativegame.nattyengine.entity.modifier.BaseValueModifier; -import com.nativegame.nattyengine.entity.modifier.tween.Tweener; -import com.nativegame.nattyengine.entity.sprite.Sprite; +import com.nativegame.nattyengine.entity.shape.Shape; +import com.nativegame.nattyengine.util.modifier.BaseValueModifier; +import com.nativegame.nattyengine.util.modifier.tween.Tweener; /** * Created by Oscar Liang on 2022/12/11 */ -public class PathModifier extends BaseValueModifier { +public class PathModifier extends BaseValueModifier { private final PathMeasure mPathMeasure; - private final float[] mPathPosition = {0f, 0f}; + private final float[] mPathPosition = new float[2]; //-------------------------------------------------------- // Constructors @@ -38,25 +38,25 @@ public void setPath(Path path) { // Overriding methods //-------------------------------------------------------- @Override - protected void initValue(Sprite sprite) { + protected void initValue(Shape shape) { mPathMeasure.getPosTan(0, mPathPosition, null); - sprite.setX(mPathPosition[0]); - sprite.setY(mPathPosition[1]); + shape.setX(mPathPosition[0]); + shape.setY(mPathPosition[1]); } @Override - protected void updateValue(Sprite sprite, float percentage) { + protected void updateValue(Shape shape, float percentage) { float percentageValue = mPathMeasure.getLength() * percentage; mPathMeasure.getPosTan(percentageValue, mPathPosition, null); - sprite.setX(mPathPosition[0]); - sprite.setY(mPathPosition[1]); + shape.setX(mPathPosition[0]); + shape.setY(mPathPosition[1]); } @Override - protected void endValue(Sprite sprite) { + protected void endValue(Shape shape) { mPathMeasure.getPosTan(mPathMeasure.getLength(), mPathPosition, null); - sprite.setX(mPathPosition[0]); - sprite.setY(mPathPosition[1]); + shape.setX(mPathPosition[0]); + shape.setY(mPathPosition[1]); } //======================================================== diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/PositionModifier.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/PositionModifier.java similarity index 74% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/PositionModifier.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/PositionModifier.java index 3ef1fa0..4cab94e 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/PositionModifier.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/PositionModifier.java @@ -1,13 +1,13 @@ -package com.nativegame.nattyengine.entity.sprite.modifier; +package com.nativegame.nattyengine.entity.modifier; -import com.nativegame.nattyengine.entity.modifier.tween.Tweener; -import com.nativegame.nattyengine.entity.sprite.Sprite; +import com.nativegame.nattyengine.entity.shape.Shape; +import com.nativegame.nattyengine.util.modifier.tween.Tweener; /** * Created by Oscar Liang on 2022/12/11 */ -public class PositionModifier extends DoubleValueSpriteModifier { +public class PositionModifier extends DoubleValueShapeModifier { //-------------------------------------------------------- // Constructors @@ -49,21 +49,21 @@ public PositionModifier(float startValueX, float endValueX, float startValueY, f // Overriding methods //-------------------------------------------------------- @Override - protected void onInitValue(Sprite sprite, float positionX, float positionY) { - sprite.setX(positionX); - sprite.setY(positionY); + protected void onInitValue(Shape shape, float positionX, float positionY) { + shape.setX(positionX); + shape.setY(positionY); } @Override - protected void onUpdateValue(Sprite sprite, float positionX, float positionY) { - sprite.setX(positionX); - sprite.setY(positionY); + protected void onUpdateValue(Shape shape, float positionX, float positionY) { + shape.setX(positionX); + shape.setY(positionY); } @Override - protected void onEndValue(Sprite sprite, float positionX, float positionY) { - sprite.setX(positionX); - sprite.setY(positionY); + protected void onEndValue(Shape shape, float positionX, float positionY) { + shape.setX(positionX); + shape.setY(positionY); } //======================================================== diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/PositionXModifier.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/PositionXModifier.java similarity index 75% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/PositionXModifier.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/PositionXModifier.java index 7261afa..9c991b5 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/PositionXModifier.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/PositionXModifier.java @@ -1,13 +1,13 @@ -package com.nativegame.nattyengine.entity.sprite.modifier; +package com.nativegame.nattyengine.entity.modifier; -import com.nativegame.nattyengine.entity.modifier.tween.Tweener; -import com.nativegame.nattyengine.entity.sprite.Sprite; +import com.nativegame.nattyengine.entity.shape.Shape; +import com.nativegame.nattyengine.util.modifier.tween.Tweener; /** * Created by Oscar Liang on 2022/12/11 */ -public class PositionXModifier extends SingleValueSpriteModifier { +public class PositionXModifier extends SingleValueShapeModifier { //-------------------------------------------------------- // Constructors @@ -49,18 +49,18 @@ public PositionXModifier(float startValue, float endValue, long duration, long s // Overriding methods //-------------------------------------------------------- @Override - protected void onInitValue(Sprite sprite, float positionX) { - sprite.setX(positionX); + protected void onInitValue(Shape shape, float positionX) { + shape.setX(positionX); } @Override - protected void onUpdateValue(Sprite sprite, float positionX) { - sprite.setX(positionX); + protected void onUpdateValue(Shape shape, float positionX) { + shape.setX(positionX); } @Override - protected void onEndValue(Sprite sprite, float positionX) { - sprite.setX(positionX); + protected void onEndValue(Shape shape, float positionX) { + shape.setX(positionX); } //======================================================== diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/PositionYModifier.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/PositionYModifier.java similarity index 75% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/PositionYModifier.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/PositionYModifier.java index 4417328..c5b3338 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/PositionYModifier.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/PositionYModifier.java @@ -1,13 +1,13 @@ -package com.nativegame.nattyengine.entity.sprite.modifier; +package com.nativegame.nattyengine.entity.modifier; -import com.nativegame.nattyengine.entity.modifier.tween.Tweener; -import com.nativegame.nattyengine.entity.sprite.Sprite; +import com.nativegame.nattyengine.entity.shape.Shape; +import com.nativegame.nattyengine.util.modifier.tween.Tweener; /** * Created by Oscar Liang on 2022/12/11 */ -public class PositionYModifier extends SingleValueSpriteModifier { +public class PositionYModifier extends SingleValueShapeModifier { //-------------------------------------------------------- // Constructors @@ -49,18 +49,18 @@ public PositionYModifier(float startValue, float endValue, long duration, long s // Overriding methods //-------------------------------------------------------- @Override - protected void onInitValue(Sprite sprite, float positionY) { - sprite.setY(positionY); + protected void onInitValue(Shape shape, float positionY) { + shape.setY(positionY); } @Override - protected void onUpdateValue(Sprite sprite, float positionY) { - sprite.setY(positionY); + protected void onUpdateValue(Shape shape, float positionY) { + shape.setY(positionY); } @Override - protected void onEndValue(Sprite sprite, float positionY) { - sprite.setY(positionY); + protected void onEndValue(Shape shape, float positionY) { + shape.setY(positionY); } //======================================================== diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/RotationModifier.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/RotationModifier.java similarity index 68% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/RotationModifier.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/RotationModifier.java index 60093f7..e9dc375 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/RotationModifier.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/RotationModifier.java @@ -1,13 +1,13 @@ -package com.nativegame.nattyengine.entity.sprite.modifier; +package com.nativegame.nattyengine.entity.modifier; -import com.nativegame.nattyengine.entity.modifier.tween.Tweener; -import com.nativegame.nattyengine.entity.sprite.Sprite; +import com.nativegame.nattyengine.entity.shape.Shape; +import com.nativegame.nattyengine.util.modifier.tween.Tweener; /** * Created by Oscar Liang on 2022/12/11 */ -public class RotationModifier extends SingleValueSpriteModifier { +public class RotationModifier extends SingleValueShapeModifier { //-------------------------------------------------------- // Constructors @@ -33,18 +33,18 @@ public RotationModifier(float startValue, float endValue, long duration, long st // Overriding methods //-------------------------------------------------------- @Override - protected void onInitValue(Sprite sprite, float rotation) { - sprite.setRotation(rotation); + protected void onInitValue(Shape shape, float rotation) { + shape.setRotation(rotation); } @Override - protected void onUpdateValue(Sprite sprite, float rotation) { - sprite.setRotation(rotation); + protected void onUpdateValue(Shape shape, float rotation) { + shape.setRotation(rotation); } @Override - protected void onEndValue(Sprite sprite, float rotation) { - sprite.setRotation(rotation); + protected void onEndValue(Shape shape, float rotation) { + shape.setRotation(rotation); } //======================================================== diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/ScaleInModifier.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/ScaleInModifier.java similarity index 85% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/ScaleInModifier.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/ScaleInModifier.java index fa22ef8..1205478 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/ScaleInModifier.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/ScaleInModifier.java @@ -1,6 +1,6 @@ -package com.nativegame.nattyengine.entity.sprite.modifier; +package com.nativegame.nattyengine.entity.modifier; -import com.nativegame.nattyengine.entity.modifier.tween.Tweener; +import com.nativegame.nattyengine.util.modifier.tween.Tweener; /** * Created by Oscar Liang on 2022/12/11 diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/ScaleModifier.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/ScaleModifier.java similarity index 79% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/ScaleModifier.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/ScaleModifier.java index 9f67be4..c8d0520 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/ScaleModifier.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/ScaleModifier.java @@ -1,13 +1,13 @@ -package com.nativegame.nattyengine.entity.sprite.modifier; +package com.nativegame.nattyengine.entity.modifier; -import com.nativegame.nattyengine.entity.modifier.tween.Tweener; -import com.nativegame.nattyengine.entity.sprite.Sprite; +import com.nativegame.nattyengine.entity.shape.Shape; +import com.nativegame.nattyengine.util.modifier.tween.Tweener; /** * Created by Oscar Liang on 2022/12/11 */ -public class ScaleModifier extends DoubleValueSpriteModifier { +public class ScaleModifier extends DoubleValueShapeModifier { //-------------------------------------------------------- // Constructors @@ -65,21 +65,21 @@ public ScaleModifier(float startValueX, float endValueX, float startValueY, floa // Overriding methods //-------------------------------------------------------- @Override - protected void onInitValue(Sprite sprite, float scaleX, float scaleY) { - sprite.setScaleX(scaleX); - sprite.setScaleY(scaleY); + protected void onInitValue(Shape shape, float scaleX, float scaleY) { + shape.setScaleX(scaleX); + shape.setScaleY(scaleY); } @Override - protected void onUpdateValue(Sprite sprite, float scaleX, float scaleY) { - sprite.setScaleX(scaleX); - sprite.setScaleY(scaleY); + protected void onUpdateValue(Shape shape, float scaleX, float scaleY) { + shape.setScaleX(scaleX); + shape.setScaleY(scaleY); } @Override - protected void onEndValue(Sprite sprite, float scaleX, float scaleY) { - sprite.setScaleX(scaleX); - sprite.setScaleY(scaleY); + protected void onEndValue(Shape shape, float scaleX, float scaleY) { + shape.setScaleX(scaleX); + shape.setScaleY(scaleY); } //======================================================== diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/ScaleOutModifier.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/ScaleOutModifier.java similarity index 85% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/ScaleOutModifier.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/ScaleOutModifier.java index c44dfb8..d8db233 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/ScaleOutModifier.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/ScaleOutModifier.java @@ -1,6 +1,6 @@ -package com.nativegame.nattyengine.entity.sprite.modifier; +package com.nativegame.nattyengine.entity.modifier; -import com.nativegame.nattyengine.entity.modifier.tween.Tweener; +import com.nativegame.nattyengine.util.modifier.tween.Tweener; /** * Created by Oscar Liang on 2022/12/11 diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/SingleValueShapeModifier.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/SingleValueShapeModifier.java new file mode 100644 index 0000000..b97405d --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/SingleValueShapeModifier.java @@ -0,0 +1,60 @@ +package com.nativegame.nattyengine.entity.modifier; + +import com.nativegame.nattyengine.entity.shape.Shape; +import com.nativegame.nattyengine.util.modifier.SingleValueModifier; +import com.nativegame.nattyengine.util.modifier.tween.LinearTweener; +import com.nativegame.nattyengine.util.modifier.tween.Tweener; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public abstract class SingleValueShapeModifier extends SingleValueModifier { + + private boolean mIsAutoRemove = false; + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + protected SingleValueShapeModifier(float startValue, float endValue, long duration) { + super(startValue, endValue, duration, 0, LinearTweener.getInstance()); + } + + protected SingleValueShapeModifier(float startValue, float endValue, long duration, long startDelay) { + super(startValue, endValue, duration, startDelay, LinearTweener.getInstance()); + } + + protected SingleValueShapeModifier(float startValue, float endValue, long duration, Tweener tweener) { + super(startValue, endValue, duration, 0, tweener); + } + + protected SingleValueShapeModifier(float startValue, float endValue, long duration, long startDelay, Tweener tweener) { + super(startValue, endValue, duration, startDelay, tweener); + } + //======================================================== + + //-------------------------------------------------------- + // Getter and Setter + //-------------------------------------------------------- + public boolean isAutoRemove() { + return mIsAutoRemove; + } + + public void setAutoRemove(boolean autoRemove) { + mIsAutoRemove = autoRemove; + } + //======================================================== + + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + protected void onEndModifier(Shape shape) { + super.onEndModifier(shape); + if (mIsAutoRemove) { + shape.removeFromGame(); + } + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/GenericParticle.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/GenericParticle.java new file mode 100644 index 0000000..5271377 --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/GenericParticle.java @@ -0,0 +1,197 @@ +package com.nativegame.nattyengine.entity.particle; + +import android.graphics.Canvas; + +import com.nativegame.nattyengine.camera.Camera; +import com.nativegame.nattyengine.engine.Engine; +import com.nativegame.nattyengine.entity.Reusable; +import com.nativegame.nattyengine.entity.ScreenEntity; +import com.nativegame.nattyengine.entity.shape.RectangleShape; +import com.nativegame.nattyengine.entity.particle.initializer.ParticleInitializer; +import com.nativegame.nattyengine.entity.particle.modifier.ParticleModifier; + +import java.util.List; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public class GenericParticle extends ScreenEntity implements Particle { + + private final T mChild; + + private Reusable.RecycleListener mListener; + private List mInitializers; + private List mModifiers; + private float mSpeedX; + private float mSpeedY; + private float mAccelerationX; + private float mAccelerationY; + private float mRotationSpeed; + private float mRotation; + private float mScale; + private int mAlpha; + private int mColor; + private long mDuration; + private long mTotalTime; + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + public GenericParticle(Engine engine, T child) { + super(engine); + mChild = child; + } + //======================================================== + + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + public long getDuration() { + return mDuration; + } + + @Override + public void setDuration(long duration) { + mDuration = duration; + } + + @Override + public void setSpeedX(float speedX) { + mSpeedX = speedX; + } + + @Override + public void setSpeedY(float speedY) { + mSpeedY = speedY; + } + + @Override + public void setAccelerationX(float accelerationX) { + mAccelerationX = accelerationX; + } + + @Override + public void setAccelerationY(float accelerationY) { + mAccelerationY = accelerationY; + } + + @Override + public void setRotationSpeed(float rotationSpeed) { + mRotationSpeed = rotationSpeed; + } + + @Override + public void setRotation(float rotation) { + mRotation = rotation; + } + + @Override + public void setScale(float scale) { + mScale = scale; + } + + @Override + public void setAlpha(int alpha) { + mAlpha = alpha; + } + + @Override + public void setColor(int color) { + mColor = color; + } + + @Override + public void setParticleInitializer(List initializers) { + mInitializers = initializers; + } + + @Override + public void setParticleModifier(List modifiers) { + mModifiers = modifiers; + } + + @Override + public void activate(float x, float y, int layer) { + setLayer(layer); + initParticle(); + onUpdateChild(x, y); + addToGame(); + mTotalTime = 0; + } + + @Override + public RecycleListener getRecycleListener() { + return mListener; + } + + @Override + public void setRecycleListener(RecycleListener listener) { + mListener = listener; + } + + @Override + protected void onRemove() { + if (mListener != null) { + mListener.recycle(this); + } + } + + @Override + protected void onUpdate(long elapsedMillis) { + mTotalTime += elapsedMillis; + if (mTotalTime >= mDuration) { + removeFromGame(); + mTotalTime = 0; + } else { + onUpdateChild(mChild.getCenterX() + mSpeedX * elapsedMillis, + mChild.getCenterY() + mSpeedY * elapsedMillis); + mSpeedX += mAccelerationX * elapsedMillis; + mSpeedY += mAccelerationY * elapsedMillis; + mRotation += mRotationSpeed * elapsedMillis; + updateParticle(); + } + } + + @Override + public boolean isCulling(Canvas canvas, Camera camera) { + return mChild.isCulling(canvas, camera); + } + + @Override + protected void onDraw(Canvas canvas, Camera camera) { + mChild.draw(canvas, camera); + } + //======================================================== + + //-------------------------------------------------------- + // Methods + //-------------------------------------------------------- + protected void onUpdateChild(float x, float y) { + mChild.setCenterX(x); + mChild.setCenterY(y); + mChild.setRotation(mRotation); + mChild.setScale(mScale); + mChild.setAlpha(mAlpha); + mChild.setColor(mColor); + } + + private void initParticle() { + int initializerCount = mInitializers.size(); + for (int i = 0; i < initializerCount; i++) { + ParticleInitializer initializer = mInitializers.get(i); + initializer.initParticle(this); + } + } + + private void updateParticle() { + int modifierCount = mModifiers.size(); + for (int i = 0; i < modifierCount; i++) { + ParticleModifier modifier = mModifiers.get(i); + modifier.updateParticle(this, mTotalTime); + } + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/ParticleSystem.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/GenericParticleSystem.java similarity index 72% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/ParticleSystem.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/GenericParticleSystem.java index 6fb3a37..7c8ebd3 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/ParticleSystem.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/GenericParticleSystem.java @@ -1,35 +1,32 @@ -package com.nativegame.nattyengine.entity.particles; +package com.nativegame.nattyengine.entity.particle; import com.nativegame.nattyengine.engine.Engine; import com.nativegame.nattyengine.entity.Entity; -import com.nativegame.nattyengine.entity.modifier.DurationModifier; -import com.nativegame.nattyengine.entity.modifier.Modifier; -import com.nativegame.nattyengine.entity.particles.initializer.AccelerationXParticleInitializer; -import com.nativegame.nattyengine.entity.particles.initializer.AccelerationYParticleInitializer; -import com.nativegame.nattyengine.entity.particles.initializer.DurationParticleInitialize; -import com.nativegame.nattyengine.entity.particles.initializer.ParticleInitializer; -import com.nativegame.nattyengine.entity.particles.initializer.RotationParticleInitializer; -import com.nativegame.nattyengine.entity.particles.initializer.RotationSpeedParticleInitializer; -import com.nativegame.nattyengine.entity.particles.initializer.SpeedParticleInitializer; -import com.nativegame.nattyengine.entity.particles.initializer.SpeedXParticleInitializer; -import com.nativegame.nattyengine.entity.particles.initializer.SpeedYParticleInitializer; -import com.nativegame.nattyengine.entity.particles.modifier.AlphaParticleModifier; -import com.nativegame.nattyengine.entity.particles.modifier.ParticleModifier; -import com.nativegame.nattyengine.entity.particles.modifier.RotationParticleModifier; -import com.nativegame.nattyengine.entity.particles.modifier.ScaleParticleModifier; -import com.nativegame.nattyengine.texture.Texture; +import com.nativegame.nattyengine.entity.Reusable; +import com.nativegame.nattyengine.entity.particle.initializer.AccelerationXParticleInitializer; +import com.nativegame.nattyengine.entity.particle.initializer.AccelerationYParticleInitializer; +import com.nativegame.nattyengine.entity.particle.initializer.ColorParticleInitializer; +import com.nativegame.nattyengine.entity.particle.initializer.DurationParticleInitialize; +import com.nativegame.nattyengine.entity.particle.initializer.ParticleInitializer; +import com.nativegame.nattyengine.entity.particle.initializer.RotationParticleInitializer; +import com.nativegame.nattyengine.entity.particle.initializer.RotationSpeedParticleInitializer; +import com.nativegame.nattyengine.entity.particle.initializer.SpeedWithAngleParticleInitializer; +import com.nativegame.nattyengine.entity.particle.initializer.SpeedXParticleInitializer; +import com.nativegame.nattyengine.entity.particle.initializer.SpeedYParticleInitializer; +import com.nativegame.nattyengine.entity.particle.modifier.AlphaParticleModifier; +import com.nativegame.nattyengine.entity.particle.modifier.ParticleModifier; +import com.nativegame.nattyengine.entity.particle.modifier.RotationParticleModifier; +import com.nativegame.nattyengine.entity.particle.modifier.ScaleParticleModifier; import com.nativegame.nattyengine.util.math.RandomUtils; -import com.nativegame.nattyengine.util.pool.ObjectPool; +import com.nativegame.nattyengine.util.modifier.DurationModifier; +import com.nativegame.nattyengine.util.modifier.Modifier; import com.nativegame.nattyengine.util.pool.Pool; import java.util.ArrayList; import java.util.List; -/** - * Created by Oscar Liang on 2022/12/11 - */ - -public class ParticleSystem extends Entity implements Modifier.ModifierListener { +public class GenericParticleSystem extends Entity + implements ParticleSystem, Reusable.RecycleListener, Modifier.ModifierListener { public static final int RATE_LOW = 10; public static final int RATE_NORMAL = 20; @@ -55,53 +52,50 @@ public class ParticleSystem extends Entity implements Modifier.ModifierListener //-------------------------------------------------------- // Constructors //-------------------------------------------------------- - public ParticleSystem(Engine engine, Texture texture, int minParticle) { - this(engine, texture, minParticle, minParticle); - } - - public ParticleSystem(Engine engine, Texture texture, int minParticle, int maxParticle) { + public GenericParticleSystem(Engine engine, Pool particlePool) { super(engine); - mParticlePool = new ObjectPool<>(new Pool.PoolObjectFactory() { - @Override - public Particle createObject() { - return new Particle(ParticleSystem.this, engine, texture); - } - }, minParticle, maxParticle); + mParticlePool = particlePool; mDurationModifier = new DurationModifier(INFINITE); mDurationModifier.setListener(this); } //======================================================== //-------------------------------------------------------- - // Getter and Setter + // Overriding methods //-------------------------------------------------------- + @Override public ParticleSystem setLayer(int layer) { mLayer = layer; return this; } + @Override public ParticleSystem setEmissionRate(int particlePerSecond) { mEmissionRate = 1000 / particlePerSecond; return this; } + @Override public ParticleSystem setEmissionDuration(long duration) { mEmissionDuration = duration; return this; } + @Override public ParticleSystem setEmissionPositionX(float x) { mEmissionMinX = x; mEmissionMaxX = x; return this; } + @Override public ParticleSystem setEmissionPositionY(float y) { mEmissionMinY = y; mEmissionMaxY = y; return this; } + @Override public ParticleSystem setEmissionPosition(float x, float y) { mEmissionMinX = x; mEmissionMaxX = x; @@ -110,149 +104,231 @@ public ParticleSystem setEmissionPosition(float x, float y) { return this; } + @Override public ParticleSystem setEmissionRangeX(float minX, float maxX) { mEmissionMinX = minX; mEmissionMaxX = maxX; return this; } + @Override public ParticleSystem setEmissionRangeY(float minY, float maxY) { mEmissionMinY = minY; mEmissionMaxY = maxY; return this; } + @Override public ParticleSystem setDuration(long duration) { mInitializers.add(new DurationParticleInitialize(duration)); return this; } + @Override public ParticleSystem setSpeedWithAngle(float minSpeed, float maxSpeed) { - mInitializers.add(new SpeedParticleInitializer(minSpeed, maxSpeed, 0, 360)); + mInitializers.add(new SpeedWithAngleParticleInitializer(minSpeed, maxSpeed, 0, 360)); return this; } + @Override public ParticleSystem setSpeedWithAngle(float minSpeed, float maxSpeed, int minAngle, int maxAngle) { - mInitializers.add(new SpeedParticleInitializer(minSpeed, maxSpeed, minAngle, maxAngle)); + mInitializers.add(new SpeedWithAngleParticleInitializer(minSpeed, maxSpeed, minAngle, maxAngle)); return this; } + @Override public ParticleSystem setSpeedX(float speedX) { mInitializers.add(new SpeedXParticleInitializer(speedX, speedX)); return this; } + @Override public ParticleSystem setSpeedX(float minSpeedX, float maxSpeedX) { mInitializers.add(new SpeedXParticleInitializer(minSpeedX, maxSpeedX)); return this; } + @Override public ParticleSystem setSpeedY(float speedY) { mInitializers.add(new SpeedYParticleInitializer(speedY, speedY)); return this; } + @Override public ParticleSystem setSpeedY(float minSpeedY, float maxSpeedY) { mInitializers.add(new SpeedYParticleInitializer(minSpeedY, maxSpeedY)); return this; } + @Override public ParticleSystem setAccelerationX(float accelerateX) { mInitializers.add(new AccelerationXParticleInitializer(accelerateX, accelerateX)); return this; } + @Override public ParticleSystem setAccelerationX(float minAccelerateX, float maxAccelerateX) { mInitializers.add(new AccelerationXParticleInitializer(minAccelerateX, maxAccelerateX)); return this; } + @Override public ParticleSystem setAccelerationY(float accelerateY) { mInitializers.add(new AccelerationYParticleInitializer(accelerateY, accelerateY)); return this; } + @Override public ParticleSystem setAccelerationY(float minAccelerateY, float maxAccelerateY) { mInitializers.add(new AccelerationYParticleInitializer(minAccelerateY, maxAccelerateY)); return this; } + @Override + public ParticleSystem setRotationSpeed(float rotationSpeed) { + mInitializers.add(new RotationSpeedParticleInitializer(rotationSpeed, rotationSpeed)); + return this; + } + + @Override + public ParticleSystem setRotationSpeed(float minRotationSpeed, float maxRotationSpeed) { + mInitializers.add(new RotationSpeedParticleInitializer(minRotationSpeed, maxRotationSpeed)); + return this; + } + + @Override public ParticleSystem setInitialRotation(int angle) { mInitializers.add(new RotationParticleInitializer(angle, angle)); return this; } + @Override public ParticleSystem setInitialRotation(int minAngle, int maxAngle) { mInitializers.add(new RotationParticleInitializer(minAngle, maxAngle)); return this; } - public ParticleSystem setRotationSpeed(float rotationSpeed) { - mInitializers.add(new RotationSpeedParticleInitializer(rotationSpeed, rotationSpeed)); + @Override + public ParticleSystem setColor(int color) { + mInitializers.add(new ColorParticleInitializer(color)); return this; } - public ParticleSystem setRotationSpeed(float minRotationSpeed, float maxRotationSpeed) { - mInitializers.add(new RotationSpeedParticleInitializer(minRotationSpeed, maxRotationSpeed)); + @Override + public ParticleSystem addInitializer(ParticleInitializer initializer) { + mInitializers.add(initializer); return this; } - public ParticleSystem addInitializer(ParticleInitializer initializer) { - mInitializers.add(initializer); + @Override + public ParticleSystem removeInitializer(ParticleInitializer initializer) { + mInitializers.remove(initializer); return this; } + @Override public ParticleSystem clearInitializer() { mInitializers.clear(); return this; } + @Override public ParticleSystem setRotation(float startValue, float endValue) { mModifiers.add(new RotationParticleModifier(startValue, endValue, 0)); return this; } + @Override public ParticleSystem setRotation(float startValue, float endValue, long startDelay) { mModifiers.add(new RotationParticleModifier(startValue, endValue, startDelay)); return this; } + @Override public ParticleSystem setScale(float startValue, float endValue) { mModifiers.add(new ScaleParticleModifier(startValue, endValue, 0)); return this; } + @Override public ParticleSystem setScale(float startValue, float endValue, long startDelay) { mModifiers.add(new ScaleParticleModifier(startValue, endValue, startDelay)); return this; } + @Override public ParticleSystem setAlpha(float startValue, float endValue) { mModifiers.add(new AlphaParticleModifier(startValue, endValue, 0)); return this; } + @Override public ParticleSystem setAlpha(float startValue, float endValue, long startDelay) { mModifiers.add(new AlphaParticleModifier(startValue, endValue, startDelay)); return this; } + @Override public ParticleSystem addModifier(ParticleModifier modifier) { mModifiers.add(modifier); return this; } + @Override + public ParticleSystem removeModifier(ParticleModifier modifier) { + mModifiers.remove(modifier); + return this; + } + + @Override public ParticleSystem clearModifier() { mModifiers.clear(); return this; } - //======================================================== - //-------------------------------------------------------- - // Overriding methods - //-------------------------------------------------------- @Override - public void onUpdate(long elapsedMillis) { + public void emit() { + mIsEmitting = true; + if (mEmissionDuration != INFINITE) { + mDurationModifier.setDuration(mEmissionDuration); + mDurationModifier.init(this); + } + addToGame(); + mTotalTime = 0; + } + + @Override + public void stopEmit() { + mIsEmitting = false; + mDurationModifier.reset(this); + removeFromGame(); + mTotalTime = 0; + } + + @Override + public void oneShot(float x, float y) { + oneShot(x, y, mParticlePool.getObjectCount()); + } + + @Override + public void oneShot(float x, float y, int count) { + for (int i = 0; i < count; i++) { + addOneParticle(x, y); + } + } + + @Override + public void recycle(Particle particle) { + mParticlePool.returnObject(particle); + } + + @Override + public void onModifierComplete() { + stopEmit(); + } + + @Override + protected void onUpdate(long elapsedMillis) { if (!mIsEmitting) { return; } @@ -263,7 +339,7 @@ public void onUpdate(long elapsedMillis) { float emissionX = RandomUtils.nextFloat(mEmissionMinX, mEmissionMaxX); float emissionY = RandomUtils.nextFloat(mEmissionMinY, mEmissionMaxY); addOneParticle(emissionX, emissionY); - mTotalTime = 0; + mTotalTime = mTotalTime % mEmissionRate; } mDurationModifier.update(this, elapsedMillis); } @@ -279,61 +355,23 @@ public void reset() { } @Override - public void dispose() { - super.dispose(); + public void release() { + super.release(); mParticlePool.release(); mInitializers.clear(); mModifiers.clear(); } - - @Override - public void onModifierComplete() { - stopEmit(); - } //======================================================== //-------------------------------------------------------- // Methods //-------------------------------------------------------- - public void emit() { - mIsEmitting = true; - if (mEmissionDuration != INFINITE) { - mDurationModifier.setDuration(mEmissionDuration); - mDurationModifier.init(this); - } - addToGame(); - mTotalTime = 0; - } - - public void stopEmit() { - mIsEmitting = false; - mDurationModifier.reset(this); - removeFromGame(); - mTotalTime = 0; - } - - public void oneShot(float x, float y) { - oneShot(x, y, mParticlePool.getMaxObject()); - } - - public void oneShot(float x, float y, int count) { - for (int i = 0; i < count; i++) { - addOneParticle(x, y); - } - } - - public void returnToPool(Particle particle) { - mParticlePool.returnObject(particle); - } - private void addOneParticle(float x, float y) { Particle p = mParticlePool.obtainObject(); - int size = mInitializers.size(); - for (int i = 0; i < size; i++) { - ParticleInitializer initializer = mInitializers.get(i); - initializer.initParticle(p); - } - p.activate(x, y, mLayer, mModifiers); + p.setParticleInitializer(mInitializers); + p.setParticleModifier(mModifiers); + p.setRecycleListener(this); + p.activate(x, y, mLayer); } //======================================================== diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/Particle.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/Particle.java new file mode 100644 index 0000000..ca7d697 --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/Particle.java @@ -0,0 +1,43 @@ +package com.nativegame.nattyengine.entity.particle; + +import com.nativegame.nattyengine.entity.Reusable; +import com.nativegame.nattyengine.entity.particle.initializer.ParticleInitializer; +import com.nativegame.nattyengine.entity.particle.modifier.ParticleModifier; + +import java.util.List; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public interface Particle extends Reusable { + + long getDuration(); + + void setDuration(long duration); + + void setSpeedX(float speedX); + + void setSpeedY(float speedY); + + void setAccelerationX(float accelerationX); + + void setAccelerationY(float accelerationY); + + void setRotationSpeed(float rotationSpeed); + + void setRotation(float rotation); + + void setScale(float scale); + + void setAlpha(int alpha); + + void setColor(int color); + + void setParticleInitializer(List initializers); + + void setParticleModifier(List modifiers); + + void activate(float x, float y, int layer); + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/ParticleSystem.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/ParticleSystem.java new file mode 100644 index 0000000..2f52987 --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/ParticleSystem.java @@ -0,0 +1,92 @@ +package com.nativegame.nattyengine.entity.particle; + +import com.nativegame.nattyengine.entity.particle.initializer.ParticleInitializer; +import com.nativegame.nattyengine.entity.particle.modifier.ParticleModifier; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public interface ParticleSystem { + + ParticleSystem setLayer(int layer); + + ParticleSystem setEmissionRate(int particlePerSecond); + + ParticleSystem setEmissionDuration(long duration); + + ParticleSystem setEmissionPositionX(float x); + + ParticleSystem setEmissionPositionY(float y); + + ParticleSystem setEmissionPosition(float x, float y); + + ParticleSystem setEmissionRangeX(float minX, float maxX); + + ParticleSystem setEmissionRangeY(float minY, float maxY); + + ParticleSystem setDuration(long duration); + + ParticleSystem setSpeedWithAngle(float minSpeed, float maxSpeed); + + ParticleSystem setSpeedWithAngle(float minSpeed, float maxSpeed, int minAngle, int maxAngle); + + ParticleSystem setSpeedX(float speedX); + + ParticleSystem setSpeedX(float minSpeedX, float maxSpeedX); + + ParticleSystem setSpeedY(float speedY); + + ParticleSystem setSpeedY(float minSpeedY, float maxSpeedY); + + ParticleSystem setAccelerationX(float accelerateX); + + ParticleSystem setAccelerationX(float minAccelerateX, float maxAccelerateX); + + ParticleSystem setAccelerationY(float accelerateY); + + ParticleSystem setAccelerationY(float minAccelerateY, float maxAccelerateY); + + ParticleSystem setRotationSpeed(float rotationSpeed); + + ParticleSystem setRotationSpeed(float minRotationSpeed, float maxRotationSpeed); + + ParticleSystem setInitialRotation(int angle); + + ParticleSystem setInitialRotation(int minAngle, int maxAngle); + + ParticleSystem setColor(int color); + + ParticleSystem addInitializer(ParticleInitializer initializer); + + ParticleSystem removeInitializer(ParticleInitializer initializer); + + ParticleSystem clearInitializer(); + + ParticleSystem setRotation(float startValue, float endValue); + + ParticleSystem setRotation(float startValue, float endValue, long startDelay); + + ParticleSystem setScale(float startValue, float endValue); + + ParticleSystem setScale(float startValue, float endValue, long startDelay); + + ParticleSystem setAlpha(float startValue, float endValue); + + ParticleSystem setAlpha(float startValue, float endValue, long startDelay); + + ParticleSystem addModifier(ParticleModifier modifier); + + ParticleSystem removeModifier(ParticleModifier modifier); + + ParticleSystem clearModifier(); + + public void emit(); + + public void stopEmit(); + + public void oneShot(float x, float y); + + public void oneShot(float x, float y, int count); + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/ParticleSystemGroup.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/ParticleSystemGroup.java similarity index 94% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/ParticleSystemGroup.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/ParticleSystemGroup.java index d977b1c..ec1a8c1 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/ParticleSystemGroup.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/ParticleSystemGroup.java @@ -1,7 +1,7 @@ -package com.nativegame.nattyengine.entity.particles; +package com.nativegame.nattyengine.entity.particle; -import com.nativegame.nattyengine.entity.particles.initializer.ParticleInitializer; -import com.nativegame.nattyengine.entity.particles.modifier.ParticleModifier; +import com.nativegame.nattyengine.entity.particle.initializer.ParticleInitializer; +import com.nativegame.nattyengine.entity.particle.modifier.ParticleModifier; import java.util.ArrayList; import java.util.List; @@ -367,23 +367,6 @@ public void oneShot(float x, float y, int count) { particleSystem.oneShot(x, y, count); } } - - public void reset() { - int size = mParticleSystems.size(); - for (int i = 0; i < size; i++) { - ParticleSystem particleSystem = mParticleSystems.get(i); - particleSystem.reset(); - } - } - - public void dispose() { - int size = mParticleSystems.size(); - for (int i = 0; i < size; i++) { - ParticleSystem particleSystem = mParticleSystems.get(i); - particleSystem.dispose(); - } - mParticleSystems.clear(); - } //======================================================== } diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/SpriteParticleSystem.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/SpriteParticleSystem.java new file mode 100644 index 0000000..3c11a3e --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/SpriteParticleSystem.java @@ -0,0 +1,20 @@ +package com.nativegame.nattyengine.entity.particle; + +import com.nativegame.nattyengine.engine.Engine; +import com.nativegame.nattyengine.entity.sprite.Sprite; +import com.nativegame.nattyengine.texture.Texture; +import com.nativegame.nattyengine.util.pool.Pool; +import com.nativegame.nattyengine.util.pool.SafeFixedObjectPool; + +public class SpriteParticleSystem extends GenericParticleSystem { + + public SpriteParticleSystem(Engine engine, Texture texture, int min, int max) { + super(engine, new SafeFixedObjectPool<>(new Pool.PoolObjectFactory() { + @Override + public Particle createObject() { + return new GenericParticle<>(engine, new Sprite(engine, texture)); + } + }, min, max)); + } + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/AccelerationXParticleInitializer.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/AccelerationXParticleInitializer.java new file mode 100644 index 0000000..c86ef62 --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/AccelerationXParticleInitializer.java @@ -0,0 +1,28 @@ +package com.nativegame.nattyengine.entity.particle.initializer; + +import com.nativegame.nattyengine.entity.particle.Particle; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public class AccelerationXParticleInitializer extends SingleValueParticleInitializer { + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + public AccelerationXParticleInitializer(float minValue, float maxValue) { + super(minValue, maxValue); + } + //======================================================== + + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + protected void onInitParticle(Particle particle, float value) { + particle.setAccelerationX(value / 1000); + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/AccelerationYParticleInitializer.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/AccelerationYParticleInitializer.java new file mode 100644 index 0000000..9bccd2a --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/AccelerationYParticleInitializer.java @@ -0,0 +1,28 @@ +package com.nativegame.nattyengine.entity.particle.initializer; + +import com.nativegame.nattyengine.entity.particle.Particle; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public class AccelerationYParticleInitializer extends SingleValueParticleInitializer { + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + public AccelerationYParticleInitializer(float minValue, float maxValue) { + super(minValue, maxValue); + } + //======================================================== + + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + protected void onInitParticle(Particle particle, float value) { + particle.setAccelerationY(value / 1000); + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/hitbox/CircleHitBox.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/ColorParticleInitializer.java similarity index 51% rename from nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/hitbox/CircleHitBox.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/ColorParticleInitializer.java index 5eac0a4..2ff47fe 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/hitbox/CircleHitBox.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/ColorParticleInitializer.java @@ -1,23 +1,20 @@ -package com.nativegame.nattyengine.engine.collision.hitbox; +package com.nativegame.nattyengine.entity.particle.initializer; -import android.graphics.Bitmap; - -import com.nativegame.nattyengine.util.bitmap.BitmapUtils; +import com.nativegame.nattyengine.entity.particle.Particle; /** * Created by Oscar Liang on 2022/12/11 */ -public class CircleHitBox extends BaseHitBox { +public class ColorParticleInitializer implements ParticleInitializer { - private final Bitmap mHitBoxBitmap; + private final int mColor; //-------------------------------------------------------- // Constructors //-------------------------------------------------------- - public CircleHitBox(int hitBoxRadius) { - super(hitBoxRadius * 2, hitBoxRadius * 2); - mHitBoxBitmap = BitmapUtils.createCircleBitmap(hitBoxRadius); + public ColorParticleInitializer(int color) { + mColor = color; } //======================================================== @@ -25,8 +22,8 @@ public CircleHitBox(int hitBoxRadius) { // Overriding methods //-------------------------------------------------------- @Override - public Bitmap getCollisionBitmap() { - return mHitBoxBitmap; + public void initParticle(Particle particle) { + particle.setColor(mColor); } //======================================================== diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/DoubleValueParticleInitializer.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/DoubleValueParticleInitializer.java new file mode 100644 index 0000000..12b3463 --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/DoubleValueParticleInitializer.java @@ -0,0 +1,45 @@ +package com.nativegame.nattyengine.entity.particle.initializer; + +import com.nativegame.nattyengine.entity.particle.Particle; +import com.nativegame.nattyengine.util.math.RandomUtils; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public abstract class DoubleValueParticleInitializer implements ParticleInitializer { + + private final float mMinValueX; + private final float mMaxValueX; + private final float mMinValueY; + private final float mMaxValueY; + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + protected DoubleValueParticleInitializer(float minValueX, float maxValueX, float minValueY, float maxValueY) { + mMinValueX = minValueX; + mMaxValueX = maxValueX; + mMinValueY = minValueY; + mMaxValueY = maxValueY; + } + //======================================================== + + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + public void initParticle(Particle particle) { + float valueX = RandomUtils.nextFloat(mMinValueX, mMaxValueX); + float valueY = RandomUtils.nextFloat(mMinValueY, mMaxValueY); + onInitParticle(particle, valueX, valueY); + } + //======================================================== + + //-------------------------------------------------------- + // Methods + //-------------------------------------------------------- + protected abstract void onInitParticle(Particle particle, float valueX, float valueY); + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/initializer/DurationParticleInitialize.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/DurationParticleInitialize.java similarity index 86% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/initializer/DurationParticleInitialize.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/DurationParticleInitialize.java index 0ef470d..432edff 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/initializer/DurationParticleInitialize.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/DurationParticleInitialize.java @@ -1,6 +1,6 @@ -package com.nativegame.nattyengine.entity.particles.initializer; +package com.nativegame.nattyengine.entity.particle.initializer; -import com.nativegame.nattyengine.entity.particles.Particle; +import com.nativegame.nattyengine.entity.particle.Particle; /** * Created by Oscar Liang on 2022/12/11 diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/initializer/ParticleInitializer.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/ParticleInitializer.java similarity index 51% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/initializer/ParticleInitializer.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/ParticleInitializer.java index ee0d143..1eb66ed 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/initializer/ParticleInitializer.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/ParticleInitializer.java @@ -1,6 +1,6 @@ -package com.nativegame.nattyengine.entity.particles.initializer; +package com.nativegame.nattyengine.entity.particle.initializer; -import com.nativegame.nattyengine.entity.particles.Particle; +import com.nativegame.nattyengine.entity.particle.Particle; /** * Created by Oscar Liang on 2022/12/11 diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/RotationParticleInitializer.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/RotationParticleInitializer.java new file mode 100644 index 0000000..38107d1 --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/RotationParticleInitializer.java @@ -0,0 +1,28 @@ +package com.nativegame.nattyengine.entity.particle.initializer; + +import com.nativegame.nattyengine.entity.particle.Particle; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public class RotationParticleInitializer extends SingleValueParticleInitializer { + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + public RotationParticleInitializer(float minValue, float maxValue) { + super(minValue, maxValue); + } + //======================================================== + + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + protected void onInitParticle(Particle particle, float value) { + particle.setRotation(value); + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/RotationSpeedParticleInitializer.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/RotationSpeedParticleInitializer.java new file mode 100644 index 0000000..982986d --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/RotationSpeedParticleInitializer.java @@ -0,0 +1,28 @@ +package com.nativegame.nattyengine.entity.particle.initializer; + +import com.nativegame.nattyengine.entity.particle.Particle; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public class RotationSpeedParticleInitializer extends SingleValueParticleInitializer { + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + public RotationSpeedParticleInitializer(float minValue, float maxValue) { + super(minValue, maxValue); + } + //======================================================== + + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + protected void onInitParticle(Particle particle, float value) { + particle.setRotationSpeed(value / 1000); + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/SingleValueParticleInitializer.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/SingleValueParticleInitializer.java new file mode 100644 index 0000000..a70de89 --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/SingleValueParticleInitializer.java @@ -0,0 +1,40 @@ +package com.nativegame.nattyengine.entity.particle.initializer; + +import com.nativegame.nattyengine.entity.particle.Particle; +import com.nativegame.nattyengine.util.math.RandomUtils; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public abstract class SingleValueParticleInitializer implements ParticleInitializer { + + private final float mMinValue; + private final float mMaxValue; + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + protected SingleValueParticleInitializer(float minValue, float maxValue) { + mMinValue = minValue; + mMaxValue = maxValue; + } + //======================================================== + + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + public void initParticle(Particle particle) { + float value = RandomUtils.nextFloat(mMinValue, mMaxValue); + onInitParticle(particle, value); + } + //======================================================== + + //-------------------------------------------------------- + // Methods + //-------------------------------------------------------- + protected abstract void onInitParticle(Particle particle, float value); + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/SpeedWithAngleParticleInitializer.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/SpeedWithAngleParticleInitializer.java new file mode 100644 index 0000000..d1252fa --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/SpeedWithAngleParticleInitializer.java @@ -0,0 +1,32 @@ +package com.nativegame.nattyengine.entity.particle.initializer; + +import com.nativegame.nattyengine.entity.particle.Particle; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public class SpeedWithAngleParticleInitializer extends DoubleValueParticleInitializer { + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + public SpeedWithAngleParticleInitializer(float minValueX, float maxValueX, float minValueY, float maxValueY) { + super(minValueX, maxValueX, minValueY, maxValueY); + } + //======================================================== + + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + protected void onInitParticle(Particle particle, float speed, float angle) { + double angleInRads = Math.toRadians(angle); + float valueX = (float) (speed * Math.cos(angleInRads) / 1000); + float valueY = (float) (speed * Math.sin(angleInRads) / 1000); + particle.setSpeedX(valueX); + particle.setSpeedY(valueY); + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/SpeedXParticleInitializer.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/SpeedXParticleInitializer.java new file mode 100644 index 0000000..e354347 --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/SpeedXParticleInitializer.java @@ -0,0 +1,28 @@ +package com.nativegame.nattyengine.entity.particle.initializer; + +import com.nativegame.nattyengine.entity.particle.Particle; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public class SpeedXParticleInitializer extends SingleValueParticleInitializer { + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + public SpeedXParticleInitializer(float minValue, float maxValue) { + super(minValue, maxValue); + } + //======================================================== + + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + protected void onInitParticle(Particle particle, float value) { + particle.setSpeedX(value / 1000); + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/SpeedYParticleInitializer.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/SpeedYParticleInitializer.java new file mode 100644 index 0000000..45e3059 --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/initializer/SpeedYParticleInitializer.java @@ -0,0 +1,28 @@ +package com.nativegame.nattyengine.entity.particle.initializer; + +import com.nativegame.nattyengine.entity.particle.Particle; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public class SpeedYParticleInitializer extends SingleValueParticleInitializer { + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + public SpeedYParticleInitializer(float minValue, float maxValue) { + super(minValue, maxValue); + } + //======================================================== + + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + protected void onInitParticle(Particle particle, float value) { + particle.setSpeedY(value / 1000); + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/modifier/AlphaParticleModifier.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/modifier/AlphaParticleModifier.java new file mode 100644 index 0000000..51d093a --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/modifier/AlphaParticleModifier.java @@ -0,0 +1,28 @@ +package com.nativegame.nattyengine.entity.particle.modifier; + +import com.nativegame.nattyengine.entity.particle.Particle; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public class AlphaParticleModifier extends SingleValueParticleModifier { + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + public AlphaParticleModifier(float startValue, float endValue, long startDelay) { + super(startValue, endValue, startDelay); + } + //======================================================== + + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + protected void onUpdateParticle(Particle particle, float value) { + particle.setAlpha((int) value); + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/modifier/DoubleValueParticleModifier.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/modifier/DoubleValueParticleModifier.java new file mode 100644 index 0000000..799ca05 --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/modifier/DoubleValueParticleModifier.java @@ -0,0 +1,51 @@ +package com.nativegame.nattyengine.entity.particle.modifier; + +import com.nativegame.nattyengine.entity.particle.Particle; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public abstract class DoubleValueParticleModifier implements ParticleModifier { + + private final float mStartValueX; + private final float mStartValueY; + private final float mRangeX; + private final float mRangeY; + private final long mStartDelay; + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + protected DoubleValueParticleModifier(float startValueX, float endValueX, float startValueY, float endValueY, long startDelay) { + mStartValueX = startValueX; + mStartValueY = startValueY; + mRangeX = endValueX - startValueX; + mRangeY = endValueY - startValueY; + mStartDelay = startDelay; + } + //======================================================== + + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + public void updateParticle(Particle particle, long elapsedMillis) { + if (elapsedMillis < mStartDelay) { + onUpdateParticle(particle, mStartValueX, mStartValueY); + } else { + float percentage = (elapsedMillis - mStartDelay) * 1f / (particle.getDuration() - mStartDelay); + float valueX = mStartValueX + mRangeX * percentage; + float valueY = mStartValueY + mRangeY * percentage; + onUpdateParticle(particle, valueX, valueY); + } + } + //======================================================== + + //-------------------------------------------------------- + // Methods + //-------------------------------------------------------- + protected abstract void onUpdateParticle(Particle particle, float valueX, float valueY); + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/modifier/ParticleModifier.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/modifier/ParticleModifier.java similarity index 55% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/modifier/ParticleModifier.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/modifier/ParticleModifier.java index 2849097..6be2f15 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/modifier/ParticleModifier.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/modifier/ParticleModifier.java @@ -1,6 +1,6 @@ -package com.nativegame.nattyengine.entity.particles.modifier; +package com.nativegame.nattyengine.entity.particle.modifier; -import com.nativegame.nattyengine.entity.particles.Particle; +import com.nativegame.nattyengine.entity.particle.Particle; /** * Created by Oscar Liang on 2022/12/11 diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/modifier/RotationParticleModifier.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/modifier/RotationParticleModifier.java new file mode 100644 index 0000000..7a81913 --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/modifier/RotationParticleModifier.java @@ -0,0 +1,28 @@ +package com.nativegame.nattyengine.entity.particle.modifier; + +import com.nativegame.nattyengine.entity.particle.Particle; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public class RotationParticleModifier extends SingleValueParticleModifier { + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + public RotationParticleModifier(float startValue, float endValue, long startDelay) { + super(startValue, endValue, startDelay); + } + //======================================================== + + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + protected void onUpdateParticle(Particle particle, float value) { + particle.setRotation(value); + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/modifier/ScaleParticleModifier.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/modifier/ScaleParticleModifier.java new file mode 100644 index 0000000..8a04563 --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/modifier/ScaleParticleModifier.java @@ -0,0 +1,28 @@ +package com.nativegame.nattyengine.entity.particle.modifier; + +import com.nativegame.nattyengine.entity.particle.Particle; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public class ScaleParticleModifier extends SingleValueParticleModifier { + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + public ScaleParticleModifier(float startValue, float endValue, long startDelay) { + super(startValue, endValue, startDelay); + } + //======================================================== + + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + protected void onUpdateParticle(Particle particle, float value) { + particle.setScale(value); + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/modifier/ScaleParticleModifier.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/modifier/SingleValueParticleModifier.java similarity index 60% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/modifier/ScaleParticleModifier.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/modifier/SingleValueParticleModifier.java index a6e21f6..7390d11 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/modifier/ScaleParticleModifier.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/modifier/SingleValueParticleModifier.java @@ -1,12 +1,12 @@ -package com.nativegame.nattyengine.entity.particles.modifier; +package com.nativegame.nattyengine.entity.particle.modifier; -import com.nativegame.nattyengine.entity.particles.Particle; +import com.nativegame.nattyengine.entity.particle.Particle; /** * Created by Oscar Liang on 2022/12/11 */ -public class ScaleParticleModifier implements ParticleModifier { +public abstract class SingleValueParticleModifier implements ParticleModifier { private final float mStartValue; private final float mRange; @@ -15,7 +15,7 @@ public class ScaleParticleModifier implements ParticleModifier { //-------------------------------------------------------- // Constructors //-------------------------------------------------------- - public ScaleParticleModifier(float startValue, float endValue, long startDelay) { + protected SingleValueParticleModifier(float startValue, float endValue, long startDelay) { mStartValue = startValue; mRange = endValue - startValue; mStartDelay = startDelay; @@ -28,13 +28,19 @@ public ScaleParticleModifier(float startValue, float endValue, long startDelay) @Override public void updateParticle(Particle particle, long elapsedMillis) { if (elapsedMillis < mStartDelay) { - particle.setScale(mStartValue); + onUpdateParticle(particle, mStartValue); } else { float percentage = (elapsedMillis - mStartDelay) * 1f / (particle.getDuration() - mStartDelay); float value = mStartValue + mRange * percentage; - particle.setScale(value); + onUpdateParticle(particle, value); } } //======================================================== + //-------------------------------------------------------- + // Methods + //-------------------------------------------------------- + protected abstract void onUpdateParticle(Particle particle, float value); + //======================================================== + } diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/shape/CircleParticleSystem.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/shape/CircleParticleSystem.java new file mode 100644 index 0000000..b392a8b --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/shape/CircleParticleSystem.java @@ -0,0 +1,22 @@ +package com.nativegame.nattyengine.entity.particle.shape; + +import com.nativegame.nattyengine.engine.Engine; +import com.nativegame.nattyengine.entity.particle.GenericParticle; +import com.nativegame.nattyengine.entity.particle.GenericParticleSystem; +import com.nativegame.nattyengine.entity.particle.Particle; +import com.nativegame.nattyengine.entity.shape.geometry.Circle; +import com.nativegame.nattyengine.util.pool.Pool; +import com.nativegame.nattyengine.util.pool.SafeFixedObjectPool; + +public class CircleParticleSystem extends GenericParticleSystem { + + public CircleParticleSystem(Engine engine, int radius, int min, int max) { + super(engine, new SafeFixedObjectPool<>(new Pool.PoolObjectFactory() { + @Override + public Particle createObject() { + return new GenericParticle<>(engine, new Circle(engine, radius)); + } + }, min, max)); + } + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/shape/OvalParticleSystem.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/shape/OvalParticleSystem.java new file mode 100644 index 0000000..9b5ae9f --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/shape/OvalParticleSystem.java @@ -0,0 +1,22 @@ +package com.nativegame.nattyengine.entity.particle.shape; + +import com.nativegame.nattyengine.engine.Engine; +import com.nativegame.nattyengine.entity.particle.GenericParticle; +import com.nativegame.nattyengine.entity.particle.GenericParticleSystem; +import com.nativegame.nattyengine.entity.particle.Particle; +import com.nativegame.nattyengine.entity.shape.geometry.Oval; +import com.nativegame.nattyengine.util.pool.Pool; +import com.nativegame.nattyengine.util.pool.SafeFixedObjectPool; + +public class OvalParticleSystem extends GenericParticleSystem { + + public OvalParticleSystem(Engine engine, int width, int height, int min, int max) { + super(engine, new SafeFixedObjectPool<>(new Pool.PoolObjectFactory() { + @Override + public Particle createObject() { + return new GenericParticle<>(engine, new Oval(engine, width, height)); + } + }, min, max)); + } + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/shape/RectangleParticleSystem.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/shape/RectangleParticleSystem.java new file mode 100644 index 0000000..da5f9a0 --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particle/shape/RectangleParticleSystem.java @@ -0,0 +1,26 @@ +package com.nativegame.nattyengine.entity.particle.shape; + +import com.nativegame.nattyengine.engine.Engine; +import com.nativegame.nattyengine.entity.particle.GenericParticle; +import com.nativegame.nattyengine.entity.particle.GenericParticleSystem; +import com.nativegame.nattyengine.entity.particle.Particle; +import com.nativegame.nattyengine.entity.shape.geometry.Rectangle; +import com.nativegame.nattyengine.util.pool.Pool; +import com.nativegame.nattyengine.util.pool.SafeFixedObjectPool; + +public class RectangleParticleSystem extends GenericParticleSystem { + + public RectangleParticleSystem(Engine engine, int width, int height, int min) { + this(engine, width, height, min, min); + } + + public RectangleParticleSystem(Engine engine, int width, int height, int min, int max) { + super(engine, new SafeFixedObjectPool<>(new Pool.PoolObjectFactory() { + @Override + public Particle createObject() { + return new GenericParticle<>(engine, new Rectangle(engine, width, height)); + } + }, min, max)); + } + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/Particle.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/Particle.java deleted file mode 100644 index ea6090f..0000000 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/Particle.java +++ /dev/null @@ -1,112 +0,0 @@ -package com.nativegame.nattyengine.entity.particles; - -import com.nativegame.nattyengine.engine.Engine; -import com.nativegame.nattyengine.entity.particles.modifier.ParticleModifier; -import com.nativegame.nattyengine.entity.sprite.Sprite; -import com.nativegame.nattyengine.texture.Texture; - -import java.util.List; - -/** - * Created by Oscar Liang on 2022/12/11 - */ - -public class Particle extends Sprite { - - private final ParticleSystem mParent; - - private List mModifiers; - private float mSpeedX; - private float mSpeedY; - private float mAccelerationX; - private float mAccelerationY; - private float mRotationSpeed; - private long mDuration; - private long mTotalTime; - - //-------------------------------------------------------- - // Constructors - //-------------------------------------------------------- - public Particle(ParticleSystem particleSystem, Engine engine, Texture texture) { - super(engine, texture); - mParent = particleSystem; - } - //======================================================== - - //-------------------------------------------------------- - // Getter and Setter - //-------------------------------------------------------- - public void setSpeedX(float speedX) { - mSpeedX = speedX; - } - - public void setSpeedY(float speedY) { - mSpeedY = speedY; - } - - public void setAccelerationX(float accelerationX) { - mAccelerationX = accelerationX; - } - - public void setAccelerationY(float accelerationY) { - mAccelerationY = accelerationY; - } - - public void setRotationSpeed(float rotationSpeed) { - mRotationSpeed = rotationSpeed; - } - - public long getDuration() { - return mDuration; - } - - public void setDuration(long duration) { - mDuration = duration; - } - //======================================================== - - //-------------------------------------------------------- - // Overriding methods - //-------------------------------------------------------- - @Override - public void onRemove() { - mParent.returnToPool(this); - } - - @Override - public void onUpdate(long elapsedMillis) { - mTotalTime += elapsedMillis; - if (mTotalTime >= mDuration) { - removeFromGame(); - mTotalTime = 0; - } else { - mX += mSpeedX * elapsedMillis; - mY += mSpeedY * elapsedMillis; - mSpeedX += mAccelerationX * elapsedMillis; - mSpeedY += mAccelerationY * elapsedMillis; - mRotation += mRotationSpeed * elapsedMillis; - - // Update modifier - int size = mModifiers.size(); - for (int i = 0; i < size; i++) { - ParticleModifier modifier = mModifiers.get(i); - modifier.updateParticle(this, mTotalTime); - } - } - } - //======================================================== - - //-------------------------------------------------------- - // Methods - //-------------------------------------------------------- - public void activate(float x, float y, int layer, List modifiers) { - setCenterX(x); - setCenterY(y); - setLayer(layer); - mModifiers = modifiers; - addToGame(); - mTotalTime = 0; - } - //======================================================== - -} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/initializer/AccelerationXParticleInitializer.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/initializer/AccelerationXParticleInitializer.java deleted file mode 100644 index 81a7e2c..0000000 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/initializer/AccelerationXParticleInitializer.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.nativegame.nattyengine.entity.particles.initializer; - -import com.nativegame.nattyengine.entity.particles.Particle; -import com.nativegame.nattyengine.util.math.RandomUtils; - -/** - * Created by Oscar Liang on 2022/12/11 - */ - -public class AccelerationXParticleInitializer implements ParticleInitializer { - - private final float mMinAccelerationX; - private final float mMaxAccelerationX; - - //-------------------------------------------------------- - // Constructors - //-------------------------------------------------------- - public AccelerationXParticleInitializer(float minAccelerationX, float maxAccelerationX) { - mMinAccelerationX = minAccelerationX; - mMaxAccelerationX = maxAccelerationX; - } - //======================================================== - - //-------------------------------------------------------- - // Overriding methods - //-------------------------------------------------------- - @Override - public void initParticle(Particle particle) { - float value = RandomUtils.nextFloat(mMinAccelerationX, mMaxAccelerationX) / 1000; - particle.setAccelerationX(value); - } - //======================================================== - -} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/initializer/AccelerationYParticleInitializer.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/initializer/AccelerationYParticleInitializer.java deleted file mode 100644 index ecb4076..0000000 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/initializer/AccelerationYParticleInitializer.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.nativegame.nattyengine.entity.particles.initializer; - -import com.nativegame.nattyengine.entity.particles.Particle; -import com.nativegame.nattyengine.util.math.RandomUtils; - -/** - * Created by Oscar Liang on 2022/12/11 - */ - -public class AccelerationYParticleInitializer implements ParticleInitializer { - - private final float mMinAccelerationY; - private final float mMaxAccelerationY; - - //-------------------------------------------------------- - // Constructors - //-------------------------------------------------------- - public AccelerationYParticleInitializer(float minAccelerationY, float maxAccelerationY) { - mMinAccelerationY = minAccelerationY; - mMaxAccelerationY = maxAccelerationY; - } - //======================================================== - - //-------------------------------------------------------- - // Overriding methods - //-------------------------------------------------------- - @Override - public void initParticle(Particle particle) { - float value = RandomUtils.nextFloat(mMinAccelerationY, mMaxAccelerationY) / 1000; - particle.setAccelerationY(value); - } - //======================================================== - -} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/initializer/RotationParticleInitializer.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/initializer/RotationParticleInitializer.java deleted file mode 100644 index be328b2..0000000 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/initializer/RotationParticleInitializer.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.nativegame.nattyengine.entity.particles.initializer; - -import com.nativegame.nattyengine.entity.particles.Particle; -import com.nativegame.nattyengine.util.math.RandomUtils; - -/** - * Created by Oscar Liang on 2022/12/11 - */ - -public class RotationParticleInitializer implements ParticleInitializer { - - private final int mMinAngle; - private final int mMaxAngle; - - //-------------------------------------------------------- - // Constructors - //-------------------------------------------------------- - public RotationParticleInitializer(int minAngle, int maxAngle) { - mMinAngle = minAngle; - mMaxAngle = maxAngle; - } - //======================================================== - - //-------------------------------------------------------- - // Overriding methods - //-------------------------------------------------------- - @Override - public void initParticle(Particle particle) { - float value = RandomUtils.nextFloat(mMinAngle, mMaxAngle); - particle.setRotation(value); - } - //======================================================== - -} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/initializer/RotationSpeedParticleInitializer.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/initializer/RotationSpeedParticleInitializer.java deleted file mode 100644 index 0525006..0000000 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/initializer/RotationSpeedParticleInitializer.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.nativegame.nattyengine.entity.particles.initializer; - -import com.nativegame.nattyengine.entity.particles.Particle; -import com.nativegame.nattyengine.util.math.RandomUtils; - -/** - * Created by Oscar Liang on 2022/12/11 - */ - -public class RotationSpeedParticleInitializer implements ParticleInitializer { - - private final float mMinRotationSpeed; - private final float mMaxRotationSpeed; - - //-------------------------------------------------------- - // Constructors - //-------------------------------------------------------- - public RotationSpeedParticleInitializer(float minRotationSpeed, float maxRotationSpeed) { - mMinRotationSpeed = minRotationSpeed; - mMaxRotationSpeed = maxRotationSpeed; - } - //======================================================== - - //-------------------------------------------------------- - // Overriding methods - //-------------------------------------------------------- - @Override - public void initParticle(Particle particle) { - float value = RandomUtils.nextFloat(mMinRotationSpeed, mMaxRotationSpeed) / 1000; - particle.setRotationSpeed(value); - } - //======================================================== - -} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/initializer/SpeedParticleInitializer.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/initializer/SpeedParticleInitializer.java deleted file mode 100644 index c043afe..0000000 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/initializer/SpeedParticleInitializer.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.nativegame.nattyengine.entity.particles.initializer; - -import com.nativegame.nattyengine.entity.particles.Particle; -import com.nativegame.nattyengine.util.math.RandomUtils; - -/** - * Created by Oscar Liang on 2022/12/11 - */ - -public class SpeedParticleInitializer implements ParticleInitializer { - - private final float mMinSpeed; - private final float mMaxSpeed; - private final int mMinAngle; - private final int mMaxAngle; - - //-------------------------------------------------------- - // Constructors - //-------------------------------------------------------- - public SpeedParticleInitializer(float minSpeed, float maxSpeed, int minAngle, int maxAngle) { - mMinSpeed = minSpeed; - mMaxSpeed = maxSpeed; - mMinAngle = minAngle; - mMaxAngle = maxAngle; - } - //======================================================== - - //-------------------------------------------------------- - // Overriding methods - //-------------------------------------------------------- - @Override - public void initParticle(Particle particle) { - float speed = RandomUtils.nextFloat(mMinSpeed, mMaxSpeed); - float angle = RandomUtils.nextFloat(mMinAngle, mMaxAngle); - double angleInRads = Math.toRadians(angle); - float valueX = (float) (speed * Math.cos(angleInRads) / 1000); - float valueY = (float) (speed * Math.sin(angleInRads) / 1000); - particle.setSpeedX(valueX); - particle.setSpeedY(valueY); - } - //======================================================== - -} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/initializer/SpeedXParticleInitializer.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/initializer/SpeedXParticleInitializer.java deleted file mode 100644 index 38a999f..0000000 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/initializer/SpeedXParticleInitializer.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.nativegame.nattyengine.entity.particles.initializer; - -import com.nativegame.nattyengine.entity.particles.Particle; -import com.nativegame.nattyengine.util.math.RandomUtils; - -/** - * Created by Oscar Liang on 2022/12/11 - */ - -public class SpeedXParticleInitializer implements ParticleInitializer { - - private final float mMinSpeedX; - private final float mMaxSpeedX; - - //-------------------------------------------------------- - // Constructors - //-------------------------------------------------------- - public SpeedXParticleInitializer(float minSpeedX, float maxSpeedX) { - mMinSpeedX = minSpeedX; - mMaxSpeedX = maxSpeedX; - } - //======================================================== - - //-------------------------------------------------------- - // Overriding methods - //-------------------------------------------------------- - @Override - public void initParticle(Particle particle) { - float value = RandomUtils.nextFloat(mMinSpeedX, mMaxSpeedX) / 1000; - particle.setSpeedX(value); - } - //======================================================== - -} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/initializer/SpeedYParticleInitializer.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/initializer/SpeedYParticleInitializer.java deleted file mode 100644 index ff9f281..0000000 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/initializer/SpeedYParticleInitializer.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.nativegame.nattyengine.entity.particles.initializer; - -import com.nativegame.nattyengine.entity.particles.Particle; -import com.nativegame.nattyengine.util.math.RandomUtils; - -/** - * Created by Oscar Liang on 2022/12/11 - */ - -public class SpeedYParticleInitializer implements ParticleInitializer { - - private final float mMinSpeedY; - private final float mMaxSpeedY; - - //-------------------------------------------------------- - // Constructors - //-------------------------------------------------------- - public SpeedYParticleInitializer(float minSpeedY, float maxSpeedY) { - mMinSpeedY = minSpeedY; - mMaxSpeedY = maxSpeedY; - } - //======================================================== - - //-------------------------------------------------------- - // Overriding methods - //-------------------------------------------------------- - @Override - public void initParticle(Particle particle) { - float value = RandomUtils.nextFloat(mMinSpeedY, mMaxSpeedY) / 1000; - particle.setSpeedY(value); - } - //======================================================== - -} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/modifier/AlphaParticleModifier.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/modifier/AlphaParticleModifier.java deleted file mode 100644 index 0fcd438..0000000 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/modifier/AlphaParticleModifier.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.nativegame.nattyengine.entity.particles.modifier; - -import com.nativegame.nattyengine.entity.particles.Particle; - -/** - * Created by Oscar Liang on 2022/12/11 - */ - -public class AlphaParticleModifier implements ParticleModifier { - - private final float mStartValue; - private final float mRange; - private final long mStartDelay; - - //-------------------------------------------------------- - // Constructors - //-------------------------------------------------------- - public AlphaParticleModifier(float startValue, float endValue, long startDelay) { - mStartValue = startValue; - mRange = endValue - startValue; - mStartDelay = startDelay; - } - //======================================================== - - //-------------------------------------------------------- - // Overriding methods - //-------------------------------------------------------- - @Override - public void updateParticle(Particle particle, long elapsedMillis) { - if (elapsedMillis < mStartDelay) { - particle.setAlpha((int) mStartValue); - } else { - float percentage = (elapsedMillis - mStartDelay) * 1f / (particle.getDuration() - mStartDelay); - float value = mStartValue + mRange * percentage; - particle.setAlpha((int) value); - } - } - //======================================================== - -} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/modifier/RotationParticleModifier.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/modifier/RotationParticleModifier.java deleted file mode 100644 index 58a4628..0000000 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/particles/modifier/RotationParticleModifier.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.nativegame.nattyengine.entity.particles.modifier; - -import com.nativegame.nattyengine.entity.particles.Particle; - -/** - * Created by Oscar Liang on 2022/12/11 - */ - -public class RotationParticleModifier implements ParticleModifier { - - private final float mStartValue; - private final float mRange; - private final long mStartDelay; - - //-------------------------------------------------------- - // Constructors - //-------------------------------------------------------- - public RotationParticleModifier(float startValue, float endValue, long startDelay) { - mStartValue = startValue; - mRange = endValue - startValue; - mStartDelay = startDelay; - } - //======================================================== - - //-------------------------------------------------------- - // Overriding methods - //-------------------------------------------------------- - @Override - public void updateParticle(Particle particle, long elapsedMillis) { - if (elapsedMillis < mStartDelay) { - particle.setRotation(mStartValue); - } else { - float percentage = (elapsedMillis - mStartDelay) * 1f / (particle.getDuration() - mStartDelay); - float value = mStartValue + mRange * percentage; - particle.setRotation(value); - } - } - //======================================================== - -} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/primitive/Grid.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/primitive/Grid.java deleted file mode 100644 index d13e2c4..0000000 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/primitive/Grid.java +++ /dev/null @@ -1,106 +0,0 @@ -package com.nativegame.nattyengine.entity.primitive; - -import android.graphics.Canvas; - -import com.nativegame.nattyengine.engine.Engine; -import com.nativegame.nattyengine.engine.camera.Camera; -import com.nativegame.nattyengine.entity.shape.Shape; - -/** - * Created by Oscar Liang on 2022/12/11 - */ - -public class Grid extends Shape { - - protected float mX2; - protected float mY2; - - protected int mRow; - protected int mColumn; - - //-------------------------------------------------------- - // Constructors - //-------------------------------------------------------- - public Grid(Engine engine, int row, int column) { - this(engine, 0, 0, engine.getCamera().getWorldWidth(), engine.getCamera().getWorldHeight(), row, column); - } - - public Grid(Engine engine, float x, float y, float x2, float y2, int row, int column) { - super(engine, x, y); - mX2 = x2; - mY2 = y2; - mRow = row; - mColumn = column; - } - //======================================================== - - //-------------------------------------------------------- - // Getter and Setter - //-------------------------------------------------------- - public float getX2() { - return mX2; - } - - public void setX2(float x2) { - mX2 = x2; - } - - public float getY2() { - return mY2; - } - - public void setY2(float y2) { - mY2 = y2; - } - - public int getRow() { - return mRow; - } - - public void setRow(int row) { - mRow = row; - } - - public int getColumn() { - return mColumn; - } - - public void setColumn(int column) { - mColumn = column; - } - //======================================================== - - //-------------------------------------------------------- - // Overriding methods - //-------------------------------------------------------- - @Override - public void onDraw(Canvas canvas, Camera camera) { - // Draw vertical line - for (int i = 0; i < mColumn + 1; i++) { - float x = mX + i * (mX2 - mX) / mColumn; - float screenX = camera.getWorldToScreenX(x); - float screenY = camera.getWorldToScreenY(mY); - float screenY2 = camera.getWorldToScreenY(mY2); - canvas.drawLine(screenX, screenY, screenX, screenY2, mPaint); - } - - // Draw horizontal line - for (int i = 0; i < mRow + 1; i++) { - float y = mY + i * (mY2 - mY) / mRow; - float screenX = camera.getWorldToScreenX(mX); - float screenX2 = camera.getWorldToScreenX(mX2); - float screenY = camera.getWorldToScreenY(y); - canvas.drawLine(screenX, screenY, screenX2, screenY, mPaint); - } - } - - @Override - public boolean isCulling(Camera camera) { - return camera.getWorldToScreenX(mX) > camera.getScreenWidth() - || camera.getWorldToScreenY(mY) > camera.getScreenHeight() - || camera.getWorldToScreenX(mX2) < 0 - || camera.getWorldToScreenY(mY2) < 0; - } - //======================================================== - -} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/Circle.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/Circle.java deleted file mode 100644 index 63e1510..0000000 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/Circle.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.nativegame.nattyengine.entity.shape; - -import android.graphics.Canvas; - -import com.nativegame.nattyengine.engine.Engine; -import com.nativegame.nattyengine.engine.camera.Camera; - -/** - * Created by Oscar Liang on 2022/12/11 - */ - -public class Circle extends RectangleShape { - - protected int mRadius; - - //-------------------------------------------------------- - // Constructors - //-------------------------------------------------------- - public Circle(Engine engine, int radius) { - this(engine, 0, 0, radius); - } - - public Circle(Engine engine, float x, float y, int radius) { - super(engine, x, y, radius * 2, radius * 2); - mRadius = radius; - } - //======================================================== - - //-------------------------------------------------------- - // Getter and Setter - //-------------------------------------------------------- - public int getRadius() { - return mRadius; - } - - public void setRadius(int radius) { - mRadius = radius; - } - //======================================================== - - //-------------------------------------------------------- - // Overriding methods - //-------------------------------------------------------- - @Override - public void onDraw(Canvas canvas, Camera camera) { - float screenX = camera.getWorldToScreenX(getCenterX()); - float screenY = camera.getWorldToScreenY(getCenterY()); - canvas.drawCircle(screenX, screenY, mRadius * camera.getPixelFactor(), mPaint); - } - //======================================================== - -} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/Rectangle.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/Rectangle.java deleted file mode 100644 index 1768aaa..0000000 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/Rectangle.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.nativegame.nattyengine.entity.shape; - -import android.graphics.Canvas; - -import com.nativegame.nattyengine.engine.Engine; -import com.nativegame.nattyengine.engine.camera.Camera; - -/** - * Created by Oscar Liang on 2022/12/11 - */ - -public class Rectangle extends RectangleShape { - - protected int mRadiusX; - protected int mRadiusY; - - //-------------------------------------------------------- - // Constructors - //-------------------------------------------------------- - public Rectangle(Engine engine, int width, int height) { - this(engine, 0, 0, width, height, 0, 0); - } - - public Rectangle(Engine engine, int width, int height, int radius) { - this(engine, 0, 0, width, height, radius, radius); - } - - public Rectangle(Engine engine, int width, int height, int radiusX, int radiusY) { - this(engine, 0, 0, width, height, radiusX, radiusY); - } - - public Rectangle(Engine engine, float x, float y, int width, int height) { - this(engine, x, y, width, height, 0, 0); - } - - public Rectangle(Engine engine, float x, float y, int width, int height, int radius) { - this(engine, x, y, width, height, radius, radius); - } - - public Rectangle(Engine engine, float x, float y, int width, int height, int radiusX, int radiusY) { - super(engine, x, y, width, height); - mRadiusX = radiusX; - mRadiusY = radiusY; - } - //======================================================== - - //-------------------------------------------------------- - // Getter and Setter - //-------------------------------------------------------- - public int getRadiusX() { - return mRadiusX; - } - - public void setRadiusX(int radiusX) { - mRadiusX = radiusX; - } - - public int getRadiusY() { - return mRadiusY; - } - - public void setRadiusY(int radiusY) { - mRadiusY = radiusY; - } - //======================================================== - - //-------------------------------------------------------- - // Overriding methods - //-------------------------------------------------------- - @Override - public void onDraw(Canvas canvas, Camera camera) { - float screenLeft = camera.getWorldToScreenX(mX); - float screenTop = camera.getWorldToScreenY(mY); - float screenRight = camera.getWorldToScreenX(mX + mWidth); - float screenBottom = camera.getWorldToScreenY(mY + mHeight); - canvas.drawRoundRect(screenLeft, screenTop, screenRight, screenBottom, - mRadiusX * camera.getPixelFactor(), mRadiusY * camera.getPixelFactor(), mPaint); - } - //======================================================== - -} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/RectangleShape.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/RectangleShape.java index 6e23807..4f038a3 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/RectangleShape.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/RectangleShape.java @@ -1,22 +1,23 @@ package com.nativegame.nattyengine.entity.shape; +import android.graphics.Canvas; + +import com.nativegame.nattyengine.camera.Camera; import com.nativegame.nattyengine.engine.Engine; -import com.nativegame.nattyengine.engine.camera.Camera; -import com.nativegame.nattyengine.engine.collision.Collidable; -import com.nativegame.nattyengine.engine.collision.CollisionType; -import com.nativegame.nattyengine.engine.collision.hitbox.HitBox; +import com.nativegame.nattyengine.util.math.TransformUtils; + +import java.util.Arrays; /** * Created by Oscar Liang on 2022/12/11 */ -public abstract class RectangleShape extends Shape implements Collidable { +public abstract class RectangleShape extends Shape { - private HitBox mHitBox; - private CollisionType mCollisionType = CollisionType.NONE; + private int mWidth; + private int mHeight; - protected int mWidth; - protected int mHeight; + private final float[] mRotatePoints = new float[4]; //-------------------------------------------------------- // Constructors @@ -25,6 +26,8 @@ protected RectangleShape(Engine engine, float x, float y, int width, int height) super(engine, x, y); mWidth = width; mHeight = height; + resetScalePivot(); + resetRotationPivot(); } //======================================================== @@ -84,72 +87,92 @@ public void setEndY(float y) { // Overriding methods //-------------------------------------------------------- @Override - public HitBox getCollisionHitBox() { - return mHitBox; - } - - @Override - public void setCollisionHitBox(HitBox hitBox) { - mHitBox = hitBox; - } - - @Override - public CollisionType getCollisionType() { - return mCollisionType; - } - - @Override - public void setCollisionType(CollisionType type) { - mCollisionType = type; - } - - @Override - public void collide(Collidable collidable) { - if (!isRunning()) { - return; + public boolean isCulling(Canvas canvas, Camera camera) { + // We use the four edge of rectangle to check culling + float leftmostX; + float rightmostX; + float topmostY; + float bottommostY; + + // Apply scale + float scalePivotX = mX + getScalePivotX(); + float scalePivotY = mY + getScalePivotY(); + float x = TransformUtils.getScaleX(mX, scalePivotX, mScaleX); + float y = TransformUtils.getScaleY(mY, scalePivotY, mScaleY); + float endX = TransformUtils.getScaleX(getEndX(), scalePivotX, mScaleX); + float endY = TransformUtils.getScaleY(getEndY(), scalePivotY, mScaleY); + float rotationPivotX = TransformUtils.getScaleX(mX + getRotationPivotX(), scalePivotX, mScaleX); + float rotationPivotY = TransformUtils.getScaleY(mY + getRotationPivotY(), scalePivotY, mScaleY); + + // Apply rotation + float rotateLeftTopX = TransformUtils.getRotateX(x, y, rotationPivotX, rotationPivotY, mRotation); + float rotateLeftTopY = TransformUtils.getRotateY(x, y, rotationPivotX, rotationPivotY, mRotation); + float rotateRightTopX = TransformUtils.getRotateX(endX, y, rotationPivotX, rotationPivotY, mRotation); + float rotateRightTopY = TransformUtils.getRotateY(endX, y, rotationPivotX, rotationPivotY, mRotation); + float rotateLeftBottomX = TransformUtils.getRotateX(x, endY, rotationPivotX, rotationPivotY, mRotation); + float rotateLeftBottomY = TransformUtils.getRotateY(x, endY, rotationPivotX, rotationPivotY, mRotation); + float rotateRightBottomX = TransformUtils.getRotateX(endX, endY, rotationPivotX, rotationPivotY, mRotation); + float rotateRightBottomY = TransformUtils.getRotateY(endX, endY, rotationPivotX, rotationPivotY, mRotation); + + // Init a temp array to store all x value + mRotatePoints[0] = rotateLeftTopX; + mRotatePoints[1] = rotateRightTopX; + mRotatePoints[2] = rotateLeftBottomX; + mRotatePoints[3] = rotateRightBottomX; + + // Sort the array to get the smallest and largest value + Arrays.sort(mRotatePoints); + leftmostX = mRotatePoints[0]; + rightmostX = mRotatePoints[3]; + + // Init a temp array to store all y value + mRotatePoints[0] = rotateLeftTopY; + mRotatePoints[1] = rotateRightTopY; + mRotatePoints[2] = rotateLeftBottomY; + mRotatePoints[3] = rotateRightBottomY; + + // Sort the array to get the smallest and largest value + Arrays.sort(mRotatePoints); + topmostY = mRotatePoints[0]; + bottommostY = mRotatePoints[3]; + + // Check is all four vertex out of bound + boolean isCulling = camera.getWorldToScreenX(leftmostX, mCoordinateType) > canvas.getWidth() + || camera.getWorldToScreenY(topmostY, mCoordinateType) > canvas.getHeight() + || camera.getWorldToScreenX(rightmostX, mCoordinateType) < 0 + || camera.getWorldToScreenY(bottommostY, mCoordinateType) < 0; + + // Print debug info + if (!isCulling && mEngine.isDebugMode() && mEngine.getDebugger().isDebugCulling()) { + canvas.drawRect(camera.getWorldToScreenX(leftmostX, mCoordinateType), + camera.getWorldToScreenY(topmostY, mCoordinateType), + camera.getWorldToScreenX(rightmostX, mCoordinateType), + camera.getWorldToScreenY(bottommostY, mCoordinateType), + mEngine.getDebugger().getDebugPaint()); } - onCollision(collidable); - } - @Override - public void onPostUpdate(long elapsedMillis) { - if (mHitBox != null) { - // Update collision bound position - mHitBox.setCollisionBoundsPosition((int) getCenterX(), (int) getCenterY()); - } - } - - @Override - public boolean isCulling(Camera camera) { - return camera.getWorldToScreenX(mX) > camera.getScreenWidth() - || camera.getWorldToScreenY(mY) > camera.getScreenHeight() - || camera.getWorldToScreenX(getEndX()) < 0 - || camera.getWorldToScreenY(getEndY()) < 0; + return isCulling; } @Override public void reset() { super.reset(); - mCollisionType = CollisionType.NONE; - if (mHitBox != null) { - mHitBox.getCollisionBitmap().recycle(); - } - mHitBox = null; - } - - @Override - public void dispose() { - super.dispose(); - if (mHitBox != null) { - mHitBox.getCollisionBitmap().recycle(); - } + resetScalePivot(); + resetRotationPivot(); } //======================================================== //-------------------------------------------------------- // Methods //-------------------------------------------------------- - public void onCollision(Collidable collidable) { + public void resetScalePivot() { + setScalePivotX(getWidth() / 2f); + setScalePivotY(getHeight() / 2f); + } + + public void resetRotationPivot() { + setRotationPivotX(getWidth() / 2f); + setRotationPivotY(getHeight() / 2f); } //======================================================== diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/Shape.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/Shape.java index 7e813f5..6bdd8d8 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/Shape.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/Shape.java @@ -1,18 +1,35 @@ package com.nativegame.nattyengine.entity.shape; +import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.MaskFilter; import android.graphics.Paint; import android.graphics.Shader; +import com.nativegame.nattyengine.camera.Camera; import com.nativegame.nattyengine.engine.Engine; +import com.nativegame.nattyengine.entity.Manipulable; import com.nativegame.nattyengine.entity.ScreenEntity; /** * Created by Oscar Liang on 2022/12/11 */ -public abstract class Shape extends ScreenEntity { +public abstract class Shape extends ScreenEntity implements Manipulable { + + protected float mX; + protected float mY; + + protected float mScaleX = 1; + protected float mScaleY = 1; + protected float mRotation; + protected int mAlpha = 255; + protected Camera.CoordinateType mCoordinateType = Camera.CoordinateType.WORLD; + + private float mScalePivotX; + private float mScalePivotY; + private float mRotationPivotX; + private float mRotationPivotY; protected final Paint mPaint = new Paint(); @@ -20,7 +37,9 @@ public abstract class Shape extends ScreenEntity { // Constructors //-------------------------------------------------------- protected Shape(Engine engine, float x, float y) { - super(engine, x, y); + super(engine); + mX = x; + mY = y; } //======================================================== @@ -43,14 +62,6 @@ public void setColor(int color) { mPaint.setColor(color); } - public int getAlpha() { - return mPaint.getAlpha(); - } - - public void setAlpha(int alpha) { - mPaint.setAlpha(alpha); - } - public Paint.Style getStyle() { return mPaint.getStyle(); } @@ -91,10 +102,6 @@ public void setShader(Shader shader) { mPaint.setShader(shader); } - public void clearShader() { - mPaint.setShader(null); - } - public ColorFilter getColorFilter() { return mPaint.getColorFilter(); } @@ -103,10 +110,6 @@ public void setColorFilter(ColorFilter filter) { mPaint.setColorFilter(filter); } - public void clearColorFilter() { - mPaint.setColorFilter(null); - } - public MaskFilter getMaskFilter() { return mPaint.getMaskFilter(); } @@ -114,28 +117,170 @@ public MaskFilter getMaskFilter() { public void setMaskFilter(MaskFilter filter) { mPaint.setMaskFilter(filter); } + //======================================================== - public void clearMaskFilter() { - mPaint.setMaskFilter(null); + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + public Camera.CoordinateType getCoordinateType() { + return mCoordinateType; } - public void setShadow(float radius, float dx, float dy, int color) { - mPaint.setShadowLayer(mPixelFactor * radius, mPixelFactor * dx, mPixelFactor * dy, color); + @Override + public void setCoordinateType(Camera.CoordinateType type) { + mCoordinateType = type; } - public void clearShadow() { - mPaint.clearShadowLayer(); + @Override + public float getX() { + return mX; + } + + @Override + public void setX(float x) { + mX = x; + } + + @Override + public float getY() { + return mY; + } + + @Override + public void setY(float y) { + mY = y; + } + + @Override + public float getScaleX() { + if (mScaleX < 0) { + return 0; + } + return mScaleX; + } + + @Override + public void setScaleX(float scaleX) { + mScaleX = scaleX; + } + + @Override + public float getScaleY() { + if (mScaleY < 0) { + return 0; + } + return mScaleY; + } + + @Override + public void setScaleY(float scaleY) { + mScaleY = scaleY; + } + + @Override + public void setScale(float scale) { + mScaleX = scale; + mScaleY = scale; + } + + @Override + public float getRotation() { + return mRotation; + } + + @Override + public void setRotation(float rotation) { + mRotation = rotation; + } + + @Override + public int getAlpha() { + if (mAlpha < 0) { + return 0; + } + if (mAlpha > 255) { + return 255; + } + return mAlpha; + } + + @Override + public void setAlpha(int alpha) { + mAlpha = alpha; + } + + @Override + public float getRotationPivotX() { + return mRotationPivotX; + } + + @Override + public void setRotationPivotX(float rotationPivotX) { + mRotationPivotX = rotationPivotX; + } + + @Override + public float getRotationPivotY() { + return mRotationPivotY; + } + + @Override + public void setRotationPivotY(float rotationPivotY) { + mRotationPivotY = rotationPivotY; + } + + @Override + public float getScalePivotX() { + return mScalePivotX; + } + + @Override + public void setScalePivotX(float scalePivotX) { + mScalePivotX = scalePivotX; + } + + @Override + public float getScalePivotY() { + return mScalePivotY; + } + + @Override + public void setScalePivotY(float scalePivotY) { + mScalePivotY = scalePivotY; + } + + @Override + protected void onDraw(Canvas canvas, Camera camera) { + canvas.save(); + float realX = mX + (1 - mScaleX) * mScalePivotX; + float realY = mY + (1 - mScaleY) * mScalePivotY; + float scaleFactor = camera.getPixelFactor() * camera.getWorldToScreenZoom(mCoordinateType); + float scaleX = getScaleX() * scaleFactor; + float scaleY = getScaleY() * scaleFactor; + canvas.translate(camera.getWorldToScreenX(realX, mCoordinateType), camera.getWorldToScreenY(realY, mCoordinateType)); + canvas.rotate(mRotation, mRotationPivotX * scaleX, mRotationPivotY * scaleY); + canvas.scale(scaleX, scaleY); + mPaint.setAlpha(getAlpha()); + onDrawCanvas(canvas); + canvas.restore(); } - //======================================================== - //-------------------------------------------------------- - // Overriding methods - //-------------------------------------------------------- @Override public void reset() { super.reset(); + mScaleX = 1; + mScaleY = 1; + mRotation = 0; + mAlpha = 255; mPaint.reset(); } //======================================================== + //-------------------------------------------------------- + // Methods + //-------------------------------------------------------- + protected abstract void onDrawCanvas(Canvas canvas); + //======================================================== + } diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/geometry/Circle.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/geometry/Circle.java new file mode 100644 index 0000000..761060d --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/geometry/Circle.java @@ -0,0 +1,83 @@ +package com.nativegame.nattyengine.entity.shape.geometry; + +import android.graphics.Canvas; + +import com.nativegame.nattyengine.camera.Camera; +import com.nativegame.nattyengine.engine.Engine; +import com.nativegame.nattyengine.entity.shape.RectangleShape; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public class Circle extends RectangleShape { + + private int mRadius; + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + public Circle(Engine engine, int radius) { + this(engine, 0, 0, radius); + } + + public Circle(Engine engine, float x, float y, int radius) { + super(engine, x, y, radius * 2, radius * 2); + mRadius = radius; + } + //======================================================== + + //-------------------------------------------------------- + // Getter and Setter + //-------------------------------------------------------- + public int getRadius() { + return mRadius; + } + + public void setRadius(int radius) { + super.setWidth(radius * 2); + super.setHeight(radius * 2); + mRadius = radius; + } + //======================================================== + + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + public void setWidth(int width) { + setRadius(width / 2); + } + + @Override + public void setHeight(int height) { + setRadius(height / 2); + } + + @Override + public boolean isCulling(Canvas canvas, Camera camera) { + // Check is all four edges out of bound + boolean isCulling = camera.getWorldToScreenX(mX, mCoordinateType) > canvas.getWidth() + || camera.getWorldToScreenY(mY, mCoordinateType) > canvas.getHeight() + || camera.getWorldToScreenX(getEndX(), mCoordinateType) < 0 + || camera.getWorldToScreenY(getEndY(), mCoordinateType) < 0; + + // Print debug info + if (!isCulling && mEngine.isDebugMode() && mEngine.getDebugger().isDebugCulling()) { + canvas.drawRect(camera.getWorldToScreenX(mX, mCoordinateType), + camera.getWorldToScreenY(mY, mCoordinateType), + camera.getWorldToScreenX(getEndX(), mCoordinateType), + camera.getWorldToScreenY(getEndY(), mCoordinateType), + mEngine.getDebugger().getDebugPaint()); + } + + return isCulling; + } + + @Override + protected void onDrawCanvas(Canvas canvas) { + canvas.drawCircle(mRadius, mRadius, mRadius, mPaint); + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/Oval.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/geometry/Oval.java similarity index 57% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/Oval.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/geometry/Oval.java index e16b7fc..f4ab1a7 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/Oval.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/geometry/Oval.java @@ -1,9 +1,9 @@ -package com.nativegame.nattyengine.entity.shape; +package com.nativegame.nattyengine.entity.shape.geometry; import android.graphics.Canvas; import com.nativegame.nattyengine.engine.Engine; -import com.nativegame.nattyengine.engine.camera.Camera; +import com.nativegame.nattyengine.entity.shape.RectangleShape; /** * Created by Oscar Liang on 2022/12/11 @@ -11,20 +11,20 @@ public class Oval extends RectangleShape { - protected int mRadiusX; - protected int mRadiusY; + private int mRadiusX; + private int mRadiusY; //-------------------------------------------------------- // Constructors //-------------------------------------------------------- - public Oval(Engine engine, int width, int height) { - this(engine, 0, 0, width, height); + public Oval(Engine engine, int radiusX, int radiusY) { + this(engine, 0, 0, radiusX, radiusY); } - public Oval(Engine engine, float x, float y, int width, int height) { - super(engine, x, y, width, height); - mRadiusX = width / 2; - mRadiusY = height / 2; + public Oval(Engine engine, float x, float y, int radiusX, int radiusY) { + super(engine, x, y, radiusX * 2, radiusY * 2); + mRadiusX = radiusX; + mRadiusY = radiusY; } //======================================================== @@ -36,6 +36,7 @@ public int getRadiusX() { } public void setRadiusX(int radiusX) { + super.setWidth(radiusX * 2); mRadiusX = radiusX; } @@ -44,6 +45,7 @@ public int getRadiusY() { } public void setRadiusY(int radiusY) { + super.setHeight(radiusY * 2); mRadiusY = radiusY; } //======================================================== @@ -52,12 +54,18 @@ public void setRadiusY(int radiusY) { // Overriding methods //-------------------------------------------------------- @Override - public void onDraw(Canvas canvas, Camera camera) { - float screenLeft = camera.getWorldToScreenX(mX); - float screenTop = camera.getWorldToScreenY(mY); - float screenRight = camera.getWorldToScreenX(mX + mWidth); - float screenBottom = camera.getWorldToScreenY(mY + mHeight); - canvas.drawOval(screenLeft, screenTop, screenRight, screenBottom, mPaint); + public void setWidth(int width) { + setRadiusX(width / 2); + } + + @Override + public void setHeight(int height) { + setRadiusY(height / 2); + } + + @Override + protected void onDrawCanvas(Canvas canvas) { + canvas.drawOval(0, 0, getWidth(), getHeight(), mPaint); } //======================================================== diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/geometry/Rectangle.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/geometry/Rectangle.java new file mode 100644 index 0000000..9e05aa5 --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/geometry/Rectangle.java @@ -0,0 +1,35 @@ +package com.nativegame.nattyengine.entity.shape.geometry; + +import android.graphics.Canvas; + +import com.nativegame.nattyengine.engine.Engine; +import com.nativegame.nattyengine.entity.shape.RectangleShape; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public class Rectangle extends RectangleShape { + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + public Rectangle(Engine engine, int width, int height) { + this(engine, 0, 0, width, height); + } + + public Rectangle(Engine engine, float x, float y, int width, int height) { + super(engine, x, y, width, height); + } + //======================================================== + + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + protected void onDrawCanvas(Canvas canvas) { + canvas.drawRect(0, 0, getWidth(), getHeight(), mPaint); + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/primitive/Grid.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/primitive/Grid.java new file mode 100644 index 0000000..eacbe99 --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/primitive/Grid.java @@ -0,0 +1,80 @@ +package com.nativegame.nattyengine.entity.shape.primitive; + +import android.graphics.Canvas; + +import com.nativegame.nattyengine.engine.Engine; +import com.nativegame.nattyengine.entity.shape.RectangleShape; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public class Grid extends RectangleShape { + + protected int mRow; + protected int mColumn; + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + public Grid(Engine engine, int row, int column) { + this(engine, 0, 0, engine.getCamera().getWorldWidth(), engine.getCamera().getWorldHeight(), row, column); + } + + public Grid(Engine engine, float x, float y, int width, int height, int row, int column) { + super(engine, x, y, width, height); + mRow = row; + mColumn = column; + } + //======================================================== + + //-------------------------------------------------------- + // Getter and Setter + //-------------------------------------------------------- + public int getRow() { + return mRow; + } + + public void setRow(int row) { + mRow = row; + } + + public int getColumn() { + return mColumn; + } + + public void setColumn(int column) { + mColumn = column; + } + + public int getGridWidth() { + return getWidth() / mColumn; + } + + public int getGridHeight() { + return getHeight() / mRow; + } + //======================================================== + + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + protected void onDrawCanvas(Canvas canvas) { + // Draw vertical line + float gridWidth = getGridWidth(); + for (int i = 0; i <= mColumn; i++) { + float x = i * gridWidth; + canvas.drawLine(x, 0, x, getHeight(), mPaint); + } + + // Draw horizontal line + float gridHeight = getGridHeight(); + for (int i = 0; i <= mRow; i++) { + float y = i * gridHeight; + canvas.drawLine(0, y, getWidth(), y, mPaint); + } + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/primitive/Line.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/primitive/Line.java similarity index 62% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/primitive/Line.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/primitive/Line.java index 27ab2ab..24caad2 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/primitive/Line.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/primitive/Line.java @@ -1,9 +1,9 @@ -package com.nativegame.nattyengine.entity.primitive; +package com.nativegame.nattyengine.entity.shape.primitive; import android.graphics.Canvas; +import com.nativegame.nattyengine.camera.Camera; import com.nativegame.nattyengine.engine.Engine; -import com.nativegame.nattyengine.engine.camera.Camera; import com.nativegame.nattyengine.entity.shape.Shape; /** @@ -63,24 +63,20 @@ public float getYFromX(float x) { // Overriding methods //-------------------------------------------------------- @Override - public void onDraw(Canvas canvas, Camera camera) { - float screenX = camera.getWorldToScreenX(mX); - float screenY = camera.getWorldToScreenY(mY); - float screenX2 = camera.getWorldToScreenX(mX2); - float screenY2 = camera.getWorldToScreenY(mY2); - canvas.drawLine(screenX, screenY, screenX2, screenY2, mPaint); + public boolean isCulling(Canvas canvas, Camera camera) { + float yLeft = camera.getWorldToScreenX(getYFromX(0), mCoordinateType); + float yRight = camera.getWorldToScreenY(getYFromX(canvas.getWidth()), mCoordinateType); + return camera.getWorldToScreenX(Math.min(mX, mX2), mCoordinateType) > canvas.getWidth() + || camera.getWorldToScreenY(Math.min(mY, mY2), mCoordinateType) > canvas.getHeight() + || camera.getWorldToScreenX(Math.max(mX, mX2), mCoordinateType) < 0 + || camera.getWorldToScreenY(Math.max(mY, mY2), mCoordinateType) < 0 + || (yLeft > canvas.getHeight() && yRight > canvas.getHeight()) + || (yLeft < 0 && yRight < 0); } @Override - public boolean isCulling(Camera camera) { - float yLeft = camera.getWorldToScreenX(getYFromX(0)); - float yRight = camera.getWorldToScreenY(getYFromX(camera.getScreenWidth())); - return camera.getWorldToScreenX(Math.min(mX, mX2)) > camera.getScreenWidth() - || camera.getWorldToScreenY(Math.min(mY, mY2)) > camera.getScreenHeight() - || camera.getWorldToScreenX(Math.max(mX, mX2)) < 0 - || camera.getWorldToScreenY(Math.max(mY, mY2)) < 0 - || (yLeft > camera.getScreenHeight() && yRight > camera.getScreenHeight()) - || (yLeft < 0 && yRight < 0); + protected void onDrawCanvas(Canvas canvas) { + canvas.drawLine(0, 0, mX2 - mX, mY2 - mY, mPaint); } //======================================================== diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/primitive/Color.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/primitive/Plane.java similarity index 66% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/primitive/Color.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/primitive/Plane.java index ce77f2b..f606353 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/primitive/Color.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/primitive/Plane.java @@ -1,25 +1,26 @@ -package com.nativegame.nattyengine.entity.primitive; +package com.nativegame.nattyengine.entity.shape.primitive; import android.graphics.Canvas; import android.graphics.PorterDuff; +import com.nativegame.nattyengine.camera.Camera; import com.nativegame.nattyengine.engine.Engine; -import com.nativegame.nattyengine.engine.camera.Camera; import com.nativegame.nattyengine.entity.ScreenEntity; /** * Created by Oscar Liang on 2022/12/11 */ -public class Color extends ScreenEntity { +public class Plane extends ScreenEntity { protected int mColor; + protected PorterDuff.Mode mColorMode = PorterDuff.Mode.SRC_ATOP; //-------------------------------------------------------- // Constructors //-------------------------------------------------------- - public Color(Engine engine, int color) { - super(engine, 0, 0); + public Plane(Engine engine, int color) { + super(engine); mColor = color; } //======================================================== @@ -34,20 +35,28 @@ public int getColor() { public void setColor(int color) { mColor = color; } + + public PorterDuff.Mode getColorMode() { + return mColorMode; + } + + public void setColorMode(PorterDuff.Mode mode) { + mColorMode = mode; + } //======================================================== //-------------------------------------------------------- // Overriding methods //-------------------------------------------------------- @Override - public void onDraw(Canvas canvas, Camera camera) { - canvas.drawColor(mColor, PorterDuff.Mode.SRC_ATOP); + public boolean isCulling(Canvas canvas, Camera camera) { + // Plane is always on screen + return false; } @Override - public boolean isCulling(Camera camera) { - // Color Bg is always on screen - return false; + public void onDraw(Canvas canvas, Camera camera) { + canvas.drawColor(mColor, mColorMode); } //======================================================== diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/primitive/Point.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/primitive/Point.java similarity index 55% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/primitive/Point.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/primitive/Point.java index 1be5a4a..5eb10f8 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/primitive/Point.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/shape/primitive/Point.java @@ -1,9 +1,9 @@ -package com.nativegame.nattyengine.entity.primitive; +package com.nativegame.nattyengine.entity.shape.primitive; import android.graphics.Canvas; +import com.nativegame.nattyengine.camera.Camera; import com.nativegame.nattyengine.engine.Engine; -import com.nativegame.nattyengine.engine.camera.Camera; import com.nativegame.nattyengine.entity.shape.Shape; /** @@ -24,18 +24,16 @@ public Point(Engine engine, float x, float y) { // Overriding methods //-------------------------------------------------------- @Override - public void onDraw(Canvas canvas, Camera camera) { - float screenX = camera.getWorldToScreenX(mX); - float screenY = camera.getWorldToScreenY(mY); - canvas.drawPoint(screenX, screenY, mPaint); + public boolean isCulling(Canvas canvas, Camera camera) { + return camera.getWorldToScreenX(mX, mCoordinateType) > canvas.getWidth() + || camera.getWorldToScreenY(mY, mCoordinateType) > canvas.getHeight() + || camera.getWorldToScreenX(mX, mCoordinateType) < 0 + || camera.getWorldToScreenY(mY, mCoordinateType) < 0; } @Override - public boolean isCulling(Camera camera) { - return camera.getWorldToScreenX(mX) > camera.getScreenWidth() - || camera.getWorldToScreenY(mY) > camera.getScreenHeight() - || camera.getWorldToScreenX(mX) < 0 - || camera.getWorldToScreenY(mY) < 0; + protected void onDrawCanvas(Canvas canvas) { + canvas.drawPoint(0, 0, mPaint); } //======================================================== diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/animation/AnimatedSprite.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/AnimateSprite.java similarity index 56% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/animation/AnimatedSprite.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/AnimateSprite.java index aab24e9..266214c 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/animation/AnimatedSprite.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/AnimateSprite.java @@ -1,19 +1,19 @@ -package com.nativegame.nattyengine.entity.sprite.animation; +package com.nativegame.nattyengine.entity.sprite; import com.nativegame.nattyengine.engine.Engine; -import com.nativegame.nattyengine.entity.sprite.TileSprite; import com.nativegame.nattyengine.texture.Texture; import com.nativegame.nattyengine.texture.TextureGroup; +import com.nativegame.nattyengine.util.math.MathUtils; /** * Created by Oscar Liang on 2022/12/11 */ -public class AnimatedSprite extends TileSprite { +public class AnimateSprite extends FrameSprite { private AnimationListener mListener; - private int mStartIndex; - private int mEndIndex; + private int mStartFrameIndex; + private int mEndFrameIndex; private long mTotalTime; private boolean mIsAutoStart = true; private boolean mIsAutoRemove = false; @@ -24,30 +24,30 @@ public class AnimatedSprite extends TileSprite { //-------------------------------------------------------- // Constructors //-------------------------------------------------------- - public AnimatedSprite(Engine engine, TextureGroup textureGroup) { + public AnimateSprite(Engine engine, TextureGroup textureGroup) { this(engine, 0, 0, textureGroup, 0, textureGroup.getTextureCount() - 1); } - public AnimatedSprite(Engine engine, TextureGroup textureGroup, int startIndex) { - this(engine, 0, 0, textureGroup, startIndex, textureGroup.getTextureCount() - 1); + public AnimateSprite(Engine engine, TextureGroup textureGroup, int startFrameIndex) { + this(engine, 0, 0, textureGroup, startFrameIndex, textureGroup.getTextureCount() - 1); } - public AnimatedSprite(Engine engine, TextureGroup textureGroup, int startIndex, int endIndex) { - this(engine, 0, 0, textureGroup, startIndex, endIndex); + public AnimateSprite(Engine engine, TextureGroup textureGroup, int startFrameIndex, int endFrameIndex) { + this(engine, 0, 0, textureGroup, startFrameIndex, endFrameIndex); } - public AnimatedSprite(Engine engine, float x, float y, TextureGroup textureGroup) { + public AnimateSprite(Engine engine, float x, float y, TextureGroup textureGroup) { this(engine, x, y, textureGroup, 0, textureGroup.getTextureCount() - 1); } - public AnimatedSprite(Engine engine, float x, float y, TextureGroup textureGroup, int startIndex) { - this(engine, x, y, textureGroup, startIndex, textureGroup.getTextureCount() - 1); + public AnimateSprite(Engine engine, float x, float y, TextureGroup textureGroup, int startFrameIndex) { + this(engine, x, y, textureGroup, startFrameIndex, textureGroup.getTextureCount() - 1); } - public AnimatedSprite(Engine engine, float x, float y, TextureGroup textureGroup, int startIndex, int endIndex) { - super(engine, x, y, textureGroup, startIndex); - mStartIndex = startIndex; - mEndIndex = endIndex; + public AnimateSprite(Engine engine, float x, float y, TextureGroup textureGroup, int startFrameIndex, int endFrameIndex) { + super(engine, x, y, textureGroup, startFrameIndex); + mStartFrameIndex = startFrameIndex; + mEndFrameIndex = endFrameIndex; } //======================================================== @@ -62,20 +62,20 @@ public void setAnimationListener(AnimationListener listener) { mListener = listener; } - public int getStartIndex() { - return mStartIndex; + public int getStartFrameIndex() { + return mStartFrameIndex; } - public void setStartIndex(int startIndex) { - mStartIndex = startIndex; + public void setStartFrameIndex(int startFrameIndex) { + mStartFrameIndex = startFrameIndex; } - public int getEndIndex() { - return mEndIndex; + public int getEndFrameIndex() { + return mEndFrameIndex; } - public void setEndIndex(int endIndex) { - mEndIndex = endIndex; + public void setEndFrameIndex(int endFrameIndex) { + mEndFrameIndex = endFrameIndex; } public boolean isAnimationAutoStart() { @@ -98,10 +98,6 @@ public boolean isPlaying() { return mIsPlaying; } - public Animation getAnimation() { - return mAnimation; - } - public void setAnimation(long frameDuration) { mAnimation.set(frameDuration, mTextureGroup.getTextureCount()); } @@ -139,14 +135,14 @@ public void addToGame() { } @Override - public void onPreUpdate(long elapsedMillis) { + protected void onPreUpdate(long elapsedMillis) { if (!mIsPlaying) { return; } // Calculate animation duration long duration = 0; - for (int i = mStartIndex; i <= mEndIndex; i++) { + for (int i = mStartFrameIndex; i <= mEndFrameIndex; i++) { duration += mAnimation.getFrameDurations()[i]; } @@ -166,10 +162,10 @@ public void onPreUpdate(long elapsedMillis) { // Update animation frame long animationElapsedTime = 0; - for (int i = mStartIndex; i <= mEndIndex; i++) { + for (int i = mStartFrameIndex; i <= mEndFrameIndex; i++) { animationElapsedTime += mAnimation.getFrameDurations()[i]; if (animationElapsedTime > mTotalTime) { - mCurrentIndex = i; + mCurrentFrameIndex = i; break; } } @@ -177,7 +173,7 @@ public void onPreUpdate(long elapsedMillis) { @Override public void reset() { - mCurrentIndex = mStartIndex; + mCurrentFrameIndex = mStartFrameIndex; super.reset(); stopAnimation(); mIsAutoStart = true; @@ -193,7 +189,7 @@ public void startAnimation() { if (mIsPlaying) { return; } - mCurrentIndex = mStartIndex; + mCurrentFrameIndex = mStartFrameIndex; mIsPlaying = true; mTotalTime = 0; if (mListener != null) { @@ -234,22 +230,93 @@ public void resumeAnimation() { mListener.onAnimationResume(); } } + + public void resetAnimation() { + mCurrentFrameIndex = mStartFrameIndex; + mTotalTime = 0; + if (mListener != null) { + mListener.onAnimationReset(); + } + } //======================================================== //-------------------------------------------------------- // Inner Classes //-------------------------------------------------------- + private static class Animation { + + private long[] mFrameDurations; + private long mDuration; + private int mFrameCount; + private boolean mIsLooping; + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + public Animation() { + } + //======================================================== + + //-------------------------------------------------------- + // Getter and Setter + //-------------------------------------------------------- + public long[] getFrameDurations() { + return mFrameDurations; + } + + public long getDuration() { + return mDuration; + } + + public int getFrameCount() { + return mFrameCount; + } + + public boolean isLooping() { + return mIsLooping; + } + + public void set(long frameDuration, int frameCount) { + set(MathUtils.fill(frameDuration, frameCount), false); + } + + public void set(long frameDuration, int frameCount, boolean looping) { + set(MathUtils.fill(frameDuration, frameCount), looping); + } + + public void set(long[] frameDurations) { + set(frameDurations, false); + } + + public void set(long[] frameDurations, boolean looping) { + mFrameDurations = frameDurations; + mDuration = MathUtils.sum(frameDurations); + mFrameCount = frameDurations.length; + mIsLooping = looping; + } + //======================================================== + + } + public interface AnimationListener { - void onAnimationStart(); + default void onAnimationStart() { + } - void onAnimationStop(); + default void onAnimationStop() { + } - void onAnimationPause(); + default void onAnimationPause() { + } - void onAnimationResume(); + default void onAnimationResume() { + } + + default void onAnimationReset() { + } - void onAnimationRepeat(); + default void onAnimationRepeat() { + } } //======================================================== diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/ButtonSprite.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/ButtonSprite.java new file mode 100644 index 0000000..f2eb894 --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/ButtonSprite.java @@ -0,0 +1,120 @@ +package com.nativegame.nattyengine.entity.sprite; + +import com.nativegame.nattyengine.engine.Engine; +import com.nativegame.nattyengine.input.touch.BoundTouchEventListener; +import com.nativegame.nattyengine.input.touch.TouchEvent; +import com.nativegame.nattyengine.input.touch.TouchEventListener; +import com.nativegame.nattyengine.texture.Texture; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public class ButtonSprite extends Sprite implements BoundTouchEventListener, TouchEventListener { + + private ButtonListener mListener; + private ButtonState mState = ButtonState.ENABLE; + + public enum ButtonState { + ENABLE, + DISABLE, + PRESSED + } + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + public ButtonSprite(Engine engine, Texture texture) { + super(engine, texture); + } + + public ButtonSprite(Engine engine, float x, float y, Texture texture) { + super(engine, x, y, texture); + } + //======================================================== + + //-------------------------------------------------------- + // Getter and Setter + //-------------------------------------------------------- + public ButtonListener getButtonListener() { + return mListener; + } + + public void setButtonListener(ButtonListener listener) { + mListener = listener; + } + + public ButtonState getButtonState() { + return mState; + } + + public void setButtonState(ButtonState state) { + mState = state; + onUpdateButtonState(); + } + //======================================================== + + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + public void onAreaTouchEvent(int type, float relativeTouchX, float relativeTouchY) { + if (mState == ButtonState.DISABLE) { + return; + } + if (type == TouchEvent.TOUCH_DOWN) { + mState = ButtonState.PRESSED; + onUpdateButtonState(); + } + } + + @Override + public void onTouchEvent(int type, float touchX, float touchY) { + if (mState == ButtonState.DISABLE) { + return; + } + // Check the touch up event when pressed state to prevent + // pressing the button in bounds and release somewhere else + if (type == TouchEvent.TOUCH_UP && mState == ButtonState.PRESSED) { + mState = ButtonState.ENABLE; + onUpdateButtonState(); + } + } + //======================================================== + + //-------------------------------------------------------- + // Methods + //-------------------------------------------------------- + protected void onUpdateButtonState() { + if (mListener == null) { + return; + } + switch (mState) { + case ENABLE: + mListener.onEnableButton(this); + break; + case DISABLE: + mListener.onDisableButton(this); + break; + case PRESSED: + mListener.onPressedButton(this); + break; + } + } + //======================================================== + + //-------------------------------------------------------- + // Inner Classes + //-------------------------------------------------------- + public interface ButtonListener { + + void onEnableButton(ButtonSprite button); + + void onDisableButton(ButtonSprite button); + + void onPressedButton(ButtonSprite button); + + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/TileSprite.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/FrameSprite.java similarity index 59% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/TileSprite.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/FrameSprite.java index d87c276..6f84d5d 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/TileSprite.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/FrameSprite.java @@ -2,8 +2,8 @@ import android.graphics.Canvas; +import com.nativegame.nattyengine.camera.Camera; import com.nativegame.nattyengine.engine.Engine; -import com.nativegame.nattyengine.engine.camera.Camera; import com.nativegame.nattyengine.texture.Texture; import com.nativegame.nattyengine.texture.TextureGroup; @@ -11,30 +11,30 @@ * Created by Oscar Liang on 2022/12/11 */ -public class TileSprite extends Sprite { +public class FrameSprite extends Sprite { protected TextureGroup mTextureGroup; - protected int mCurrentIndex; + protected int mCurrentFrameIndex; //-------------------------------------------------------- // Constructors //-------------------------------------------------------- - public TileSprite(Engine engine, TextureGroup textureGroup) { + public FrameSprite(Engine engine, TextureGroup textureGroup) { this(engine, 0, 0, textureGroup, 0); } - public TileSprite(Engine engine, TextureGroup textureGroup, int startIndex) { - this(engine, 0, 0, textureGroup, startIndex); + public FrameSprite(Engine engine, TextureGroup textureGroup, int startFrameIndex) { + this(engine, 0, 0, textureGroup, startFrameIndex); } - public TileSprite(Engine engine, float x, float y, TextureGroup textureGroup) { + public FrameSprite(Engine engine, float x, float y, TextureGroup textureGroup) { this(engine, x, y, textureGroup, 0); } - public TileSprite(Engine engine, float x, float y, TextureGroup textureGroup, int startIndex) { - super(engine, x, y, textureGroup.getTextures().get(startIndex)); + public FrameSprite(Engine engine, float x, float y, TextureGroup textureGroup, int startFrameIndex) { + super(engine, x, y, textureGroup.getTextures().get(startFrameIndex)); mTextureGroup = textureGroup; - mCurrentIndex = startIndex; + mCurrentFrameIndex = startFrameIndex; } //======================================================== @@ -49,12 +49,12 @@ public void setTextureGroup(TextureGroup textureGroup) { mTextureGroup = textureGroup; } - public int getCurrentIndex() { - return mCurrentIndex; + public int getCurrentFrameIndex() { + return mCurrentFrameIndex; } - public void setCurrentIndex(int currentIndex) { - mCurrentIndex = currentIndex; + public void setCurrentFrameIndex(int currentFrameIndex) { + mCurrentFrameIndex = currentFrameIndex; } //======================================================== @@ -62,14 +62,14 @@ public void setCurrentIndex(int currentIndex) { // Overriding methods //-------------------------------------------------------- @Override - public void onPreDraw(Canvas canvas, Camera camera) { - mTexture = mTextureGroup.getTextures().get(mCurrentIndex); + protected void onPreDraw(Canvas canvas, Camera camera) { + mTexture = mTextureGroup.getTextures().get(mCurrentFrameIndex); } @Override public void reset() { super.reset(); - mTexture = mTextureGroup.getTextures().get(mCurrentIndex); + mTexture = mTextureGroup.getTextures().get(mCurrentFrameIndex); } //======================================================== diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/Sprite.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/Sprite.java index 4b95335..183a397 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/Sprite.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/Sprite.java @@ -1,10 +1,8 @@ package com.nativegame.nattyengine.entity.sprite; import android.graphics.Canvas; -import android.graphics.Matrix; import com.nativegame.nattyengine.engine.Engine; -import com.nativegame.nattyengine.engine.camera.Camera; import com.nativegame.nattyengine.entity.shape.RectangleShape; import com.nativegame.nattyengine.texture.Texture; @@ -15,17 +13,6 @@ public class Sprite extends RectangleShape { protected Texture mTexture; - protected float mScaleX = 1; - protected float mScaleY = 1; - protected float mRotation; - protected float mAlpha = 255; - - private float mScalePivotX; - private float mScalePivotY; - private float mRotationPivotX; - private float mRotationPivotY; - - private final Matrix mMatrix = new Matrix(); //-------------------------------------------------------- // Constructors @@ -37,8 +24,6 @@ public Sprite(Engine engine, Texture texture) { public Sprite(Engine engine, float x, float y, Texture texture) { super(engine, x, y, texture.getWidth(), texture.getHeight()); mTexture = texture; - resetScalePivot(); - resetRotationPivot(); } //======================================================== @@ -51,140 +36,19 @@ public Texture getTexture() { public void setTexture(Texture texture) { mTexture = texture; - resetSize(); + setWidth(mTexture.getWidth()); + setHeight(mTexture.getHeight()); resetScalePivot(); resetRotationPivot(); } - - public float getScaleX() { - if (mScaleX < 0) { - return 0; - } - return mScaleX; - } - - public void setScaleX(float scaleX) { - mScaleX = scaleX; - } - - public float getScaleY() { - if (mScaleY < 0) { - return 0; - } - return mScaleY; - } - - public void setScaleY(float scaleY) { - mScaleY = scaleY; - } - - public void setScale(float scale) { - mScaleX = scale; - mScaleY = scale; - } - - public float getRotation() { - return mRotation; - } - - public void setRotation(float rotation) { - mRotation = rotation; - } - - public float getRotationPivotX() { - return mRotationPivotX; - } - - public void setRotationPivotX(float rotationPivotX) { - mRotationPivotX = rotationPivotX; - } - - public float getRotationPivotY() { - return mRotationPivotY; - } - - public void setRotationPivotY(float rotationPivotY) { - mRotationPivotY = rotationPivotY; - } - - public float getScalePivotX() { - return mScalePivotX; - } - - public void setScalePivotX(float scalePivotX) { - mScalePivotX = scalePivotX; - } - - public float getScalePivotY() { - return mScalePivotY; - } - - public void setScalePivotY(float scalePivotY) { - mScalePivotY = scalePivotY; - } //======================================================== //-------------------------------------------------------- // Overriding methods //-------------------------------------------------------- @Override - public int getAlpha() { - if (mAlpha < 0) { - return 0; - } - if (mAlpha > 255) { - return 255; - } - return (int) mAlpha; - } - - @Override - public void setAlpha(int alpha) { - mAlpha = alpha; - } - - @Override - public void onDraw(Canvas canvas, Camera camera) { - float offsetX = (1 - mScaleX) * mScalePivotX; - float offsetY = (1 - mScaleY) * mScalePivotY; - mMatrix.reset(); - mMatrix.postScale(getScaleX() * camera.getPixelFactor(), getScaleY() * camera.getPixelFactor()); - mMatrix.postTranslate(camera.getWorldToScreenX(mX + offsetX), camera.getWorldToScreenY(mY + offsetY)); - mMatrix.postRotate(mRotation, camera.getWorldToScreenX(mX + mRotationPivotX), camera.getWorldToScreenY(mY + mRotationPivotY)); - mPaint.setAlpha(getAlpha()); - canvas.drawBitmap(mTexture.getBitmap(), mMatrix, mPaint); - } - - @Override - public void reset() { - super.reset(); - mScaleX = 1; - mScaleY = 1; - mRotation = 0; - mAlpha = 255; - mMatrix.reset(); - resetSize(); - resetScalePivot(); - resetRotationPivot(); - } - //======================================================== - - //-------------------------------------------------------- - // Methods - //-------------------------------------------------------- - public void resetSize() { - mWidth = mTexture.getWidth(); - mHeight = mTexture.getHeight(); - } - - public void resetScalePivot() { - mScalePivotX = mWidth / 2f; - mScalePivotY = mHeight / 2f; - } - - public void resetRotationPivot() { - mRotationPivotX = mWidth / 2f; - mRotationPivotY = mHeight / 2f; + protected void onDrawCanvas(Canvas canvas) { + canvas.drawBitmap(mTexture.getBitmap(), 0, 0, mPaint); } //======================================================== diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/animation/Animation.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/animation/Animation.java deleted file mode 100644 index dede46a..0000000 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/animation/Animation.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.nativegame.nattyengine.entity.sprite.animation; - -import com.nativegame.nattyengine.util.math.MathUtils; - -/** - * Created by Oscar Liang on 2022/12/11 - */ - -public class Animation { - - private long[] mFrameDurations; - private long mDuration; - private int mFrameCount; - private boolean mIsLooping; - - //-------------------------------------------------------- - // Constructors - //-------------------------------------------------------- - public Animation() { - } - //======================================================== - - //-------------------------------------------------------- - // Getter and Setter - //-------------------------------------------------------- - public long[] getFrameDurations() { - return mFrameDurations; - } - - public long getDuration() { - return mDuration; - } - - public int getFrameCount() { - return mFrameCount; - } - - public boolean isLooping() { - return mIsLooping; - } - - public void set(long frameDuration, int frameCount) { - set(MathUtils.fill(frameDuration, frameCount), false); - } - - public void set(long frameDuration, int frameCount, boolean looping) { - set(MathUtils.fill(frameDuration, frameCount), looping); - } - - public void set(long[] frameDurations) { - set(frameDurations, false); - } - - public void set(long[] frameDurations, boolean looping) { - mFrameDurations = frameDurations; - mDuration = MathUtils.sum(mFrameDurations); - mFrameCount = mFrameDurations.length; - mIsLooping = looping; - } - //======================================================== - -} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/SingleValueSpriteModifier.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/SingleValueSpriteModifier.java deleted file mode 100644 index 1a97ba1..0000000 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/sprite/modifier/SingleValueSpriteModifier.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.nativegame.nattyengine.entity.sprite.modifier; - -import com.nativegame.nattyengine.entity.modifier.SingleValueModifier; -import com.nativegame.nattyengine.entity.modifier.tween.LinearTweener; -import com.nativegame.nattyengine.entity.modifier.tween.Tweener; -import com.nativegame.nattyengine.entity.sprite.Sprite; - -/** - * Created by Oscar Liang on 2022/12/11 - */ - -public abstract class SingleValueSpriteModifier extends SingleValueModifier { - - //-------------------------------------------------------- - // Constructors - //-------------------------------------------------------- - protected SingleValueSpriteModifier(float startValue, float endValue, long duration) { - super(startValue, endValue, duration, 0, LinearTweener.getInstance()); - } - - protected SingleValueSpriteModifier(float startValue, float endValue, long duration, long startDelay) { - super(startValue, endValue, duration, startDelay, LinearTweener.getInstance()); - } - - protected SingleValueSpriteModifier(float startValue, float endValue, long duration, Tweener tweener) { - super(startValue, endValue, duration, 0, tweener); - } - - protected SingleValueSpriteModifier(float startValue, float endValue, long duration, long startDelay, Tweener tweener) { - super(startValue, endValue, duration, startDelay, tweener); - } - //======================================================== - -} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/text/Text.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/text/Text.java index ab1d545..6721afa 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/text/Text.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/text/Text.java @@ -1,12 +1,12 @@ package com.nativegame.nattyengine.entity.text; import android.graphics.Canvas; -import android.graphics.Paint; +import android.graphics.Rect; import android.graphics.Typeface; import com.nativegame.nattyengine.engine.Engine; -import com.nativegame.nattyengine.engine.camera.Camera; import com.nativegame.nattyengine.entity.shape.RectangleShape; +import com.nativegame.nattyengine.util.exception.EngineRuntimeException; /** * Created by Oscar Liang on 2022/12/11 @@ -15,12 +15,32 @@ public class Text extends RectangleShape { private String mText; + private TextHorizontalAlign mHorizontalAlign = TextHorizontalAlign.CENTER; + private TextVerticalAlign mVerticalAlign = TextVerticalAlign.CENTER; + + private final Rect mTextBound = new Rect(-1, -1, -1, -1); + + public enum TextHorizontalAlign { + LEFT, + CENTER, + RIGHT + } + + public enum TextVerticalAlign { + TOP, + CENTER, + BOTTOM + } //-------------------------------------------------------- // Constructors //-------------------------------------------------------- - public Text(Engine engine, String text) { - this(engine, 0, 0, 0, 0, text); + public Text(Engine engine, int width, int height) { + this(engine, 0, 0, width, height, ""); + } + + public Text(Engine engine, float x, float y, int width, int height) { + this(engine, x, y, width, height, ""); } public Text(Engine engine, int width, int height, String text) { @@ -44,20 +64,28 @@ public void setText(String text) { mText = text; } - public float getTextSize() { - return mPaint.getTextSize(); + public TextHorizontalAlign getTextHorizontalAlign() { + return mHorizontalAlign; } - public void setTextSize(float textSize) { - mPaint.setTextSize(mPixelFactor * textSize); + public void setTextHorizontalAlign(TextHorizontalAlign horizontalAlign) { + mHorizontalAlign = horizontalAlign; } - public Paint.Align getTextAlign() { - return mPaint.getTextAlign(); + public TextVerticalAlign getTextVerticalAlign() { + return mVerticalAlign; } - public void setTextAlign(Paint.Align align) { - mPaint.setTextAlign(align); + public void setTextVerticalAlign(TextVerticalAlign verticalAlign) { + mVerticalAlign = verticalAlign; + } + + public float getTextSize() { + return mPaint.getTextSize(); + } + + public void setTextSize(float textSize) { + mPaint.setTextSize(textSize); } public Typeface getTextTypeface() { @@ -67,16 +95,84 @@ public Typeface getTextTypeface() { public void setTextTypeface(Typeface typeface) { mPaint.setTypeface(typeface); } + + public float getTextX() { + switch (mHorizontalAlign) { + case LEFT: + return 0; + case CENTER: + return getWidth() / 2f - getTextWidth() / 2f; + case RIGHT: + return getWidth() - getTextWidth(); + default: + throw new EngineRuntimeException("HorizontalAlign not found!"); + } + } + + public float getTextY() { + switch (mVerticalAlign) { + case TOP: + return 0; + case CENTER: + return getHeight() / 2f - getTextHeight() / 2f; + case BOTTOM: + return getHeight() - getTextHeight(); + default: + throw new EngineRuntimeException("VerticalAlign not found!"); + } + } + + public int getTextWidth() { + String[] texts = mText.split("\n"); + // Find the line with the largest width + int maxTextWidth = 0; + for (String text : texts) { + mPaint.getTextBounds(text, 0, text.length(), mTextBound); + if (mTextBound.width() > maxTextWidth) { + maxTextWidth = mTextBound.width(); + } + } + return maxTextWidth; + } + + public int getTextHeight() { + String[] texts = mText.split("\n"); + int lineCount = texts.length; + mPaint.getTextBounds(mText, 0, mText.length(), mTextBound); + return lineCount * mTextBound.height(); + } //======================================================== //-------------------------------------------------------- // Overriding methods //-------------------------------------------------------- @Override - public void onDraw(Canvas canvas, Camera camera) { - float screenX = camera.getWorldToScreenX(getCenterX()); - float screenY = camera.getWorldToScreenY(getCenterY()); - canvas.drawText(mText, screenX, screenY, mPaint); + protected void onDrawCanvas(Canvas canvas) { + // Check if text has next line sign and divide it + String[] texts = mText.split("\n"); + int textWidth = getTextWidth(); + int textHeight = getTextHeight(); + int textLineCount = texts.length; + int textLineHeight = textHeight / textLineCount; + float textX = getTextX(); + float textY = getTextY() + textLineHeight; // Align the baseline + + // Draw the text to canvas + for (int i = 0; i < textLineCount; i++) { + canvas.drawText(texts[i], textX, textY, mPaint); + // Move to the next line position y + textY += textLineHeight; + } + + // Print debug info + if (mEngine.isDebugMode() && mEngine.getDebugger().isDebugText()) { + // Draw the bounding rectangle to canvas + float boundX = getTextX(); + float boundY = getTextY(); + canvas.drawRect(boundX, boundY, boundX + textWidth, boundY + textHeight, + mEngine.getDebugger().getDebugPaint()); + canvas.drawRect(0, 0, getWidth(), getHeight(), mEngine.getDebugger().getDebugPaint()); + } } //======================================================== diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/timer/Timer.java b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/timer/Timer.java index 32ef01e..201b1e7 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/timer/Timer.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/entity/timer/Timer.java @@ -12,46 +12,30 @@ public class Timer extends Entity { - private TimerListener mListener; - private long mPeriod; + private int mEventCount; + private long mTotalEventTime; private long mTotalTime; - private boolean mIsLooping = false; + private boolean mIsLooping; + private boolean mIsTimerRunning = false; - private final List mTimerEvents = new ArrayList<>(); + private final List mEvents = new ArrayList<>(); //-------------------------------------------------------- // Constructors //-------------------------------------------------------- - public Timer(Engine engine, TimerListener listener) { - this(engine, listener, 0); + public Timer(Engine engine) { + this(engine, false); } - public Timer(Engine engine, TimerListener listener, long period) { + public Timer(Engine engine, boolean isLooping) { super(engine); - mListener = listener; - mPeriod = period; + mIsLooping = isLooping; } //======================================================== //-------------------------------------------------------- // Getter and Setter //-------------------------------------------------------- - public TimerListener getListener() { - return mListener; - } - - public void setListener(TimerListener listener) { - mListener = listener; - } - - public long getPeriod() { - return mPeriod; - } - - public void setPeriod(long period) { - mPeriod = period; - } - public boolean isLooping() { return mIsLooping; } @@ -66,29 +50,41 @@ public void setLooping(boolean looping) { //-------------------------------------------------------- @Override public void onUpdate(long elapsedMillis) { + if (!mIsTimerRunning) { + return; + } mTotalTime += elapsedMillis; + // Dispatch timer event - int size = mTimerEvents.size(); - for (int i = 0; i < size; i++) { - TimerEvent event = mTimerEvents.get(i); + int eventCount = mEvents.size(); + for (int i = 0; i < eventCount; i++) { + TimerEvent event = mEvents.get(i); + // Dispatch one event if time passed if (!event.isTimerEventDispatch() && mTotalTime >= event.getEventTime()) { event.dispatchTimerEvent(); + mEventCount--; } } - // Check is time passed - if (mTotalTime >= mPeriod) { - if (!mIsLooping) { - removeFromGame(); + + // Check is total time passed + if (mEventCount <= 0) { + if (mIsLooping) { + mTotalTime = mTotalTime % mTotalEventTime; + // Reset event state + for (int i = 0; i < mEventCount; i++) { + TimerEvent event = mEvents.get(i); + event.resetTimerEvent(); + } + } else { + stopTimer(); } - mListener.onTimerComplete(this); - mTotalTime = 0; } } @Override public void reset() { super.reset(); - cancelTimer(); + stopTimer(); clearTimerEvent(); } //======================================================== @@ -97,44 +93,55 @@ public void reset() { // Methods //-------------------------------------------------------- public void startTimer() { - // Reset event state - int size = mTimerEvents.size(); - for (int i = 0; i < size; i++) { - TimerEvent event = mTimerEvents.get(i); - event.resetTimerEvent(); - } + mIsTimerRunning = true; + resetTimer(); addToGame(); - mTotalTime = 0; } - public void cancelTimer() { + public void stopTimer() { // Check has timer been started yet if (isRunning()) { removeFromGame(); } + mIsTimerRunning = false; + mEventCount = 0; + mTotalEventTime = 0; mTotalTime = 0; + System.out.println("end"); } - public void addTimerEvent(TimerEvent event) { - mTimerEvents.add(event); + public void pauseTimer() { + mIsTimerRunning = false; } - public void removeTimerEvent(TimerEvent event) { - mTimerEvents.remove(event); + public void resumeTimer() { + mIsTimerRunning = true; } - public void clearTimerEvent() { - mTimerEvents.clear(); + public void resetTimer() { + mEventCount = mEvents.size(); + mTotalEventTime = 0; + mTotalTime = 0; + // Reset event state + for (int i = 0; i < mEventCount; i++) { + TimerEvent event = mEvents.get(i); + event.resetTimerEvent(); + if (event.getEventTime() > mTotalEventTime) { + mTotalEventTime = event.getEventTime(); + } + } } - //======================================================== - //-------------------------------------------------------- - // Inner Classes - //-------------------------------------------------------- - public interface TimerListener { + public void addTimerEvent(TimerEvent event) { + mEvents.add(event); + } - void onTimerComplete(Timer timer); + public void removeTimerEvent(TimerEvent event) { + mEvents.remove(event); + } + public void clearTimerEvent() { + mEvents.clear(); } //======================================================== diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/event/Event.java b/nattyengine/src/main/java/com/nativegame/nattyengine/event/Event.java similarity index 61% rename from nattyengine/src/main/java/com/nativegame/nattyengine/engine/event/Event.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/event/Event.java index 6e43fa0..2e1f8e0 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/event/Event.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/event/Event.java @@ -1,4 +1,4 @@ -package com.nativegame.nattyengine.engine.event; +package com.nativegame.nattyengine.event; /** * Created by Oscar Liang on 2022/12/11 diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/event/EventListener.java b/nattyengine/src/main/java/com/nativegame/nattyengine/event/EventListener.java similarity index 70% rename from nattyengine/src/main/java/com/nativegame/nattyengine/engine/event/EventListener.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/event/EventListener.java index ae8c5e5..c175b9d 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/event/EventListener.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/event/EventListener.java @@ -1,4 +1,4 @@ -package com.nativegame.nattyengine.engine.event; +package com.nativegame.nattyengine.event; /** * Created by Oscar Liang on 2022/12/11 diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/input/touch/BaseTouchController.java b/nattyengine/src/main/java/com/nativegame/nattyengine/input/touch/BaseTouchController.java index 0628a94..5eb981e 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/input/touch/BaseTouchController.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/input/touch/BaseTouchController.java @@ -2,8 +2,8 @@ import android.view.View; -import com.nativegame.nattyengine.util.pool.ObjectPool; import com.nativegame.nattyengine.util.pool.Pool; +import com.nativegame.nattyengine.util.pool.SafeFixedObjectPool; import java.util.ArrayList; import java.util.List; @@ -14,10 +14,14 @@ public abstract class BaseTouchController implements TouchController { - protected final Pool mTouchEventPool; - protected boolean mIsEnable = false; + protected final Pool mTouchEventPool = new SafeFixedObjectPool<>(new Pool.PoolObjectFactory() { + @Override + public TouchEvent createObject() { + return new TouchEvent(); + } + }, 100); protected final List mTouchEvents = new ArrayList<>(); protected final List mTouchEventsBuffer = new ArrayList<>(); @@ -25,12 +29,6 @@ public abstract class BaseTouchController implements TouchController { // Constructors //-------------------------------------------------------- protected BaseTouchController(View view) { - mTouchEventPool = new ObjectPool<>(new Pool.PoolObjectFactory() { - @Override - public TouchEvent createObject() { - return new TouchEvent(); - } - }, 100); view.setOnTouchListener(this); } //======================================================== diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/input/touch/BoundTouchEventListener.java b/nattyengine/src/main/java/com/nativegame/nattyengine/input/touch/BoundTouchEventListener.java new file mode 100644 index 0000000..b648c5c --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/input/touch/BoundTouchEventListener.java @@ -0,0 +1,25 @@ +package com.nativegame.nattyengine.input.touch; + +import com.nativegame.nattyengine.camera.Camera; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public interface BoundTouchEventListener { + + Camera.CoordinateType getCoordinateType(); + + void setCoordinateType(Camera.CoordinateType type); + + float getX(); + + float getY(); + + float getEndX(); + + float getEndY(); + + void onAreaTouchEvent(int type, float relativeTouchX, float relativeTouchY); + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/input/touch/SingleTouchController.java b/nattyengine/src/main/java/com/nativegame/nattyengine/input/touch/SingleTouchController.java index 323d56c..4de5324 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/input/touch/SingleTouchController.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/input/touch/SingleTouchController.java @@ -50,11 +50,11 @@ public boolean isTouchDown() { @Override public List getTouchEvents() { synchronized (this) { - int size = mTouchEvents.size(); - for (int i = 0; i < size; i++) { + // Remove previous event and add new event + int eventCount = mTouchEvents.size(); + for (int i = 0; i < eventCount; i++) { mTouchEventPool.returnObject(mTouchEvents.get(i)); } - // Remove previous event and add new event mTouchEvents.clear(); mTouchEvents.addAll(mTouchEventsBuffer); mTouchEventsBuffer.clear(); diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/input/touch/TouchEventListener.java b/nattyengine/src/main/java/com/nativegame/nattyengine/input/touch/TouchEventListener.java index 6828fea..6d49aec 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/input/touch/TouchEventListener.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/input/touch/TouchEventListener.java @@ -1,11 +1,17 @@ package com.nativegame.nattyengine.input.touch; +import com.nativegame.nattyengine.camera.Camera; + /** * Created by Oscar Liang on 2022/12/11 */ public interface TouchEventListener { + Camera.CoordinateType getCoordinateType(); + + void setCoordinateType(Camera.CoordinateType type); + void onTouchEvent(int type, float touchX, float touchY); } diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/scene/BaseScene.java b/nattyengine/src/main/java/com/nativegame/nattyengine/scene/BaseScene.java new file mode 100644 index 0000000..575c3f2 --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/scene/BaseScene.java @@ -0,0 +1,118 @@ +package com.nativegame.nattyengine.scene; + +import com.nativegame.nattyengine.entity.Updatable; +import com.nativegame.nattyengine.util.exception.EngineRuntimeException; + +import java.util.ArrayList; +import java.util.List; + +public abstract class BaseScene implements Scene { + + private boolean mIsRunning = false; + private boolean mIsPause = false; + + private final List mUpdatables = new ArrayList<>(); + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + protected BaseScene() { + } + //======================================================== + + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + public List getAllChild() { + return mUpdatables; + } + + @Override + public String getName() { + return getClass().getSimpleName(); + } + + @Override + public void addToScene(Updatable updatable) { + mUpdatables.add(updatable); + } + + @Override + public void removeFromScene(Updatable updatable) { + mUpdatables.remove(updatable); + } + + @Override + public void startScene() { + if (mIsRunning) { + throw new EngineRuntimeException("'" + getName() + "' is already started!"); + } + mIsRunning = true; + mIsPause = false; + int updatableCount = mUpdatables.size(); + for (int i = 0; i < updatableCount; i++) { + Updatable u = mUpdatables.get(i); + if (!u.isRunning()) { + // Add the child directly to the game, + // since they are already in the scene + u.addToGame(); + } + } + onStartScene(); + } + + @Override + public void stopScene() { + if (!mIsRunning) { + throw new EngineRuntimeException("'" + getName() + "' is not started yet!"); + } + mIsRunning = false; + int updatableCount = mUpdatables.size(); + for (int i = updatableCount - 1; i >= 0; i--) { + Updatable u = mUpdatables.get(i); + if (u.isRunning()) { + // Remove the child from the game, but not + // remove from scene, so scene can be reused + u.removeFromGame(); + } + } + onStopScene(); + } + + @Override + public void pauseScene() { + if (mIsPause) { + throw new EngineRuntimeException("'" + getName() + "' is already paused!"); + } + mIsPause = true; + onPauseScene(); + } + + @Override + public void resumeScene() { + if (!mIsPause) { + throw new EngineRuntimeException("'" + getName() + "' is not paused yet!"); + } + mIsPause = false; + onPauseScene(); + } + //======================================================== + + //-------------------------------------------------------- + // Methods + //-------------------------------------------------------- + protected void onStartScene() { + } + + protected void onStopScene() { + } + + protected void onPauseScene() { + } + + protected void onResumeScene() { + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/scene/MultiScene.java b/nattyengine/src/main/java/com/nativegame/nattyengine/scene/MultiScene.java new file mode 100644 index 0000000..dbfd1fe --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/scene/MultiScene.java @@ -0,0 +1,91 @@ +package com.nativegame.nattyengine.scene; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public class MultiScene extends BaseScene { + + private final List mChildScenes = new ArrayList<>(); + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + public MultiScene() { + super(); + } + //======================================================== + + //-------------------------------------------------------- + // Getter and Setter + //-------------------------------------------------------- + public List getAllChildScenes() { + return mChildScenes; + } + //======================================================== + + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + public void startScene() { + super.startScene(); + int childCount = mChildScenes.size(); + for (int i = 0; i < childCount; i++) { + Scene child = mChildScenes.get(i); + child.startScene(); + } + } + + @Override + public void stopScene() { + super.stopScene(); + int childCount = mChildScenes.size(); + for (int i = 0; i < childCount; i++) { + Scene child = mChildScenes.get(i); + child.stopScene(); + } + mChildScenes.clear(); + } + + @Override + public void pauseScene() { + super.pauseScene(); + int childCount = mChildScenes.size(); + for (int i = 0; i < childCount; i++) { + Scene child = mChildScenes.get(i); + child.pauseScene(); + } + } + + @Override + public void resumeScene() { + super.resumeScene(); + int childCount = mChildScenes.size(); + for (int i = 0; i < childCount; i++) { + Scene child = mChildScenes.get(i); + child.resumeScene(); + } + } + //======================================================== + + //-------------------------------------------------------- + // Methods + //-------------------------------------------------------- + public void addChildScene(Scene scene) { + mChildScenes.add(scene); + } + + public void removeChildScene(Scene scene) { + mChildScenes.remove(scene); + } + + public void clearChildScene() { + mChildScenes.clear(); + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/scene/Scene.java b/nattyengine/src/main/java/com/nativegame/nattyengine/scene/Scene.java new file mode 100644 index 0000000..0f59acd --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/scene/Scene.java @@ -0,0 +1,29 @@ +package com.nativegame.nattyengine.scene; + +import com.nativegame.nattyengine.entity.Updatable; + +import java.util.List; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public interface Scene { + + List getAllChild(); + + String getName(); + + void addToScene(Updatable updatable); + + void removeFromScene(Updatable updatable); + + void startScene(); + + void stopScene(); + + void pauseScene(); + + void resumeScene(); + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/scene/SceneController.java b/nattyengine/src/main/java/com/nativegame/nattyengine/scene/SceneController.java new file mode 100644 index 0000000..4ce542c --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/scene/SceneController.java @@ -0,0 +1,120 @@ +package com.nativegame.nattyengine.scene; + +import java.util.HashMap; +import java.util.Map; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public class SceneController { + + private Scene mCurrentScene; + + private final Map mScenes = new HashMap<>(); + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + public SceneController() { + } + + public SceneController(Scene scene) { + mCurrentScene = scene; + addScene(scene); + } + //======================================================== + + //-------------------------------------------------------- + // Getter and Setter + //-------------------------------------------------------- + public void setCurrentScene(String sceneName) { + mCurrentScene = mScenes.get(sceneName); + } + + public void setCurrentScene(Scene scene) { + mCurrentScene = scene; + } + + public Scene getCurrentScene() { + return mCurrentScene; + } + + public Scene getScene(String sceneName) { + return mScenes.get(sceneName); + } + + public Map getAllScenes() { + return mScenes; + } + //======================================================== + + //-------------------------------------------------------- + // Methods + //-------------------------------------------------------- + public void start() { + if (mCurrentScene != null) { + mCurrentScene.startScene(); + } + } + + public void stop() { + // Clear the map and all child Scene + for (Scene child : mScenes.values()) { + child.getAllChild().clear(); + } + mScenes.clear(); + // Stop the current Scene + if (mCurrentScene != null) { + mCurrentScene.stopScene(); + mCurrentScene = null; + } + } + + public void pause() { + if (mCurrentScene != null) { + mCurrentScene.pauseScene(); + } + } + + public void resume() { + if (mCurrentScene != null) { + mCurrentScene.resumeScene(); + } + } + + public void addScene(Scene scene) { + mScenes.put(scene.getName(), scene); + } + + public void addScene(Scene scene, String sceneName) { + mScenes.put(sceneName, scene); + } + + public void removeScene(Scene scene) { + mScenes.remove(scene.getName()); + } + + public void removeScene(String sceneName) { + mScenes.remove(sceneName); + } + + public void clearScene() { + mScenes.clear(); + } + + public void changeCurrentScene(Scene nextScene) { + if (mCurrentScene != null) { + mCurrentScene.stopScene(); + } + mCurrentScene = nextScene; + mCurrentScene.startScene(); + } + + public void changeCurrentScene(String nextSceneName) { + Scene nextScene = mScenes.get(nextSceneName); + changeCurrentScene(nextScene); + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/scene/SingleScene.java b/nattyengine/src/main/java/com/nativegame/nattyengine/scene/SingleScene.java new file mode 100644 index 0000000..b7bdd31 --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/scene/SingleScene.java @@ -0,0 +1,17 @@ +package com.nativegame.nattyengine.scene; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public class SingleScene extends BaseScene { + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + public SingleScene() { + super(); + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/ui/GameActivity.java b/nattyengine/src/main/java/com/nativegame/nattyengine/ui/GameActivity.java index 90f866f..2e4d73a 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/ui/GameActivity.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/ui/GameActivity.java @@ -1,5 +1,6 @@ package com.nativegame.nattyengine.ui; +import android.media.AudioManager; import android.os.Bundle; import android.view.View; @@ -7,6 +8,7 @@ import com.nativegame.nattyengine.audio.music.MusicManager; import com.nativegame.nattyengine.audio.sound.SoundManager; +import com.nativegame.nattyengine.engine.Engine; import com.nativegame.nattyengine.texture.texture2d.Texture2DManager; import java.util.Stack; @@ -19,16 +21,21 @@ public class GameActivity extends AppCompatActivity { private static final String FRAGMENT_TAG = "content"; + private Engine mEngine; private MusicManager mMusicManager; private SoundManager mSoundManager; - private Texture2DManager mTexture2DManager; - private int mContainerViewId; + private Texture2DManager mTextureManager; + private int mFragmentContainerId; private final Stack mDialogStack = new Stack<>(); //-------------------------------------------------------- // Getter and Setter //-------------------------------------------------------- + public Engine getEngine() { + return mEngine; + } + public MusicManager getMusicManager() { return mMusicManager; } @@ -38,11 +45,11 @@ public SoundManager getSoundManager() { } public Texture2DManager getTextureManager() { - return mTexture2DManager; + return mTextureManager; } - protected void setContainerView(int containerViewId) { - mContainerViewId = containerViewId; + public void setFragmentContainer(int fragmentContainerId) { + mFragmentContainerId = fragmentContainerId; } //======================================================== @@ -50,8 +57,8 @@ protected void setContainerView(int containerViewId) { // Overriding methods //-------------------------------------------------------- @Override - protected void onStart() { - super.onStart(); + public void onWindowFocusChanged(boolean hasFocus) { + super.onWindowFocusChanged(hasFocus); View decorView = getWindow().getDecorView(); decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN @@ -62,8 +69,8 @@ protected void onStart() { } @Override - public void onWindowFocusChanged(boolean hasFocus) { - super.onWindowFocusChanged(hasFocus); + protected void onStart() { + super.onStart(); View decorView = getWindow().getDecorView(); decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN @@ -76,10 +83,11 @@ public void onWindowFocusChanged(boolean hasFocus) { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setVolumeControlStream(android.media.AudioManager.STREAM_MUSIC); + setVolumeControlStream(AudioManager.STREAM_MUSIC); + mEngine = new Engine(); mMusicManager = new MusicManager(this); mSoundManager = new SoundManager(this); - mTexture2DManager = new Texture2DManager(this); + mTextureManager = new Texture2DManager(this); } @Override @@ -101,7 +109,7 @@ protected void onDestroy() { super.onDestroy(); mMusicManager.release(); mSoundManager.release(); - mTexture2DManager.release(); + mTextureManager.release(); } @Override @@ -129,7 +137,7 @@ public void navigateToFragment(GameFragment fragment, int enterAnimationId, int getSupportFragmentManager() .beginTransaction() .setCustomAnimations(enterAnimationId, exitAnimationId, enterAnimationId, exitAnimationId) - .replace(mContainerViewId, fragment, FRAGMENT_TAG) + .replace(mFragmentContainerId, fragment, FRAGMENT_TAG) .addToBackStack(null) .commit(); } diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/ui/GameFragment.java b/nattyengine/src/main/java/com/nativegame/nattyengine/ui/GameFragment.java index a50deb8..e6642ed 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/ui/GameFragment.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/ui/GameFragment.java @@ -42,7 +42,7 @@ public synchronized void onGlobalLayout() { ViewTreeObserver viewTreeObserver = view.getViewTreeObserver(); if (viewTreeObserver.isAlive()) { viewTreeObserver.removeOnGlobalLayoutListener(this); - onLayoutCreated(view); + onViewCreated(view); } } }); @@ -52,16 +52,12 @@ public synchronized void onGlobalLayout() { //-------------------------------------------------------- // Methods //-------------------------------------------------------- - protected void onLayoutCreated(View view) { + protected void onViewCreated(View view) { } public boolean onBackPressed() { return false; } - - public void showDialog(GameDialog newDialog) { - getGameActivity().showDialog(newDialog); - } //======================================================== } diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/ui/GameView.java b/nattyengine/src/main/java/com/nativegame/nattyengine/ui/GameView.java index a8da380..9ba8cd1 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/ui/GameView.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/ui/GameView.java @@ -45,7 +45,9 @@ public void setListener(OnDrawListener listener) { @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); - mListener.onDraw(canvas); + if (mListener != null) { + mListener.onDraw(canvas); + } } //======================================================== diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/util/bitmap/ClipUtils.java b/nattyengine/src/main/java/com/nativegame/nattyengine/util/bitmap/BitmapClipUtils.java similarity index 98% rename from nattyengine/src/main/java/com/nativegame/nattyengine/util/bitmap/ClipUtils.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/util/bitmap/BitmapClipUtils.java index b3efa16..89b8bfb 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/util/bitmap/ClipUtils.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/util/bitmap/BitmapClipUtils.java @@ -8,12 +8,12 @@ * Created by Oscar Liang on 2022/12/11 */ -public class ClipUtils { +public class BitmapClipUtils { //-------------------------------------------------------- // Constructors //-------------------------------------------------------- - private ClipUtils() { + private BitmapClipUtils() { } //======================================================== diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/util/debug/Debugger.java b/nattyengine/src/main/java/com/nativegame/nattyengine/util/debug/Debugger.java new file mode 100644 index 0000000..3489b45 --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/util/debug/Debugger.java @@ -0,0 +1,82 @@ +package com.nativegame.nattyengine.util.debug; + +import android.graphics.Color; +import android.graphics.Paint; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public class Debugger { + + public static final int DEFAULT_DEBUG_COLOR = Color.BLUE; + public static final Paint.Style DEFAULT_DEBUG_STYLE = Paint.Style.STROKE; + + public static final int DEFAULT_DEBUG_TEXT_SIZE = 50; + public static final Paint.Style DEFAULT_DEBUG_TEXT_STYLE = Paint.Style.FILL; + + private boolean mIsDebugText = true; + private boolean mIsDebugCulling = true; + + private final Paint mDebugPaint = new Paint(); + private final Paint mDebugTextPaint = new Paint(); + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + public Debugger() { + mDebugPaint.setColor(DEFAULT_DEBUG_COLOR); + mDebugPaint.setStyle(DEFAULT_DEBUG_STYLE); + + mDebugTextPaint.setColor(DEFAULT_DEBUG_COLOR); + mDebugTextPaint.setTextSize(DEFAULT_DEBUG_TEXT_SIZE); + mDebugTextPaint.setStyle(DEFAULT_DEBUG_TEXT_STYLE); + } + //======================================================== + + //-------------------------------------------------------- + // Getter and Setter + //-------------------------------------------------------- + public Paint getDebugPaint() { + return mDebugPaint; + } + + public void setDebugPaint(Paint paint) { + mDebugPaint.set(paint); + } + + public Paint getDebugTextPaint() { + return mDebugTextPaint; + } + + public void setDebugTextPaint(Paint paint) { + mDebugTextPaint.set(paint); + } + + public boolean isDebugText() { + return mIsDebugText; + } + + public void setDebugText(boolean isDebugText) { + mIsDebugText = isDebugText; + } + + public boolean isDebugCulling() { + return mIsDebugCulling; + } + + public void setDebugCulling(boolean isDebugCulling) { + mIsDebugCulling = isDebugCulling; + } + //======================================================== + + //-------------------------------------------------------- + // Methods + //-------------------------------------------------------- + public void set(Debugger debugger) { + mIsDebugText = debugger.isDebugText(); + mIsDebugCulling = debugger.isDebugCulling(); + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/util/exception/EngineException.java b/nattyengine/src/main/java/com/nativegame/nattyengine/util/exception/EngineException.java new file mode 100644 index 0000000..f605efd --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/util/exception/EngineException.java @@ -0,0 +1,29 @@ +package com.nativegame.nattyengine.util.exception; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public class EngineException extends Exception { + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + public EngineException() { + super(); + } + + public EngineException(String message) { + super(message); + } + + public EngineException(Throwable cause) { + super(cause); + } + + public EngineException(String message, Throwable cause) { + super(message, cause); + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/util/exception/EngineRuntimeException.java b/nattyengine/src/main/java/com/nativegame/nattyengine/util/exception/EngineRuntimeException.java new file mode 100644 index 0000000..70c0174 --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/util/exception/EngineRuntimeException.java @@ -0,0 +1,29 @@ +package com.nativegame.nattyengine.util.exception; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public class EngineRuntimeException extends RuntimeException { + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + public EngineRuntimeException() { + super(); + } + + public EngineRuntimeException(String message) { + super(message); + } + + public EngineRuntimeException(Throwable cause) { + super(cause); + } + + public EngineRuntimeException(String message, Throwable cause) { + super(message, cause); + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/util/math/RandomUtils.java b/nattyengine/src/main/java/com/nativegame/nattyengine/util/math/RandomUtils.java index a053770..5dd2b0e 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/util/math/RandomUtils.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/util/math/RandomUtils.java @@ -17,7 +17,6 @@ private RandomUtils() { } //======================================================== - //-------------------------------------------------------- // Static methods //-------------------------------------------------------- diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/util/math/ResolutionUtils.java b/nattyengine/src/main/java/com/nativegame/nattyengine/util/math/ResolutionUtils.java new file mode 100644 index 0000000..d52508d --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/util/math/ResolutionUtils.java @@ -0,0 +1,32 @@ +package com.nativegame.nattyengine.util.math; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public class ResolutionUtils { + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + private ResolutionUtils() { + } + //======================================================== + + //-------------------------------------------------------- + // Static methods + //-------------------------------------------------------- + public static int getResolutionWidth(int screenWidth, int screenHeight, int worldWidth, int worldHeight) { + float screenRatio = screenWidth * 1f / screenHeight; + float worldRatio = worldWidth * 1f / worldHeight; + return screenRatio > worldRatio ? screenWidth : (int) (screenHeight * worldRatio); + } + + public static int getResolutionHeight(int screenWidth, int screenHeight, int worldWidth, int worldHeight) { + float screenRatio = screenWidth * 1f / screenHeight; + float worldRatio = worldWidth * 1f / worldHeight; + return screenRatio > worldRatio ? (int) (screenWidth / worldRatio) : screenHeight; + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/util/math/TransformUtils.java b/nattyengine/src/main/java/com/nativegame/nattyengine/util/math/TransformUtils.java new file mode 100644 index 0000000..980b1d7 --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/util/math/TransformUtils.java @@ -0,0 +1,44 @@ +package com.nativegame.nattyengine.util.math; + +public class TransformUtils { + + private TransformUtils() { + } + + public static float getScaleX(float x, float scalePivotX, float scaleX) { + float distance = x - scalePivotX; + float offset = (scaleX - 1) * distance; + return x + offset; + } + + public static float getScaleY(float y, float scalePivotY, float scaleY) { + float distance = y - scalePivotY; + float offset = (scaleY - 1) * distance; + return y + offset; + } + + public static float getRotateX(float x, float y, float rotationPivotX, float rotationPivotY, float angle) { + // Calculate distance to pivot + float angleInRadians = (float) Math.toRadians(angle); + float distanceX = x - rotationPivotX; + float distanceY = y - rotationPivotY; + + // Calculate new distance to pivot + float rotateDistance = (float) (distanceX * Math.cos(angleInRadians) - distanceY * Math.sin(angleInRadians)); + + return rotationPivotX + rotateDistance; + } + + public static float getRotateY(float x, float y, float rotationPivotX, float rotationPivotY, float angle) { + // Calculate distance to pivot + float angleInRadians = (float) Math.toRadians(angle); + float distanceX = x - rotationPivotX; + float distanceY = y - rotationPivotY; + + // Calculate new distance to pivot + float rotateDistance = (float) (distanceY * Math.cos(angleInRadians) + distanceX * Math.sin(angleInRadians)); + + return rotationPivotY + rotateDistance; + } + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/BaseModifier.java b/nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/BaseModifier.java similarity index 86% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/BaseModifier.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/BaseModifier.java index 3bc0459..2d39018 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/BaseModifier.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/BaseModifier.java @@ -1,12 +1,10 @@ -package com.nativegame.nattyengine.entity.modifier; - -import com.nativegame.nattyengine.entity.Entity; +package com.nativegame.nattyengine.util.modifier; /** * Created by Oscar Liang on 2022/12/11 */ -public abstract class BaseModifier implements Modifier { +public abstract class BaseModifier implements Modifier { private ModifierListener mListener; private long mDuration; @@ -14,7 +12,6 @@ public abstract class BaseModifier implements Modifier { private long mTotalTime; private boolean mIsRunning = false; private boolean mIsLooping = false; - private boolean mIsAutoRemove = false; //-------------------------------------------------------- // Constructors @@ -83,16 +80,6 @@ public void setLooping(boolean looping) { mIsLooping = looping; } - @Override - public boolean isAutoRemove() { - return mIsAutoRemove; - } - - @Override - public void setAutoRemove(boolean autoRemove) { - mIsAutoRemove = autoRemove; - } - @Override public void init(T entity) { onStartModifier(entity); @@ -110,9 +97,6 @@ public void update(T entity, long elapsedMillis) { onEndModifier(entity); if (!mIsLooping) { mIsRunning = false; - if (mIsAutoRemove) { - entity.removeFromGame(); - } } mTotalTime = 0; if (mListener != null) { diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/BaseValueModifier.java b/nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/BaseValueModifier.java similarity index 91% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/BaseValueModifier.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/BaseValueModifier.java index a6db7d2..7c41927 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/BaseValueModifier.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/BaseValueModifier.java @@ -1,13 +1,12 @@ -package com.nativegame.nattyengine.entity.modifier; +package com.nativegame.nattyengine.util.modifier; -import com.nativegame.nattyengine.entity.Entity; -import com.nativegame.nattyengine.entity.modifier.tween.Tweener; +import com.nativegame.nattyengine.util.modifier.tween.Tweener; /** * Created by Oscar Liang on 2022/12/11 */ -public abstract class BaseValueModifier extends BaseModifier { +public abstract class BaseValueModifier extends BaseModifier { private Tweener mTweener; private boolean mIsResetBefore = true; diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/DoubleValueModifier.java b/nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/DoubleValueModifier.java similarity index 88% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/DoubleValueModifier.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/DoubleValueModifier.java index 2b5b25c..0f37eef 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/DoubleValueModifier.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/DoubleValueModifier.java @@ -1,13 +1,12 @@ -package com.nativegame.nattyengine.entity.modifier; +package com.nativegame.nattyengine.util.modifier; -import com.nativegame.nattyengine.entity.Entity; -import com.nativegame.nattyengine.entity.modifier.tween.Tweener; +import com.nativegame.nattyengine.util.modifier.tween.Tweener; /** * Created by Oscar Liang on 2022/12/11 */ -public abstract class DoubleValueModifier extends BaseValueModifier { +public abstract class DoubleValueModifier extends BaseValueModifier { private float mStartValueX; private float mStartValueY; @@ -50,10 +49,6 @@ public float getEndValueY() { return mEndValueY; } - public void setValue(float startValue, float endValue) { - setValue(startValue, endValue, startValue, endValue); - } - public void setValue(float startValueX, float endValueX, float startValueY, float endValueY) { mStartValueX = startValueX; mStartValueY = startValueY; diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/DurationModifier.java b/nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/DurationModifier.java similarity index 95% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/DurationModifier.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/DurationModifier.java index f0062cb..95403b0 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/DurationModifier.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/DurationModifier.java @@ -1,4 +1,4 @@ -package com.nativegame.nattyengine.entity.modifier; +package com.nativegame.nattyengine.util.modifier; import com.nativegame.nattyengine.entity.Entity; diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/Modifier.java b/nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/Modifier.java similarity index 73% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/Modifier.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/Modifier.java index aa7f686..9f6c676 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/Modifier.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/Modifier.java @@ -1,12 +1,10 @@ -package com.nativegame.nattyengine.entity.modifier; - -import com.nativegame.nattyengine.entity.Entity; +package com.nativegame.nattyengine.util.modifier; /** * Created by Oscar Liang on 2022/12/11 */ -public interface Modifier { +public interface Modifier { ModifierListener getListener(); @@ -30,10 +28,6 @@ public interface Modifier { void setLooping(boolean looping); - boolean isAutoRemove(); - - void setAutoRemove(boolean autoRemove); - void init(T entity); void update(T entity, long elapsedMillis); diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/SingleValueModifier.java b/nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/SingleValueModifier.java similarity index 89% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/SingleValueModifier.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/SingleValueModifier.java index 582208b..569e609 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/SingleValueModifier.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/SingleValueModifier.java @@ -1,13 +1,12 @@ -package com.nativegame.nattyengine.entity.modifier; +package com.nativegame.nattyengine.util.modifier; -import com.nativegame.nattyengine.entity.Entity; -import com.nativegame.nattyengine.entity.modifier.tween.Tweener; +import com.nativegame.nattyengine.util.modifier.tween.Tweener; /** * Created by Oscar Liang on 2022/12/11 */ -public abstract class SingleValueModifier extends BaseValueModifier { +public abstract class SingleValueModifier extends BaseValueModifier { private float mStartValue; private float mEndValue; diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/tween/AccelerateTweener.java b/nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/tween/AccelerateTweener.java similarity index 95% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/tween/AccelerateTweener.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/tween/AccelerateTweener.java index f3278f0..995ebdb 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/tween/AccelerateTweener.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/tween/AccelerateTweener.java @@ -1,4 +1,4 @@ -package com.nativegame.nattyengine.entity.modifier.tween; +package com.nativegame.nattyengine.util.modifier.tween; import android.view.animation.AccelerateInterpolator; import android.view.animation.Interpolator; diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/tween/AnticipateTweener.java b/nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/tween/AnticipateTweener.java similarity index 95% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/tween/AnticipateTweener.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/tween/AnticipateTweener.java index 1b0503c..27da7f2 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/tween/AnticipateTweener.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/tween/AnticipateTweener.java @@ -1,4 +1,4 @@ -package com.nativegame.nattyengine.entity.modifier.tween; +package com.nativegame.nattyengine.util.modifier.tween; import android.view.animation.AnticipateInterpolator; import android.view.animation.Interpolator; diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/tween/BounceTweener.java b/nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/tween/BounceTweener.java similarity index 95% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/tween/BounceTweener.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/tween/BounceTweener.java index adf81c0..6476149 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/tween/BounceTweener.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/tween/BounceTweener.java @@ -1,4 +1,4 @@ -package com.nativegame.nattyengine.entity.modifier.tween; +package com.nativegame.nattyengine.util.modifier.tween; import android.view.animation.BounceInterpolator; import android.view.animation.Interpolator; diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/tween/DecelerateTweener.java b/nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/tween/DecelerateTweener.java similarity index 95% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/tween/DecelerateTweener.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/tween/DecelerateTweener.java index 38ed697..ffe1ea9 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/tween/DecelerateTweener.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/tween/DecelerateTweener.java @@ -1,4 +1,4 @@ -package com.nativegame.nattyengine.entity.modifier.tween; +package com.nativegame.nattyengine.util.modifier.tween; import android.view.animation.DecelerateInterpolator; import android.view.animation.Interpolator; diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/tween/LinearTweener.java b/nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/tween/LinearTweener.java similarity index 95% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/tween/LinearTweener.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/tween/LinearTweener.java index 559f7fd..8c273c2 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/tween/LinearTweener.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/tween/LinearTweener.java @@ -1,4 +1,4 @@ -package com.nativegame.nattyengine.entity.modifier.tween; +package com.nativegame.nattyengine.util.modifier.tween; import android.view.animation.Interpolator; import android.view.animation.LinearInterpolator; diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/tween/OvershootTweener.java b/nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/tween/OvershootTweener.java similarity index 95% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/tween/OvershootTweener.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/tween/OvershootTweener.java index dd1b86b..d6f3fcd 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/tween/OvershootTweener.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/tween/OvershootTweener.java @@ -1,4 +1,4 @@ -package com.nativegame.nattyengine.entity.modifier.tween; +package com.nativegame.nattyengine.util.modifier.tween; import android.view.animation.Interpolator; import android.view.animation.OvershootInterpolator; diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/tween/Tweener.java b/nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/tween/Tweener.java similarity index 68% rename from nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/tween/Tweener.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/tween/Tweener.java index 7e6038a..e8d7bf4 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/entity/modifier/tween/Tweener.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/util/modifier/tween/Tweener.java @@ -1,4 +1,4 @@ -package com.nativegame.nattyengine.entity.modifier.tween; +package com.nativegame.nattyengine.util.modifier.tween; /** * Created by Oscar Liang on 2022/12/11 diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/util/pool/ObjectPool.java b/nattyengine/src/main/java/com/nativegame/nattyengine/util/pool/BasePool.java similarity index 58% rename from nattyengine/src/main/java/com/nativegame/nattyengine/util/pool/ObjectPool.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/util/pool/BasePool.java index 1791c8f..bf70d99 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/util/pool/ObjectPool.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/util/pool/BasePool.java @@ -7,28 +7,19 @@ * Created by Oscar Liang on 2022/12/11 */ -public class ObjectPool implements Pool { +public abstract class BasePool implements Pool { private final PoolObjectFactory mFactory; - private final int mMinObject; - private final int mMaxObject; - private List mObjects; + private List mObjects = new ArrayList<>(); //-------------------------------------------------------- // Constructors //-------------------------------------------------------- - public ObjectPool(PoolObjectFactory factory, int minObject) { - this(factory, minObject, minObject); - } - - public ObjectPool(PoolObjectFactory factory, int minObject, int maxObject) { + public BasePool(PoolObjectFactory factory, int initCount) { mFactory = factory; - mMinObject = minObject; - mMaxObject = maxObject; - mObjects = new ArrayList<>(maxObject); // We add them to the pool now - for (int i = 0; i < minObject; i++) { + for (int i = 0; i < initCount; i++) { mObjects.add(factory.createObject()); } } @@ -38,13 +29,18 @@ public ObjectPool(PoolObjectFactory factory, int minObject, int maxObject) { // Overriding methods //-------------------------------------------------------- @Override - public int getMinObject() { - return mMinObject; + public PoolObjectFactory getFactory() { + return mFactory; + } + + @Override + public List getAllObjects() { + return mObjects; } @Override - public int getMaxObject() { - return mMaxObject; + public int getObjectCount() { + return mObjects.size(); } @Override @@ -52,15 +48,13 @@ public T obtainObject() { if (!mObjects.isEmpty()) { return mObjects.remove(0); } else { - return mFactory.createObject(); + return getDefaultObject(); } } @Override public void returnObject(T object) { - if (mObjects.size() < mMaxObject) { - mObjects.add(object); - } + onReturnObject(object); } @Override @@ -70,4 +64,12 @@ public void release() { } //======================================================== + //-------------------------------------------------------- + // Methods + //-------------------------------------------------------- + protected abstract T getDefaultObject(); + + protected abstract void onReturnObject(T object); + //======================================================== + } diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/util/pool/FixedObjectPool.java b/nattyengine/src/main/java/com/nativegame/nattyengine/util/pool/FixedObjectPool.java new file mode 100644 index 0000000..d24ce45 --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/util/pool/FixedObjectPool.java @@ -0,0 +1,48 @@ +package com.nativegame.nattyengine.util.pool; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public class FixedObjectPool extends BasePool { + + private final int mMaxObjectCount; + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + public FixedObjectPool(PoolObjectFactory factory, int initCount) { + this(factory, initCount, initCount); + } + + public FixedObjectPool(PoolObjectFactory factory, int initCount, int maxCount) { + super(factory, initCount); + mMaxObjectCount = maxCount; + } + //======================================================== + + //-------------------------------------------------------- + // Getter and Setter + //-------------------------------------------------------- + public int getMaxObjectCount() { + return mMaxObjectCount; + } + //======================================================== + + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + protected T getDefaultObject() { + return null; + } + + @Override + protected void onReturnObject(T object) { + if (getObjectCount() < mMaxObjectCount) { + getAllObjects().add(object); + } + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/util/pool/GrowObjectPool.java b/nattyengine/src/main/java/com/nativegame/nattyengine/util/pool/GrowObjectPool.java new file mode 100644 index 0000000..936eae4 --- /dev/null +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/util/pool/GrowObjectPool.java @@ -0,0 +1,31 @@ +package com.nativegame.nattyengine.util.pool; + +/** + * Created by Oscar Liang on 2022/12/11 + */ + +public class GrowObjectPool extends BasePool { + + //-------------------------------------------------------- + // Constructors + //-------------------------------------------------------- + public GrowObjectPool(PoolObjectFactory factory, int initCount) { + super(factory, initCount); + } + //======================================================== + + //-------------------------------------------------------- + // Overriding methods + //-------------------------------------------------------- + @Override + protected T getDefaultObject() { + return getFactory().createObject(); + } + + @Override + protected void onReturnObject(T object) { + getAllObjects().add(object); + } + //======================================================== + +} diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/util/pool/Pool.java b/nattyengine/src/main/java/com/nativegame/nattyengine/util/pool/Pool.java index edd48c1..409f706 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/util/pool/Pool.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/util/pool/Pool.java @@ -1,14 +1,18 @@ package com.nativegame.nattyengine.util.pool; +import java.util.List; + /** * Created by Oscar Liang on 2022/12/11 */ public interface Pool { - int getMinObject(); + PoolObjectFactory getFactory(); + + List getAllObjects(); - int getMaxObject(); + int getObjectCount(); T obtainObject(); diff --git a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/hitbox/OvalHitBox.java b/nattyengine/src/main/java/com/nativegame/nattyengine/util/pool/SafeFixedObjectPool.java similarity index 50% rename from nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/hitbox/OvalHitBox.java rename to nattyengine/src/main/java/com/nativegame/nattyengine/util/pool/SafeFixedObjectPool.java index 9215ebb..c6f365b 100644 --- a/nattyengine/src/main/java/com/nativegame/nattyengine/engine/collision/hitbox/OvalHitBox.java +++ b/nattyengine/src/main/java/com/nativegame/nattyengine/util/pool/SafeFixedObjectPool.java @@ -1,23 +1,20 @@ -package com.nativegame.nattyengine.engine.collision.hitbox; - -import android.graphics.Bitmap; - -import com.nativegame.nattyengine.util.bitmap.BitmapUtils; +package com.nativegame.nattyengine.util.pool; /** * Created by Oscar Liang on 2022/12/11 */ -public class OvalHitBox extends BaseHitBox { - - private final Bitmap mHitBoxBitmap; +public class SafeFixedObjectPool extends FixedObjectPool { //-------------------------------------------------------- // Constructors //-------------------------------------------------------- - public OvalHitBox(int hitBoxWidth, int hitBoxHeight) { - super(hitBoxWidth, hitBoxHeight); - mHitBoxBitmap = BitmapUtils.createOvalBitmap(hitBoxWidth, hitBoxHeight); + public SafeFixedObjectPool(PoolObjectFactory factory, int minObject) { + super(factory, minObject); + } + + public SafeFixedObjectPool(PoolObjectFactory factory, int minObject, int maxObject) { + super(factory, minObject, maxObject); } //======================================================== @@ -25,8 +22,8 @@ public OvalHitBox(int hitBoxWidth, int hitBoxHeight) { // Overriding methods //-------------------------------------------------------- @Override - public Bitmap getCollisionBitmap() { - return mHitBoxBitmap; + protected T getDefaultObject() { + return getFactory().createObject(); } //========================================================