diff options
author | Daniel Sandler <dsandler@android.com> | 2015-08-16 01:33:37 +0000 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2015-08-16 01:33:37 +0000 |
commit | 64b05c0fd413d481cea19e9590732e8219a60912 (patch) | |
tree | 977a0f9f6c4bdee28c68b50922fa042d8d3d6d22 /packages/SystemUI/src/com | |
parent | 8c8387d235b780c4a78a54d8240fe1f5a17c6af5 (diff) | |
parent | 9f238a9455639fccb6f44abd03752e3bb3bbc91e (diff) | |
download | frameworks_base-64b05c0fd413d481cea19e9590732e8219a60912.zip frameworks_base-64b05c0fd413d481cea19e9590732e8219a60912.tar.gz frameworks_base-64b05c0fd413d481cea19e9590732e8219a60912.tar.bz2 |
am 9f238a94: Merge "Introducing M Land." into mnc-dev
* commit '9f238a9455639fccb6f44abd03752e3bb3bbc91e':
Introducing M Land.
Diffstat (limited to 'packages/SystemUI/src/com')
-rw-r--r-- | packages/SystemUI/src/com/android/systemui/egg/LLandActivity.java | 43 | ||||
-rw-r--r-- | packages/SystemUI/src/com/android/systemui/egg/MLand.java (renamed from packages/SystemUI/src/com/android/systemui/egg/LLand.java) | 799 | ||||
-rw-r--r-- | packages/SystemUI/src/com/android/systemui/egg/MLandActivity.java | 91 | ||||
-rw-r--r-- | packages/SystemUI/src/com/android/systemui/egg/ShruggyActivity.java | 34 |
4 files changed, 717 insertions, 250 deletions
diff --git a/packages/SystemUI/src/com/android/systemui/egg/LLandActivity.java b/packages/SystemUI/src/com/android/systemui/egg/LLandActivity.java deleted file mode 100644 index 50221d3..0000000 --- a/packages/SystemUI/src/com/android/systemui/egg/LLandActivity.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (C) 2014 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.android.systemui.egg; - -import android.app.Activity; -import android.os.Bundle; -import android.widget.TextView; - -import com.android.systemui.R; - -public class LLandActivity extends Activity { - LLand mLand; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.lland); - mLand = (LLand) findViewById(R.id.world); - mLand.setScoreField((TextView) findViewById(R.id.score)); - mLand.setSplash(findViewById(R.id.welcome)); - //Log.v(LLand.TAG, "focus: " + mLand.requestFocus()); - } - - @Override - public void onPause() { - mLand.stop(); - super.onPause(); - } -} diff --git a/packages/SystemUI/src/com/android/systemui/egg/LLand.java b/packages/SystemUI/src/com/android/systemui/egg/MLand.java index fa257b1..b84777b 100644 --- a/packages/SystemUI/src/com/android/systemui/egg/LLand.java +++ b/packages/SystemUI/src/com/android/systemui/egg/MLand.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2014 The Android Open Source Project + * Copyright (C) 2015 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. @@ -16,6 +16,7 @@ package com.android.systemui.egg; +import android.animation.LayoutTransition; import android.animation.TimeAnimator; import android.content.Context; import android.content.res.Resources; @@ -26,6 +27,7 @@ import android.graphics.Outline; import android.graphics.Paint; import android.graphics.Path; import android.graphics.PorterDuff; +import android.graphics.PorterDuffColorFilter; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.graphics.drawable.GradientDrawable; @@ -35,49 +37,51 @@ import android.os.Vibrator; import android.util.AttributeSet; import android.util.Log; import android.view.Gravity; +import android.view.InputDevice; import android.view.KeyEvent; +import android.view.LayoutInflater; import android.view.MotionEvent; -import android.util.Slog; import android.view.View; +import android.view.ViewGroup; import android.view.ViewOutlineProvider; -import android.view.animation.DecelerateInterpolator; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.TextView; -import com.android.systemui.R; - import java.util.ArrayList; -public class LLand extends FrameLayout { - public static final String TAG = "LLand"; +import com.android.internal.logging.MetricsLogger; + +import com.android.systemui.R; + +// It's like LLand, but "M"ultiplayer. +public class MLand extends FrameLayout { + public static final String TAG = "MLand"; public static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); public static final boolean DEBUG_DRAW = false; // DEBUG + public static final boolean SHOW_TOUCHES = true; + public static void L(String s, Object ... objects) { if (DEBUG) { - Slog.d(TAG, objects.length == 0 ? s : String.format(s, objects)); + Log.d(TAG, objects.length == 0 ? s : String.format(s, objects)); } } + public static final float PI_2 = (float) (Math.PI/2); + public static final boolean AUTOSTART = true; public static final boolean HAVE_STARS = true; - public static final float DEBUG_SPEED_MULTIPLIER = 1f; // 0.1f; + public static final float DEBUG_SPEED_MULTIPLIER = 0.5f; // only if DEBUG public static final boolean DEBUG_IDDQD = Log.isLoggable(TAG + ".iddqd", Log.DEBUG); - final static int[] POPS = { - // resid // spinny! // alpha - R.drawable.pop_belt, 0, 255, - R.drawable.pop_droid, 0, 255, - R.drawable.pop_pizza, 1, 255, - R.drawable.pop_stripes, 0, 255, - R.drawable.pop_swirl, 1, 255, - R.drawable.pop_vortex, 1, 255, - R.drawable.pop_vortex2, 1, 255, - R.drawable.pop_ball, 0, 190, - }; + public static final int DEFAULT_PLAYERS = 1; + public static final int MIN_PLAYERS = 1; + public static final int MAX_PLAYERS = 6; + + static final float CONTROLLER_VIBRATION_MULTIPLIER = 2f; private static class Params { public float TRANSLATION_PER_SEC; @@ -125,7 +129,7 @@ public class LLand extends FrameLayout { // Sanity checking if (OBSTACLE_MIN <= OBSTACLE_WIDTH / 2) { - Slog.e(TAG, "error: obstacles might be too short, adjusting"); + L("error: obstacles might be too short, adjusting"); OBSTACLE_MIN = OBSTACLE_WIDTH / 2 + 1; } } @@ -137,21 +141,24 @@ public class LLand extends FrameLayout { private final AudioAttributes mAudioAttrs = new AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_GAME).build(); - private TextView mScoreField; private View mSplash; + private ViewGroup mScoreFields; - private Player mDroid; + private ArrayList<Player> mPlayers = new ArrayList<Player>(); private ArrayList<Obstacle> mObstaclesInPlay = new ArrayList<Obstacle>(); private float t, dt; - private int mScore; private float mLastPipeTime; // in sec + private int mCurrentPipeId; // basically, equivalent to the current score private int mWidth, mHeight; private boolean mAnimating, mPlaying; private boolean mFrozen; // after death, a short backoff + private int mCountdown = 0; private boolean mFlipped; + private int mTaps; + private int mTimeOfDay; private static final int DAY = 0, NIGHT = 1, TWILIGHT = 2, SUNSET = 3; private static final int[][] SKIES = { @@ -161,26 +168,62 @@ public class LLand extends FrameLayout { { 0xFFa08020, 0xFF204080 }, // SUNSET }; + private int mScene; + private static final int SCENE_CITY = 0, SCENE_TX = 1, SCENE_ZRH = 2; + private static final int SCENE_COUNT = 3; + private static Params PARAMS; - public LLand(Context context) { + private static float dp = 1f; + + private Paint mTouchPaint, mPlayerTracePaint; + + private ArrayList<Integer> mGameControllers = new ArrayList<>(); + + public MLand(Context context) { this(context, null); } - public LLand(Context context, AttributeSet attrs) { + public MLand(Context context, AttributeSet attrs) { this(context, attrs, 0); } - public LLand(Context context, AttributeSet attrs, int defStyle) { + public MLand(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); + mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); setFocusable(true); PARAMS = new Params(getResources()); - mTimeOfDay = irand(0, SKIES.length); + mTimeOfDay = irand(0, SKIES.length - 1); + mScene = irand(0, SCENE_COUNT); + + mTouchPaint = new Paint(Paint.ANTI_ALIAS_FLAG); + mTouchPaint.setColor(0x80FFFFFF); + mTouchPaint.setStyle(Paint.Style.FILL); + + mPlayerTracePaint = new Paint(Paint.ANTI_ALIAS_FLAG); + mPlayerTracePaint.setColor(0x80FFFFFF); + mPlayerTracePaint.setStyle(Paint.Style.STROKE); + mPlayerTracePaint.setStrokeWidth(2 * dp); // we assume everything will be laid out left|top setLayoutDirection(LAYOUT_DIRECTION_LTR); + + setupPlayers(DEFAULT_PLAYERS); + + MetricsLogger.count(getContext(), "egg_mland_create", 1); + } + + @Override + public void onAttachedToWindow() { + super.onAttachedToWindow(); + dp = getResources().getDisplayMetrics().density; + + reset(); + if (AUTOSTART) { + start(false); + } } @Override @@ -193,13 +236,18 @@ public class LLand extends FrameLayout { public float getGameTime() { return t; } public float getLastTimeStep() { return dt; } - public void setScoreField(TextView tv) { - mScoreField = tv; - if (tv != null) { - tv.setTranslationZ(PARAMS.HUD_Z); - if (!(mAnimating && mPlaying)) { - tv.setTranslationY(-500); - } + public void setScoreFieldHolder(ViewGroup vg) { + mScoreFields = vg; + if (vg != null) { + final LayoutTransition lt = new LayoutTransition(); + lt.setDuration(250); + mScoreFields.setLayoutTransition(lt); + } + for (Player p : mPlayers) { + mScoreFields.addView(p.mScoreField, + new MarginLayoutParams( + MarginLayoutParams.WRAP_CONTENT, + MarginLayoutParams.MATCH_PARENT)); } } @@ -207,23 +255,134 @@ public class LLand extends FrameLayout { mSplash = v; } + public static boolean isGamePad(InputDevice dev) { + int sources = dev.getSources(); + + // Verify that the device has gamepad buttons, control sticks, or both. + return (((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) + || ((sources & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK)); + } + + public ArrayList getGameControllers() { + mGameControllers.clear(); + int[] deviceIds = InputDevice.getDeviceIds(); + for (int deviceId : deviceIds) { + InputDevice dev = InputDevice.getDevice(deviceId); + if (isGamePad(dev)) { + if (!mGameControllers.contains(deviceId)) { + mGameControllers.add(deviceId); + } + } + } + return mGameControllers; + } + + public int getControllerPlayer(int id) { + final int player = mGameControllers.indexOf(id); + if (player < 0 || player >= mPlayers.size()) return 0; + return player; + } + @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { + dp = getResources().getDisplayMetrics().density; + stop(); + reset(); if (AUTOSTART) { start(false); } } - final float hsv[] = {0, 0, 0}; + final static float hsv[] = {0, 0, 0}; + + private static float luma(int bgcolor) { + return 0.2126f * (float) (bgcolor & 0xFF0000) / 0xFF0000 + + 0.7152f * (float) (bgcolor & 0xFF00) / 0xFF00 + + 0.0722f * (float) (bgcolor & 0xFF) / 0xFF; + } + + public Player getPlayer(int i) { + return i < mPlayers.size() ? mPlayers.get(i) : null; + } + + private int addPlayerInternal(Player p) { + mPlayers.add(p); + realignPlayers(); + TextView scoreField = (TextView) + LayoutInflater.from(getContext()).inflate(R.layout.mland_scorefield, null); + if (mScoreFields != null) { + mScoreFields.addView(scoreField, + new MarginLayoutParams( + MarginLayoutParams.WRAP_CONTENT, + MarginLayoutParams.MATCH_PARENT)); + } + p.setScoreField(scoreField); + return mPlayers.size()-1; + } + + private void removePlayerInternal(Player p) { + if (mPlayers.remove(p)) { + removeView(p); + mScoreFields.removeView(p.mScoreField); + realignPlayers(); + } + } + + private void realignPlayers() { + final int N = mPlayers.size(); + float x = (mWidth - (N-1) * PARAMS.PLAYER_SIZE) / 2; + for (int i=0; i<N; i++) { + final Player p = mPlayers.get(i); + p.setX(x); + x += PARAMS.PLAYER_SIZE; + } + } + + private void clearPlayers() { + while (mPlayers.size() > 0) { + removePlayerInternal(mPlayers.get(0)); + } + } + + public void setupPlayers(int num) { + clearPlayers(); + for (int i=0; i<num; i++) { + addPlayerInternal(Player.create(this)); + } + } + + public void addPlayer() { + if (getNumPlayers() == MAX_PLAYERS) return; + addPlayerInternal(Player.create(this)); + } + + public int getNumPlayers() { + return mPlayers.size(); + } + + public void removePlayer() { + if (getNumPlayers() == MIN_PLAYERS) return; + removePlayerInternal(mPlayers.get(mPlayers.size() - 1)); + } - private void thump() { + private void thump(int playerIndex, long ms) { if (mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT) { // No interruptions. Not even game haptics. return; } - mVibrator.vibrate(80, mAudioAttrs); + if (playerIndex < mGameControllers.size()) { + int controllerId = mGameControllers.get(playerIndex); + InputDevice dev = InputDevice.getDevice(controllerId); + if (dev != null && dev.getVibrator().hasVibrator()) { + dev.getVibrator().vibrate( + (long) (ms * CONTROLLER_VIBRATION_MULTIPLIER), + mAudioAttrs); + return; + } + } + mVibrator.vibrate(ms, mAudioAttrs); } public void reset() { @@ -238,8 +397,6 @@ public class LLand extends FrameLayout { mFlipped = frand() > 0.5f; setScaleX(mFlipped ? -1 : 1); - setScore(0); - int i = getChildCount(); while (i-->0) { final View v = getChildAt(i); @@ -249,6 +406,7 @@ public class LLand extends FrameLayout { } mObstaclesInPlay.clear(); + mCurrentPipeId = 0; mWidth = getWidth(); mHeight = getHeight(); @@ -297,16 +455,29 @@ public class LLand extends FrameLayout { } else if (r1 < 0.6 && !cloudless) { s = new Cloud(getContext()); } else { - s = new Building(getContext()); - - s.z = (float)i/N; - s.setTranslationZ(PARAMS.SCENERY_Z * (1+s.z)); + switch (mScene) { + case SCENE_ZRH: + s = new Mountain(getContext()); + break; + case SCENE_TX: + s = new Cactus(getContext()); + break; + case SCENE_CITY: + default: + s = new Building(getContext()); + break; + } + s.z = (float) i / N; + // no more shadows for these things + //s.setTranslationZ(PARAMS.SCENERY_Z * (1+s.z)); s.v = 0.85f * s.z; // buildings move proportional to their distance - hsv[0] = 175; - hsv[1] = 0.25f; - hsv[2] = 1 * s.z; - s.setBackgroundColor(Color.HSVToColor(hsv)); - s.h = irand(PARAMS.BUILDING_HEIGHT_MIN, mh); + if (mScene == SCENE_CITY) { + s.setBackgroundColor(Color.GRAY); + s.h = irand(PARAMS.BUILDING_HEIGHT_MIN, mh); + } + final int c = (int)(255f*s.z); + final Drawable bg = s.getBackground(); + if (bg != null) bg.setColorFilter(Color.rgb(c,c,c), PorterDuff.Mode.MULTIPLY); } final LayoutParams lp = new LayoutParams(s.w, s.h); if (s instanceof Building) { @@ -321,15 +492,21 @@ public class LLand extends FrameLayout { } } + addView(s, lp); s.setTranslationX(frand(-lp.width, mWidth + lp.width)); } - mDroid = new Player(getContext()); - mDroid.setX(mWidth / 2); - mDroid.setY(mHeight / 2); - addView(mDroid, new LayoutParams(PARAMS.PLAYER_SIZE, PARAMS.PLAYER_SIZE)); + for (Player p : mPlayers) { + addView(p); // put it back! + p.reset(); + } + + realignPlayers(); + if (mAnim != null) { + mAnim.cancel(); + } mAnim = new TimeAnimator(); mAnim.setTimeListener(new TimeAnimator.TimeListener() { @Override @@ -339,58 +516,107 @@ public class LLand extends FrameLayout { }); } - private void setScore(int score) { - mScore = score; - if (mScoreField != null) { - mScoreField.setText(DEBUG_IDDQD ? "??" : String.valueOf(score)); - } - } - - private void addScore(int incr) { - setScore(mScore + incr); - } - public void start(boolean startPlaying) { - L("start(startPlaying=%s)", startPlaying?"true":"false"); - if (startPlaying) { - mPlaying = true; + L("start(startPlaying=%s)", startPlaying ? "true" : "false"); + if (startPlaying && mCountdown <= 0) { + showSplash(); - t = 0; - // there's a sucker born every OBSTACLE_PERIOD - mLastPipeTime = getGameTime() - PARAMS.OBSTACLE_PERIOD; + mSplash.findViewById(R.id.play_button).setEnabled(false); - if (mSplash != null && mSplash.getAlpha() > 0f) { - mSplash.setTranslationZ(PARAMS.HUD_Z); - mSplash.animate().alpha(0).translationZ(0).setDuration(400); + final View playImage = mSplash.findViewById(R.id.play_button_image); + final TextView playText = (TextView) mSplash.findViewById(R.id.play_button_text); - mScoreField.animate().translationY(0) - .setInterpolator(new DecelerateInterpolator()) - .setDuration(1500); - } + playImage.animate().alpha(0f); + playText.animate().alpha(1f); - mScoreField.setTextColor(0xFFAAAAAA); - mScoreField.setBackgroundResource(R.drawable.scorecard); - mDroid.setVisibility(View.VISIBLE); - mDroid.setX(mWidth / 2); - mDroid.setY(mHeight / 2); - } else { - mDroid.setVisibility(View.GONE); + mCountdown = 3; + post(new Runnable() { + @Override + public void run() { + if (mCountdown == 0) { + startPlaying(); + } else { + postDelayed(this, 500); + } + playText.setText(String.valueOf(mCountdown)); + mCountdown--; + } + }); + } + + for (Player p : mPlayers) { + p.setVisibility(View.INVISIBLE); } + if (!mAnimating) { mAnim.start(); mAnimating = true; } } + public void hideSplash() { + if (mSplash != null && mSplash.getVisibility() == View.VISIBLE) { + mSplash.setClickable(false); + mSplash.animate().alpha(0).translationZ(0).setDuration(300).withEndAction( + new Runnable() { + @Override + public void run() { + mSplash.setVisibility(View.GONE); + } + } + ); + } + } + + public void showSplash() { + if (mSplash != null && mSplash.getVisibility() != View.VISIBLE) { + mSplash.setClickable(true); + mSplash.setAlpha(0f); + mSplash.setVisibility(View.VISIBLE); + mSplash.animate().alpha(1f).setDuration(1000); + mSplash.findViewById(R.id.play_button_image).setAlpha(1f); + mSplash.findViewById(R.id.play_button_text).setAlpha(0f); + mSplash.findViewById(R.id.play_button).setEnabled(true); + mSplash.findViewById(R.id.play_button).requestFocus(); + } + } + + public void startPlaying() { + mPlaying = true; + + t = 0; + // there's a sucker born every OBSTACLE_PERIOD + mLastPipeTime = getGameTime() - PARAMS.OBSTACLE_PERIOD; + + hideSplash(); + + realignPlayers(); + mTaps = 0; + + final int N = mPlayers.size(); + MetricsLogger.histogram(getContext(), "egg_mland_players", N); + for (int i=0; i<N; i++) { + final Player p = mPlayers.get(i); + p.setVisibility(View.VISIBLE); + p.reset(); + p.start(); + p.boost(-1, -1); // start you off flying! + p.unboost(); // not forever, though + } + } + public void stop() { if (mAnimating) { mAnim.cancel(); mAnim = null; mAnimating = false; - mScoreField.setTextColor(0xFFFFFFFF); - mScoreField.setBackgroundResource(R.drawable.scorecard_gameover); - mTimeOfDay = irand(0, SKIES.length); // for next reset + mPlaying = false; + mTimeOfDay = irand(0, SKIES.length - 1); // for next reset + mScene = irand(0, SCENE_COUNT); mFrozen = true; + for (Player p : mPlayers) { + p.die(); + } postDelayed(new Runnable() { @Override public void run() { @@ -421,7 +647,11 @@ public class LLand extends FrameLayout { } public static final int irand(int a, int b) { - return (int) lerp(frand(), (float) a, (float) b); + return Math.round(frand((float) a, (float) b)); + } + + public static int pick(int[] l) { + return l[irand(0, l.length-1)]; } private void step(long t_ms, long dt_ms) { @@ -443,34 +673,58 @@ public class LLand extends FrameLayout { } } - // 2. Check for altitude - if (mPlaying && mDroid.below(mHeight)) { - if (DEBUG_IDDQD) { - poke(); - unpoke(); - } else { - L("player hit the floor"); - thump(); - stop(); + if (mPlaying) { + int livingPlayers = 0; + for (i = 0; i < mPlayers.size(); i++) { + final Player p = getPlayer(i); + + if (p.mAlive) { + // 2. Check for altitude + if (p.below(mHeight)) { + if (DEBUG_IDDQD) { + poke(i); + unpoke(i); + } else { + L("player %d hit the floor", i); + thump(i, 80); + p.die(); + } + } + + // 3. Check for obstacles + int maxPassedStem = 0; + for (int j = mObstaclesInPlay.size(); j-- > 0; ) { + final Obstacle ob = mObstaclesInPlay.get(j); + if (ob.intersects(p) && !DEBUG_IDDQD) { + L("player hit an obstacle"); + thump(i, 80); + p.die(); + } else if (ob.cleared(p)) { + if (ob instanceof Stem) { + maxPassedStem = Math.max(maxPassedStem, ((Stem)ob).id); + } + } + } + + if (maxPassedStem > p.mScore) { + p.addScore(1); + } + } + + if (p.mAlive) livingPlayers++; } - } - // 3. Check for obstacles - boolean passedBarrier = false; - for (int j = mObstaclesInPlay.size(); j-->0;) { - final Obstacle ob = mObstaclesInPlay.get(j); - if (mPlaying && ob.intersects(mDroid) && !DEBUG_IDDQD) { - L("player hit an obstacle"); - thump(); + if (livingPlayers == 0) { stop(); - } else if (ob.cleared(mDroid)) { - if (ob instanceof Stem) passedBarrier = true; - mObstaclesInPlay.remove(j); - } - } - if (mPlaying && passedBarrier) { - addScore(1); + MetricsLogger.count(getContext(), "egg_mland_taps", mTaps); + mTaps = 0; + final int playerCount = mPlayers.size(); + for (int pi=0; pi<playerCount; pi++) { + final Player p = mPlayers.get(pi); + MetricsLogger.histogram(getContext(), "egg_mland_score", p.getScore()); + } + } } // 4. Handle edge of screen @@ -480,6 +734,7 @@ public class LLand extends FrameLayout { if (v instanceof Obstacle) { if (v.getTranslationX() + v.getWidth() < 0) { removeViewAt(i); + mObstaclesInPlay.remove(v); } } else if (v instanceof Scenery) { final Scenery s = (Scenery) v; @@ -492,6 +747,7 @@ public class LLand extends FrameLayout { // 3. Time for more obstacles! if (mPlaying && (t - mLastPipeTime) > PARAMS.OBSTACLE_PERIOD) { mLastPipeTime = t; + mCurrentPipeId ++; final int obstacley = (int)(frand() * (mHeight - 2*PARAMS.OBSTACLE_MIN - PARAMS.OBSTACLE_GAP)) + PARAMS.OBSTACLE_MIN; @@ -523,11 +779,11 @@ public class LLand extends FrameLayout { p1.setTranslationY(-PARAMS.OBSTACLE_WIDTH); p1.setTranslationZ(PARAMS.OBSTACLE_Z); p1.setScaleX(0.25f); - p1.setScaleY(0.25f); + p1.setScaleY(-0.25f); p1.animate() .translationY(s1.h-inset) .scaleX(1f) - .scaleY(1f) + .scaleY(-1f) .setStartDelay(d1) .setDuration(250); mObstaclesInPlay.add(p1); @@ -568,18 +824,25 @@ public class LLand extends FrameLayout { mObstaclesInPlay.add(p2); } - if (DEBUG_DRAW) invalidate(); + if (SHOW_TOUCHES || DEBUG_DRAW) invalidate(); } - + @Override public boolean onTouchEvent(MotionEvent ev) { L("touch: %s", ev); - switch (ev.getAction()) { + final int actionIndex = ev.getActionIndex(); + final float x = ev.getX(actionIndex); + final float y = ev.getY(actionIndex); + int playerIndex = (int) (getNumPlayers() * (x / getWidth())); + if (mFlipped) playerIndex = getNumPlayers() - 1 - playerIndex; + switch (ev.getActionMasked()) { case MotionEvent.ACTION_DOWN: - poke(); + case MotionEvent.ACTION_POINTER_DOWN: + poke(playerIndex, x, y); return true; case MotionEvent.ACTION_UP: - unpoke(); + case MotionEvent.ACTION_POINTER_UP: + unpoke(playerIndex); return true; } return false; @@ -590,10 +853,10 @@ public class LLand extends FrameLayout { L("trackball: %s", ev); switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: - poke(); + poke(0); return true; case MotionEvent.ACTION_UP: - unpoke(); + unpoke(0); return true; } return false; @@ -608,7 +871,8 @@ public class LLand extends FrameLayout { case KeyEvent.KEYCODE_SPACE: case KeyEvent.KEYCODE_ENTER: case KeyEvent.KEYCODE_BUTTON_A: - poke(); + int player = getControllerPlayer(ev.getDeviceId()); + poke(player); return true; } return false; @@ -623,7 +887,8 @@ public class LLand extends FrameLayout { case KeyEvent.KEYCODE_SPACE: case KeyEvent.KEYCODE_ENTER: case KeyEvent.KEYCODE_BUTTON_A: - unpoke(); + int player = getControllerPlayer(ev.getDeviceId()); + unpoke(player); return true; } return false; @@ -635,47 +900,76 @@ public class LLand extends FrameLayout { return false; } - private void poke() { - L("poke"); + private void poke(int playerIndex) { + poke(playerIndex, -1, -1); + } + + private void poke(int playerIndex, float x, float y) { + L("poke(%d)", playerIndex); if (mFrozen) return; if (!mAnimating) { reset(); - start(true); - } else if (!mPlaying) { - start(true); } - mDroid.boost(); - if (DEBUG) { - mDroid.dv *= DEBUG_SPEED_MULTIPLIER; - mDroid.animate().setDuration((long) (200/DEBUG_SPEED_MULTIPLIER)); + if (!mPlaying) { + start(true); + } else { + final Player p = getPlayer(playerIndex); + if (p == null) return; // no player for this controller + p.boost(x, y); + mTaps++; + if (DEBUG) { + p.dv *= DEBUG_SPEED_MULTIPLIER; + p.animate().setDuration((long) (200 / DEBUG_SPEED_MULTIPLIER)); + } } } - private void unpoke() { - L("unboost"); - if (mFrozen) return; - if (!mAnimating) return; - mDroid.unboost(); + private void unpoke(int playerIndex) { + L("unboost(%d)", playerIndex); + if (mFrozen || !mAnimating || !mPlaying) return; + final Player p = getPlayer(playerIndex); + if (p == null) return; // no player for this controller + p.unboost(); } @Override public void onDraw(Canvas c) { super.onDraw(c); + if (SHOW_TOUCHES) { + for (Player p : mPlayers) { + if (p.mTouchX > 0) { + mTouchPaint.setColor(0x80FFFFFF & p.color); + mPlayerTracePaint.setColor(0x80FFFFFF & p.color); + float x1 = p.mTouchX; + float y1 = p.mTouchY; + c.drawCircle(x1, y1, 100, mTouchPaint); + float x2 = p.getX() + p.getPivotX(); + float y2 = p.getY() + p.getPivotY(); + float angle = PI_2 - (float) Math.atan2(x2-x1, y2-y1); + x1 += 100*Math.cos(angle); + y1 += 100*Math.sin(angle); + c.drawLine(x1, y1, x2, y2, mPlayerTracePaint); + } + } + } + if (!DEBUG_DRAW) return; final Paint pt = new Paint(); pt.setColor(0xFFFFFFFF); - final int L = mDroid.corners.length; - final int N = L/2; - for (int i=0; i<N; i++) { - final int x = (int) mDroid.corners[i*2]; - final int y = (int) mDroid.corners[i*2+1]; - c.drawCircle(x, y, 4, pt); - c.drawLine(x, y, - mDroid.corners[(i*2+2)%L], - mDroid.corners[(i*2+3)%L], - pt); + for (Player p : mPlayers) { + final int L = p.corners.length; + final int N = L / 2; + for (int i = 0; i < N; i++) { + final int x = (int) p.corners[i * 2]; + final int y = (int) p.corners[i * 2 + 1]; + c.drawCircle(x, y, 4, pt); + c.drawLine(x, y, + p.corners[(i * 2 + 2) % L], + p.corners[(i * 2 + 3) % L], + pt); + } } pt.setStyle(Paint.Style.STROKE); @@ -685,11 +979,11 @@ public class LLand extends FrameLayout { pt.setColor(0x8000FF00); for (int i=0; i<M; i++) { final View v = getChildAt(i); - if (v == mDroid) continue; + if (v instanceof Player) continue; if (!(v instanceof GameView)) continue; if (v instanceof Pop) { - final Pop p = (Pop) v; - c.drawCircle(p.cx, p.cy, p.r, pt); + final Pop pop = (Pop) v; + c.drawCircle(pop.cx, pop.cy, pop.r, pt); } else { final Rect r = new Rect(); v.getHitRect(r); @@ -713,14 +1007,26 @@ public class LLand extends FrameLayout { public void step(long t_ms, long dt_ms, float t, float dt); } - private class Player extends ImageView implements GameView { + private static class Player extends ImageView implements GameView { public float dv; - + public int color; + private MLand mLand; private boolean mBoosting; + private float mTouchX = -1, mTouchY = -1; + private boolean mAlive; + private int mScore; + private TextView mScoreField; private final int[] sColors = new int[] { - 0xFF78C557, + //0xFF78C557, + 0xFFDB4437, + 0xFF3B78E7, + 0xFFF4B400, + 0xFF0F9D58, + 0xFF7B1880, + 0xFF9E9E9E, }; + static int sNextColor = 0; private final float[] sHull = new float[] { 0.3f, 0f, // left antenna @@ -734,12 +1040,58 @@ public class LLand extends FrameLayout { }; public final float[] corners = new float[sHull.length]; + public static Player create(MLand land) { + final Player p = new Player(land.getContext()); + p.mLand = land; + p.reset(); + p.setVisibility(View.INVISIBLE); + land.addView(p, new LayoutParams(PARAMS.PLAYER_SIZE, PARAMS.PLAYER_SIZE)); + return p; + } + + private void setScore(int score) { + mScore = score; + if (mScoreField != null) { + mScoreField.setText(DEBUG_IDDQD ? "??" : String.valueOf(score)); + } + } + + public int getScore() { + return mScore; + } + + private void addScore(int incr) { + setScore(mScore + incr); + } + + public void setScoreField(TextView tv) { + mScoreField = tv; + if (tv != null) { + setScore(mScore); // reapply + //mScoreField.setBackgroundResource(R.drawable.scorecard); + mScoreField.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_ATOP); + mScoreField.setTextColor(luma(color) > 0.7f ? 0xFF000000 : 0xFFFFFFFF); + } + } + + public void reset() { + //setX(mLand.mWidth / 2); + setY(mLand.mHeight / 2 + + (int)(Math.random() * PARAMS.PLAYER_SIZE) + - PARAMS.PLAYER_SIZE / 2); + setScore(0); + setScoreField(mScoreField); // refresh color + mBoosting = false; + dv = 0; + } + public Player(Context context) { super(context); setBackgroundResource(R.drawable.android); getBackground().setTintMode(PorterDuff.Mode.SRC_ATOP); - getBackground().setTint(sColors[0]); + color = sColors[(sNextColor++%sColors.length)]; + getBackground().setTint(color); setOutlineProvider(new ViewOutlineProvider() { @Override public void getOutline(View view, Outline outline) { @@ -774,7 +1126,11 @@ public class LLand extends FrameLayout { } public void step(long t_ms, long dt_ms, float t, float dt) { - if (getVisibility() != View.VISIBLE) return; // not playing yet + if (!mAlive) { + // float away with the garbage + setTranslationX(getTranslationX()-PARAMS.TRANSLATION_PER_SEC*dt); + return; + } if (mBoosting) { dv = -PARAMS.BOOST_DV; @@ -792,6 +1148,12 @@ public class LLand extends FrameLayout { prepareCheckIntersections(); } + public void boost(float x, float y) { + mTouchX = x; + mTouchY = y; + boost(); + } + public void boost() { mBoosting = true; dv = -PARAMS.BOOST_DV; @@ -808,6 +1170,7 @@ public class LLand extends FrameLayout { public void unboost() { mBoosting = false; + mTouchX = mTouchY = -1; animate().cancel(); animate() @@ -816,6 +1179,19 @@ public class LLand extends FrameLayout { .translationZ(PARAMS.PLAYER_Z) .setDuration(200); } + + public void die() { + mAlive = false; + if (mScoreField != null) { + //mScoreField.setTextColor(0xFFFFFFFF); + //mScoreField.getBackground().setColorFilter(0xFF666666, PorterDuff.Mode.SRC_ATOP); + //mScoreField.setBackgroundResource(R.drawable.scorecard_gameover); + } + } + + public void start() { + mAlive = true; + } } private class Obstacle extends View implements GameView { @@ -855,20 +1231,31 @@ public class LLand extends FrameLayout { } } + static final int[] ANTENNAE = new int[] {R.drawable.mm_antennae, R.drawable.mm_antennae2}; + static final int[] EYES = new int[] {R.drawable.mm_eyes, R.drawable.mm_eyes2}; + static final int[] MOUTHS = new int[] {R.drawable.mm_mouth1, R.drawable.mm_mouth2, + R.drawable.mm_mouth3, R.drawable.mm_mouth4}; private class Pop extends Obstacle { int mRotate; int cx, cy, r; + // The marshmallow illustration and hitbox is 2/3 the size of its container. + Drawable antenna, eyes, mouth; + + public Pop(Context context, float h) { super(context, h); - int idx = 3*irand(0, POPS.length/3); - setBackgroundResource(POPS[idx]); - setAlpha((float)(POPS[idx+2])/255); - setScaleX(frand() < 0.5f ? -1 : 1); - mRotate = POPS[idx+1] == 0 ? 0 : (frand() < 0.5f ? -1 : 1); + setBackgroundResource(R.drawable.mm_head); + antenna = context.getDrawable(pick(ANTENNAE)); + if (frand() > 0.5f) { + eyes = context.getDrawable(pick(EYES)); + if (frand() > 0.8f) { + mouth = context.getDrawable(pick(MOUTHS)); + } + } setOutlineProvider(new ViewOutlineProvider() { @Override public void getOutline(View view, Outline outline) { - final int pad = (int) (getWidth() * 0.02f); + final int pad = (int) (getWidth() * 1f/6); outline.setOval(pad, pad, getWidth()-pad, getHeight()-pad); } }); @@ -893,20 +1280,56 @@ public class LLand extends FrameLayout { cx = (hitRect.left + hitRect.right)/2; cy = (hitRect.top + hitRect.bottom)/2; - r = getWidth()/2; + r = getWidth() / 3; // see above re 2/3 container size + } + + @Override + public void onDraw(Canvas c) { + super.onDraw(c); + if (antenna != null) { + antenna.setBounds(0, 0, c.getWidth(), c.getHeight()); + antenna.draw(c); + } + if (eyes != null) { + eyes.setBounds(0, 0, c.getWidth(), c.getHeight()); + eyes.draw(c); + } + if (mouth != null) { + mouth.setBounds(0, 0, c.getWidth(), c.getHeight()); + mouth.draw(c); + } } } private class Stem extends Obstacle { Paint mPaint = new Paint(); Path mShadow = new Path(); + GradientDrawable mGradient = new GradientDrawable(); boolean mDrawShadow; + Path mJandystripe; + Paint mPaint2; + int id; // use this to track which pipes have been cleared public Stem(Context context, float h, boolean drawShadow) { super(context, h); + id = mCurrentPipeId; + mDrawShadow = drawShadow; - mPaint.setColor(0xFFAAAAAA); setBackground(null); + mGradient.setOrientation(GradientDrawable.Orientation.LEFT_RIGHT); + mPaint.setColor(0xFF000000); + mPaint.setColorFilter(new PorterDuffColorFilter(0x22000000, PorterDuff.Mode.MULTIPLY)); + + if (frand() < 0.01f) { + mGradient.setColors(new int[]{0xFFFFFFFF, 0xFFDDDDDD}); + mJandystripe = new Path(); + mPaint2 = new Paint(); + mPaint2.setColor(0xFFFF0000); + mPaint2.setColorFilter(new PorterDuffColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY)); + } else { + //mPaint.setColor(0xFFA1887F); + mGradient.setColors(new int[]{0xFFBCAAA4, 0xFFA1887F}); + } } @Override @@ -924,18 +1347,29 @@ public class LLand extends FrameLayout { public void onDraw(Canvas c) { final int w = c.getWidth(); final int h = c.getHeight(); - final GradientDrawable g = new GradientDrawable(); - g.setOrientation(GradientDrawable.Orientation.LEFT_RIGHT); - g.setGradientCenter(w * 0.75f, 0); - g.setColors(new int[] { 0xFFFFFFFF, 0xFFAAAAAA }); - g.setBounds(0, 0, w, h); - g.draw(c); + mGradient.setGradientCenter(w * 0.75f, 0); + mGradient.setBounds(0, 0, w, h); + mGradient.draw(c); + + if (mJandystripe != null) { + mJandystripe.reset(); + mJandystripe.moveTo(0, w); + mJandystripe.lineTo(w, 0); + mJandystripe.lineTo(w, 2 * w); + mJandystripe.lineTo(0, 3 * w); + mJandystripe.close(); + for (int y=0; y<h; y+=4*w) { + c.drawPath(mJandystripe, mPaint2); + mJandystripe.offset(0, 4 * w); + } + } + if (!mDrawShadow) return; mShadow.reset(); - mShadow.moveTo(0,0); + mShadow.moveTo(0, 0); mShadow.lineTo(w, 0); - mShadow.lineTo(w, PARAMS.OBSTACLE_WIDTH/2+w*1.5f); - mShadow.lineTo(0, PARAMS.OBSTACLE_WIDTH/2); + mShadow.lineTo(w, PARAMS.OBSTACLE_WIDTH * 0.4f + w*1.5f); + mShadow.lineTo(0, PARAMS.OBSTACLE_WIDTH * 0.4f); mShadow.close(); c.drawPath(mShadow, mPaint); } @@ -961,11 +1395,30 @@ public class LLand extends FrameLayout { w = irand(PARAMS.BUILDING_WIDTH_MIN, PARAMS.BUILDING_WIDTH_MAX); h = 0; // will be setup later, along with z + } + } + + static final int[] CACTI = { R.drawable.cactus1, R.drawable.cactus2, R.drawable.cactus3 }; + private class Cactus extends Building { + public Cactus(Context context) { + super(context); - setTranslationZ(PARAMS.SCENERY_Z); + setBackgroundResource(pick(CACTI)); + w = h = irand(PARAMS.BUILDING_WIDTH_MAX / 4, PARAMS.BUILDING_WIDTH_MAX / 2); } } + static final int[] MOUNTAINS = { + R.drawable.mountain1, R.drawable.mountain2, R.drawable.mountain3 }; + private class Mountain extends Building { + public Mountain(Context context) { + super(context); + + setBackgroundResource(pick(MOUNTAINS)); + w = h = irand(PARAMS.BUILDING_WIDTH_MAX / 2, PARAMS.BUILDING_WIDTH_MAX); + z = 0; + } + } private class Cloud extends Scenery { public Cloud(Context context) { super(context); diff --git a/packages/SystemUI/src/com/android/systemui/egg/MLandActivity.java b/packages/SystemUI/src/com/android/systemui/egg/MLandActivity.java new file mode 100644 index 0000000..cdda45f --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/egg/MLandActivity.java @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2015 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.android.systemui.egg; + +import android.app.Activity; +import android.os.Bundle; +import android.view.View; +import android.view.ViewGroup; + +import com.android.systemui.R; + +public class MLandActivity extends Activity { + MLand mLand; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.mland); + mLand = (MLand) findViewById(R.id.world); + mLand.setScoreFieldHolder((ViewGroup) findViewById(R.id.scores)); + final View welcome = findViewById(R.id.welcome); + mLand.setSplash(welcome); + final int numControllers = mLand.getGameControllers().size(); + if (numControllers > 0) { + mLand.setupPlayers(numControllers); + } + } + + public void updateSplashPlayers() { + final int N = mLand.getNumPlayers(); + final View minus = findViewById(R.id.player_minus_button); + final View plus = findViewById(R.id.player_plus_button); + if (N == 1) { + minus.setVisibility(View.INVISIBLE); + plus.setVisibility(View.VISIBLE); + plus.requestFocus(); + } else if (N == mLand.MAX_PLAYERS) { + minus.setVisibility(View.VISIBLE); + plus.setVisibility(View.INVISIBLE); + minus.requestFocus(); + } else { + minus.setVisibility(View.VISIBLE); + plus.setVisibility(View.VISIBLE); + } + } + + @Override + public void onPause() { + mLand.stop(); + super.onPause(); + } + + @Override + public void onResume() { + super.onResume(); + + mLand.onAttachedToWindow(); // resets and starts animation + updateSplashPlayers(); + mLand.showSplash(); + } + + public void playerMinus(View v) { + mLand.removePlayer(); + updateSplashPlayers(); + } + + public void playerPlus(View v) { + mLand.addPlayer(); + updateSplashPlayers(); + } + + public void startButtonPressed(View v) { + findViewById(R.id.player_minus_button).setVisibility(View.INVISIBLE); + findViewById(R.id.player_plus_button).setVisibility(View.INVISIBLE); + mLand.start(true); + } +} diff --git a/packages/SystemUI/src/com/android/systemui/egg/ShruggyActivity.java b/packages/SystemUI/src/com/android/systemui/egg/ShruggyActivity.java deleted file mode 100644 index 7459957..0000000 --- a/packages/SystemUI/src/com/android/systemui/egg/ShruggyActivity.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (C) 2015 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.android.systemui.egg; - -import android.app.Activity; -import android.os.Bundle; -import android.util.Log; -import android.widget.Toast; -import com.android.systemui.R; - -public class ShruggyActivity extends Activity { - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - Toast.makeText(this, getString(R.string.regrettable_lack_of_easter_egg), - Toast.LENGTH_SHORT).show(); - Log.v("SystemUI", "Hey, it's just a preview; what did you expect?"); - finish(); - } -} |