summaryrefslogtreecommitdiff
path: root/src/com/replica/replicaisland/OrbitalMagnetComponent.java
blob: 7f2646874216e207faffe3c88182abd7c9f84fb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.replica.replicaisland;


public class OrbitalMagnetComponent extends GameComponent {
	private final static float DEFAULT_STRENGTH = 15.0f;

	private float mStrength;
	private Vector2 mCenter;
	private Vector2 mDelta;
	private Vector2 mRim;
	private Vector2 mVelocity;
	private float mMagnetRadius;
	private float mAreaRadius;
	
	public OrbitalMagnetComponent() {
        super();
        mCenter = new Vector2();
        mDelta = new Vector2();
        mRim = new Vector2();
        mVelocity = new Vector2();
        reset();
        setPhase(ComponentPhases.COLLISION_DETECTION.ordinal());
    }
    
    @Override
    public void reset() {
    	mCenter.zero();
    	mDelta.zero();
    	mRim.zero();
    	mVelocity.zero();
    	mStrength = DEFAULT_STRENGTH;
    	mAreaRadius = 0.0f;
    	mMagnetRadius = 0.0f;
    }

    @Override
    public void update(float timeDelta, BaseObject parent) {
        GameObjectManager manager = sSystemRegistry.gameObjectManager;
        if (manager != null) {
        	GameObject player = manager.getPlayer();
        	if (player != null) {
        		GameObject parentObject =  (GameObject)parent;
        		applyMagnetism(player, 
        				parentObject.getCenteredPositionX(), 
        				parentObject.getCenteredPositionY(),
        				timeDelta);
        	}
        }
    }
    
    private void applyMagnetism(GameObject target, float centerX, float centerY, float timeDelta) {
    	mCenter.set(centerX, centerY);
    	final float targetX = target.getCenteredPositionX();
    	final float targetY = target.getCenteredPositionY();
    	mDelta.set(targetX, targetY);
    	mDelta.subtract(mCenter);
    	
    	final float distanceFromCenter2 = mDelta.length2();
    	final float area2 = mAreaRadius * mAreaRadius;
    	if (distanceFromCenter2 < area2) {
    		mRim.set(mDelta);
    		mRim.normalize();
    		mRim.multiply(mMagnetRadius);
    		mRim.add(mCenter);
    		// rim is now the closest point on the magnet circle
    		
    		// remove gravity
    		final Vector2 targetVelocity = target.getVelocity();
    		GravityComponent gravity = target.findByClass(GravityComponent.class);
            final Vector2 gravityVector = gravity.getGravity();
            mVelocity.set(gravityVector);
            mVelocity.multiply(timeDelta);
            targetVelocity.subtract(mVelocity);
            
    		mDelta.add(targetVelocity);
    		mDelta.normalize();
    		mDelta.multiply(mMagnetRadius);
    		mDelta.add(mCenter);
    		
    		// mDelta is now the next point on the magnet circle in the direction of
    		// movement.
    		
    		mDelta.subtract(mRim);
    		mDelta.normalize();
    		// Now mDelta is the tangent to the magnet circle, pointing in the direction
    		// of movement.
    		
    		mVelocity.set(mDelta);
    		mVelocity.normalize();
    		
    		// mVelocity is now the direction to push the player
    		
			mVelocity.multiply(mStrength);
			float weight = 1.0f;
			if (distanceFromCenter2 > mMagnetRadius * mMagnetRadius) {
				float distance = (float)Math.sqrt(distanceFromCenter2);
				weight = (distance - mMagnetRadius) / (mAreaRadius - mMagnetRadius);
				weight = 1.0f - weight;
				mVelocity.multiply(weight);
			}
			final float speed = targetVelocity.length();
			targetVelocity.add(mVelocity);
			if (targetVelocity.length2() > (speed * speed)) {
				targetVelocity.normalize();
				targetVelocity.multiply(speed);
			}
	    	
    	}
    	
    }
    
    public void setup(float areaRadius, float orbitRadius) {
    	mAreaRadius = areaRadius;
    	mMagnetRadius = orbitRadius;
    }
}