New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provider system for the message api #6
Merged
+309
−4
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5ba2ee5
Adding provider behaviour to allow new Telegraph to retreive last msg
avianey e36cf73
Rename provider method for better understanding
avianey 447687f
Providers can send different info depending on the registering Telegraph
avianey 72b1d33
Merge remote-tracking branch 'ai/master' into providers
avianey fb0802c
Rename Provider and add Test
avianey 0759300
Add licence header
avianey a42cbe3
Code formatting and Javadoc
avianey File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.
| @@ -0,0 +1,31 @@ | ||
| /******************************************************************************* | ||
| * Copyright 2011 See AUTHORS file. | ||
| * | ||
| * 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.badlogic.gdx.ai.msg; | ||
|
|
||
| /** Telegram providers respond to {@link MessageDispatcher#addListener} by providing optional {@link Telegram#extraInfo} to be sent | ||
| * in a Telegram of a given type to the newly registered {@link Telegraph}. | ||
| * @author avianey */ | ||
| public interface TelegramProvider { | ||
| /** Provides {@link Telegram#extraInfo} to dispatch immediately when a {@link Telegraph} is registered for the given message | ||
| * type. | ||
| * @param msg the message type to provide | ||
| * @param receiver the newly registered Telegraph. Providers can provide different info depending on the targeted Telegraph. | ||
| * @return extra info to dispatch in a Telegram or null if nothing to dispatch | ||
| * @see com.badlogic.gdx.ai.msg.MessageDispatcher#addListener(Telegraph, int) | ||
| * @see com.badlogic.gdx.ai.msg.MessageDispatcher#addListeners(Telegraph, int...) */ | ||
| Object provideMessageInfo (int msg, Telegraph receiver); | ||
| } |
| @@ -0,0 +1,55 @@ | ||
| /******************************************************************************* | ||
| * Copyright 2011 See AUTHORS file. | ||
| * | ||
| * 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.badlogic.gdx.ai.tests; | ||
|
|
||
| import com.badlogic.gdx.Gdx; | ||
| import com.badlogic.gdx.ai.msg.MessageDispatcher; | ||
| import com.badlogic.gdx.ai.tests.msg.City; | ||
| import com.badlogic.gdx.ai.tests.utils.GdxAiTest; | ||
|
|
||
| /** A simple test to demonstrate telegram providers.<br/> | ||
| * Build a ideal city where every new citizen say hello to every existing citizens... | ||
| * @author avianey */ | ||
| public class TelegramProviderTest extends GdxAiTest { | ||
|
|
||
| public static final int MSG_TIME_TO_ACT = 0; | ||
| public static final int MSG_EXISTING_CITIZEN = 1; | ||
|
|
||
| public static void main (String[] args) { | ||
| launch(new TelegramProviderTest()); | ||
| } | ||
|
|
||
| City city; | ||
| float elapsedTime; | ||
|
|
||
| @Override | ||
| public void create () { | ||
| elapsedTime = 0; | ||
| // build a new city | ||
| city = new City(); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void render () { | ||
| elapsedTime += Gdx.graphics.getRawDeltaTime(); | ||
| if (elapsedTime > 0.8f) { | ||
| MessageDispatcher.getInstance().dispatchMessage(null, MSG_TIME_TO_ACT); | ||
| elapsedTime = 0; | ||
| } | ||
| } | ||
| } |
| @@ -0,0 +1,59 @@ | ||
| /******************************************************************************* | ||
| * Copyright 2011 See AUTHORS file. | ||
| * | ||
| * 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.badlogic.gdx.ai.tests.msg; | ||
|
|
||
| import com.badlogic.gdx.Gdx; | ||
| import com.badlogic.gdx.ai.msg.MessageDispatcher; | ||
| import com.badlogic.gdx.ai.msg.Telegram; | ||
| import com.badlogic.gdx.ai.msg.TelegramProvider; | ||
| import com.badlogic.gdx.ai.msg.Telegraph; | ||
| import com.badlogic.gdx.ai.tests.TelegramProviderTest; | ||
|
|
||
| /** @author avianey */ | ||
| public class Citizen implements Telegraph, TelegramProvider { | ||
|
|
||
| static int NUM = 0; | ||
|
|
||
| final int num; | ||
| final House house; | ||
|
|
||
| public Citizen (House house) { | ||
| this.num = NUM++; | ||
| this.house = house; | ||
| Gdx.app.log(Citizen.class.getSimpleName() + " " + num, "Hi there, I'm new in town and I live in house number " + house.num); | ||
| MessageDispatcher.getInstance().addListener(this, TelegramProviderTest.MSG_EXISTING_CITIZEN); | ||
| MessageDispatcher.getInstance().addProvider(this, TelegramProviderTest.MSG_EXISTING_CITIZEN); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean handleMessage (Telegram msg) { | ||
| Citizen citizen = (Citizen)msg.extraInfo; | ||
| // greet only if not in the same house | ||
| if (this.house.num != citizen.house.num) { | ||
| Gdx.app.log(Citizen.class.getSimpleName() + " " + num, "Hi " + Citizen.class.getSimpleName() + " " + citizen.num | ||
| + ", I'm your new neighbour"); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public Object provideMessageInfo (int msg, Telegraph receiver) { | ||
| // when a new citizen come to town we tell him that we exists | ||
| return this; | ||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,47 @@ | ||
| /******************************************************************************* | ||
| * Copyright 2011 See AUTHORS file. | ||
| * | ||
| * 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.badlogic.gdx.ai.tests.msg; | ||
|
|
||
| import com.badlogic.gdx.Gdx; | ||
| import com.badlogic.gdx.ai.msg.MessageDispatcher; | ||
| import com.badlogic.gdx.ai.msg.Telegram; | ||
| import com.badlogic.gdx.ai.msg.Telegraph; | ||
| import com.badlogic.gdx.ai.tests.TelegramProviderTest; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| /** @author avianey */ | ||
| public class City implements Telegraph { | ||
|
|
||
| List<House> houses; | ||
|
|
||
| public City () { | ||
| Gdx.app.log(City.class.getSimpleName(), "A new city is born..."); | ||
| houses = new ArrayList<>(); | ||
| MessageDispatcher.getInstance().addListeners(this, TelegramProviderTest.MSG_TIME_TO_ACT); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean handleMessage (Telegram msg) { | ||
| // build a new house | ||
| if (houses.size() <= 10) { | ||
| houses.add(new House()); | ||
| } | ||
| return false; | ||
| } | ||
| } |
| @@ -0,0 +1,55 @@ | ||
| /******************************************************************************* | ||
| * Copyright 2011 See AUTHORS file. | ||
| * | ||
| * 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.badlogic.gdx.ai.tests.msg; | ||
|
|
||
| import com.badlogic.gdx.Gdx; | ||
| import com.badlogic.gdx.ai.msg.MessageDispatcher; | ||
| import com.badlogic.gdx.ai.msg.Telegram; | ||
| import com.badlogic.gdx.ai.msg.Telegraph; | ||
| import com.badlogic.gdx.ai.tests.TelegramProviderTest; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| /** @author avianey */ | ||
| public class House implements Telegraph { | ||
|
|
||
| static int NUM = 0; | ||
|
|
||
| List<Citizen> citizens; | ||
| final int num; | ||
|
|
||
| public House () { | ||
| num = NUM++; | ||
| citizens = new ArrayList<>(); | ||
| Gdx.app.log(House.class.getSimpleName() + " " + num, "New house in town"); | ||
| // Mr & Mrs | ||
| citizens.add(new Citizen(this)); | ||
| citizens.add(new Citizen(this)); | ||
| MessageDispatcher.getInstance().addListeners(this, TelegramProviderTest.MSG_TIME_TO_ACT); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean handleMessage (Telegram msg) { | ||
| if (citizens.size() < 3) { | ||
| // new child | ||
| Gdx.app.log(House.class.getSimpleName() + " " + num, "We're having a baby!"); | ||
| citizens.add(new Citizen(this)); | ||
| } | ||
| return false; | ||
| } | ||
| } |
ProTip!
Use n and p to navigate between commits in a pull request.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
for people who still use jdk7 or lower