Back to project page Sertimus.
The source code is released under:
GNU General Public License
If you think the Android project Sertimus listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* Sertimus - A live wallpaper featuring a cute Chao. // ww w . j av a 2s . co m Copyright (C) 2013 Kevin Negrin This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ // Sertimus.java - Class describing the main character of the wallpaper. package gameaddict30.wallpaper.sertimus.objects; import java.io.BufferedInputStream; import java.io.IOException; import gameaddict30.wallpaper.sertimus.*; import gameaddict30.wallpaper.sertimus.constants.CharacterAnimation; import gameaddict30.wallpaper.sertimus.events.LiveObjectMotionEvent; import gameaddict30.wallpaper.sertimus.interfaces.*; import android.graphics.*; import android.util.Log; import android.content.Context; public final class Sertimus extends LiveObject implements InteractiveLiveObject,TalkableLiveObject { private final int movingDelay = 5; // In seconds private int timeToMove = 0; private float futureX,futureY; private boolean finishedMoving = false; protected float velocity = 1f; private Bitmap currentFrame, sheetArray[][]; private int animationTimeline = 0; private int direction = 1; private String[] talkLines; private int timeToTalk = 0; private final int talkDelay = 15; // In seconds private boolean finishedTalking = true; public Sertimus(MainCanvas c) { super(c, "images/sheets/sertimussheet_optimized.png"); try { initTalkable(); } catch (IOException e) { talkLines = new String[] {"How embarrassing. I don't really have much to say!"}; } width = 118; height = 118; offsetX = -1*(width/2); offsetY = -1*(height-18); x = (int)(Math.random() * (c.getWidth()+1-(width*2))); y = (int)(Math.random() * (c.getHeight()+1-(height*2))); generateLocation(); } private void generateLocation() { // Propose a new location for Sertimus to move to. generateLocation((int)(Math.random() * (refCanvas.getWidth()+1-(width*2))), (int)(Math.random() * (refCanvas.getHeight()+1-(height*2)))); } private synchronized void generateLocation(float x, float y) { // Propose a new location for Sertimus to move to. futureX = x; futureY = y; timeToMove = 0; Log.i("Sertimus", "futureX = "+futureX+" futureY = "+futureY); } protected void initTalkable() throws IOException { BufferedInputStream b = new BufferedInputStream(refCanvas.getAppContext().getAssets().open("text/sertimus_lines")); String strFileContents = ""; int bytesRead = 0; byte[] contents = new byte[1024]; while ((bytesRead = b.read(contents)) != -1) strFileContents += new String(contents, 0, bytesRead); b.close(); talkLines = strFileContents.split("\n"); } @Override public void updateObject() { // Location logic randomLocation(); // Movement move(); // Animation logic animate(); // Chatty logic (are those even a good choice of words) talk(); } private void talk() { if (finishedTalking) ++timeToTalk; if ((timeToTalk/MainService.frameRate) >= talkDelay) { new TalkBubble(refCanvas, this, talkLines[(int)(Math.random()*talkLines.length)]); timeToTalk = 0; finishedTalking = false; } } protected void randomLocation() { if (finishedMoving && finishedTalking) ++timeToMove; // When the time is right, generate a random location for Sertimus to move at if ((timeToMove/MainService.frameRate) >= movingDelay) { generateLocation(); timeToMove = 0; finishedMoving = false; } } protected void move() { if (x != futureX || y != futureY) { if (x < futureX) super.move(velocity,0); else if (x > futureX) super.move(-1*velocity,0); if (y < futureY) super.move(0,velocity); else if (y > futureY) super.move(0,-1*velocity); } } protected void animate() { if (y > futureY) { if (direction != 0) direction = 0; updateFrame(CharacterAnimation.ANIMATION_WALK_B); } else if (y < futureY) { if (direction != 1) direction = 1; updateFrame(CharacterAnimation.ANIMATION_WALK_F); } else if (x != futureX) { switch (direction) { case 0: updateFrame(CharacterAnimation.ANIMATION_WALK_B); break; case 1: updateFrame(CharacterAnimation.ANIMATION_WALK_F); break; } } else { if (!finishedMoving) finishedMoving = true; switch (direction) { case 0: updateFrame(CharacterAnimation.ANIMATION_IDLE_B); break; case 1: updateFrame(CharacterAnimation.ANIMATION_IDLE_F); break; } } } private void updateFrame(CharacterAnimation a) { int[] currentFrameSequence = a.getFrameSequence(); int delay = a.getDelay(); if (animationTimeline/delay >= currentFrameSequence.length || currentFrameSequence[(int)(animationTimeline/delay)] >= sheetArray[a.getRow()].length) animationTimeline = 0; if ((animationTimeline/delay) == Math.floor(animationTimeline/delay)) { currentFrame = sheetArray[a.getRow()][currentFrameSequence[(int)(animationTimeline/delay)]]; } animationTimeline++; } @Override public void drawObject(Canvas c) { c.drawBitmap(currentFrame, x+offsetX, y+offsetY, null); } @Override protected void loadImageResource(Context c, int r) { super.loadImageResource(c, r); initializeSheet(); } @Override protected void loadImageAsset(Context c, String s) throws IOException { super.loadImageAsset(c, s); initializeSheet(); } protected void initializeSheet() { int rows = 0,columns = 0; for (; rows*120 < image.getHeight(); rows++) for (; columns*120 < image.getWidth(); columns++) ; sheetArray = new Bitmap[rows][columns]; for (int y=0; y < rows; y++) for (int x=0; x < columns; x++) sheetArray[y][x] = Bitmap.createBitmap(image, (120*x)+1, (120*y)+1, 118, 118); image.recycle(); } public void onTouchEvent(LiveObjectMotionEvent e) { generateLocation(e.getX(),e.getY()); } @Override public void onSurfaceChanged(int oW, int oH, int nW, int nH) { super.onSurfaceChanged(oW, oH, nW, nH); generateLocation(x,y); } @Override public void onFinishTalk() { finishedTalking = true; } }